递归穷举法

// Java
import java.security.MessageDigest; import java.util.ArrayList; import java.util.Arrays; import java.util.List;
public class MD5Util { public static final String MD5(String inStr) { MessageDigest md5 = null; try { md5 = MessageDigest.getInstance("MD5"); } catch (Exception e) { System.out.println(e.toString()); e.printStackTrace(); return ""; } char[] charArray = inStr.toCharArray(); byte[] byteArray = new byte[charArray.length]; for (int i = 0; i < charArray.length; i++) byteArray[i] = (byte) charArray[i]; byte[] md5Bytes = md5.digest(byteArray); StringBuffer hexValue = new StringBuffer(); for (int i = 0; i < md5Bytes.length; i++) { int val = ((int) md5Bytes[i]) & 0xff; if (val < 16) hexValue.append("0"); hexValue.append(Integer.toHexString(val)); } return hexValue.toString(); } private static final List<Character> chars = Arrays.asList('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '~', '`', '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '-', '_', '=', '+', '[', ']', '{', '}', '\\', '|', ';', ':', '\'', '"', ',', '<', '.', '>', '/', '?', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'); static List<String> strings = new ArrayList<>(); public static List<String> exhaustive(int count) { return exhaustive(count, ""); } public static List<String> exhaustive(int count, String param) { if (count == 0) { for (Character character : chars) { String result = param + character; System.out.println(result); strings.add(result); } } else { count -= 1; for (Character character : chars) { exhaustive(count, param + character.toString()); } } return strings; } public static void main(String[] args) { for (int i = 0; i < 4; i++) { System.out.println(exhaustive(i)); } } }

 

python:

import md5
import hashlib

def md5_encryption(src):
  src = '123456'
  enc = md5.new()
  enc.update(src)
  print enc.hexdigest()
# md5_encryption('123456')

def hashlib_md5_encryption(src):
  m2 = hashlib.md5()   
  m2.update(src)   
  print m2.hexdigest()
# hashlib_md5_encryption('123456')

_chars = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
          'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
           '~', '`', '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '-', '_', '=', '+', '[', ']', '{', '}', '\\', '|', ';', ':', '\'',
           '"', ',', '<', '.', '>', '/', '?', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9']


# results = []
def exhaustive(max_digit=0, add_str=''):
  if max_digit == 0:
    for char in _chars:
      result = '%s%s' % (add_str, char)
#       results.append(result)
      print result
  else:
    max_digit -= 1
    for char in _chars:
      exhaustive(max_digit, '%s%s' % (add_str, char));
#   return results
  
if __name__ == '__main__':
  exhaustive(3)

稍微改点理论上就可以无穷列举密码,但是只是理论,电脑根本承受不了,没那么大内存。

posted @ 2017-03-02 13:59  bad_boy_f  阅读(729)  评论(0编辑  收藏  举报