-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathBruteCrack.java
120 lines (99 loc) · 2.14 KB
/
BruteCrack.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import java.security.MessageDigest;
public class BruteCrack
{
MessageDigest md;
char min_char_value;
char max_char_value;
char[] guess;
int max_num_chars;
public BruteCrack() throws Exception
{
min_char_value = 32;
max_char_value = 126;
max_num_chars = 10;
md = MessageDigest.getInstance("MD5");
guess = null;
}
public String crack(String hash)
{
boolean done = false;
String guess_hash;
for(int num_chars = 0; num_chars < max_num_chars && !done; num_chars++)
{
// Initialize guess at the start of each interation
guess = new char[num_chars];
for(int x = 0; x < num_chars; x++)
{
guess[x] = min_char_value;
}
while(canIncrementGuess() && !done)
{
incrementGuess();
md.reset();
md.update(new String(guess).getBytes());
guess_hash = hashToString(md.digest());
if(hash.equals(guess_hash))
{
done = true;
}
}
}
return new String(guess);
}
protected boolean canIncrementGuess()
{
boolean canIncrement = false;
for(int x=0; x < guess.length; x++)
{
if(guess[x] < max_char_value) canIncrement = true;
}
return canIncrement;
}
protected void incrementGuess()
{
boolean incremented = false;
for(int x = (guess.length - 1);x >= 0 && !incremented; x--)
{
if(guess[x] < max_char_value)
{
guess[x]++;
if(x < (guess.length-1))
{
guess[x+1] = min_char_value;
}
incremented = true;
}
}
}
protected String hashToString(byte[] hash)
{
StringBuffer sb = new StringBuffer();
for(int i = 0; i < hash.length; i++)
{
sb.append(Integer.toString((hash[i] & 0xff) + 0x100, 16).substring(1));
}
return sb.toString();
}
public static void main(String args[])
{
if(args.length > 0)
{
try
{
BruteCrack bc = new BruteCrack();
long start;
long end;
String answer;
start = System.nanoTime();
answer = bc.crack(args[0]);
end = System.nanoTime();
System.out.println("Answer: " + answer);
System.out.println("Processing Time: " + ((end - start)/1000000000));
}
catch(Exception e)
{
System.out.println("Exception: " + e.toString());
}
}
}
}