Print string with variable length?

I have an MD5 hash for String stored as String . I am writing a small program to find the source string by brute force.

I would like to go through a subset of char .

Below is the code below if String.length() == 0 .

I cannot figure out how to edit this code to work with variable length String s. 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 must mention this for the MD5 academic study.

+4
source share
2 answers

You do a " Depth first " search (and unlimited depth!), It is almost guaranteed to fail (exhausting your stack) unless you add some depth checking.

Perhaps you are better off Quickly search by width : your loop should first try all the combinations, which leads to the addition of a character, and only then, if it does not succeed, try recursively calling the method with each line added.

In any case, you should add some depth check, always.

Edited: Thinking about it twice, I’m not sure that you should not stick to the depth first. The width here is first blurred here for shallow depths and combinations (character ranges). Possible implementation

  // Breadth first returns null if not found public String bruteforce(List<String> prefixes, String md5,int availdepth) { if(availabledepth<0) return null; List<String> newprefixes = new ArrayList<String>(); for(String prefix : prefixes) { for (char c = ' '; c < '~'; ++c) { String attempt = prefix + Character.toString(c); generatedMd5 = generateMD5(attempt); if (hashesMatch(md5, generatedMd5)) return attempt; newprefixes.add(attempt); } } // no success in this level go for next return bruteforce(newprefixes,md5,availddepth-1); } // Depth first - returns null if not found public String bruteforce(String prefix, String md5,int availdepth) { if(availdepth <= 0) return null; for (char c = ' '; c < '~'; ++c) { String attempt = prefix + Character.toString(c); if (hashesMatch(md5, generateMD5(attempt))) return attempt; String res = bruteforce(attempt, md5, availdepth-1); if(res != null) return res; } return null; } 
+6
source

the first of you does not return a result ...

second you do the depth first in infinite space (this will never end ...)

 public String attempt(String initial, String md5,int depth) { if(depth < 0)return null;//failed backtrack for (char c = ' '; c < '~'; ++c) { attempt = initial + Character.toString(c); generatedMd5 = generateMD5(attempt); System.out.println(attempt); if (hashesMatch(md5, generatedMd5)) { return attempt;//success } else { String res = attempt(attempt, md5,depth-1); if(res!=null)return res; } } } 

this limited depth at first means that it will not recursively go further than depth (and returns null when it cannot find anything

to move throughout the space to a depth of 10,000, you can use the following:

 String attempt(String initial,String md5){ for(int i = 5;i<10000;i++){ String res = attempt(initial,md5,i); if(res!= null)return res; } return null;//let not go overboard on recursion } 

I set the maximum size in recursion to 10,000 to complete it in the near future

+1
source

All Articles