I have the MD5 hash for a String, stored as a String. I’m writing a small program to find out the original string by brute force.
I’d like to loop through a subset of char.
The code below works for when String.length() == 0.
I can’t figure out how to edit this code to work with variable-length Strings. I feel like I’m on the right track with recursion, but can’t go any further.
I have the following code:
public void attempt(String initial, String md5) {
for (char c = ' '; c < '~'; ++c) {
attempt = initial + Character.toString(c);
generatedMd5 = generateMD5(attempt);
System.out.println(attempt);
if (hashesMatch(md5, generatedMd5)) {
break;
} else attempt(attempt, md5);
}
}
Note: I should mention this is for an academic study on MD5.
You are doing a “Depth first” search (and of unlimited depth!), this is almost guaranteed to fail ( exhausting your stack) if you do not add some depth check.
You
should probably bettermight want to do a Breadth first search : your loop should first try all the combinations that results in adding a character, and only then, if no success, try to recursively call the method with each augmented string.In any case, you should add some depth check, always.
Edited: thinking it twice, I’m not so sure you should not stick with depth first. Breadth first is only rasonable here for small depths and combinations (character ranges). A possible implementation