1

我正在使用一些 MD5 哈希值,这些哈希值已使用下面显示的方法转换为数字序列。给定下面的代码和示例哈希,我如何“反转”该getString()方法的效果并将数字序列转换回 MD5 哈希?

例如,encrypt("umadbro");返回“1518918615824625494170109603025017352201241”,因为 MD5 哈希是通过该getString()方法传递的。“umadbro”的 MD5 哈希是 9759ba9ef6fe5eaa6d3c1efaad34c9f1。我需要一个方法,它接受由该getString()方法修改的一串数字并将其转换为它的 MD5 哈希。例如:reverseMethod("1518918615824625494170109603025017352201241");应该输出“9759ba9ef6fe5eaa6d3c1efaad34c9f1”(输入参数为修改后的数字串,输出为原字符串的MD5哈希)。注意:我不是在寻找破解 MD5 哈希的方法。我只需要一个反转下getString()图所示方法效果的方法。

    public String encrypt(String source)
        {
            try
            {
                MessageDigest md = MessageDigest.getInstance("MD5");
                byte[] bytes = md.digest(source.getBytes());
                return getString(bytes);
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
            return null;
        }

    private String getString(byte[] bytes) //this takes an md5 hash and turns it into a string of numbers.
    // How can I reverse the effect of this method?
    {
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < bytes.length; i++)
        {
            byte b = bytes[i];
            sb.append(0xFF & b);
        }
        return sb.toString();
}
4

2 回答 2

2

如果不尝试所有组合并进行比较,这是不可能的。问题是它getString不会返回一个对于给定哈希值唯一的数字作为字节。

例如,如果字节的值为0216十六进制,则02变为"2"十进制字符串编码并16变为"22". 因此,您的方法将"222"作为两者的串联返回。现在,如果我们为1602then 做同样的事情,它将导致"22"and "2",并且连接仍然会导致"222". 这可能发生在字节数组中的每个字节组合上。

所以虽然结果很可能还是比较安全的hash,但是发现碰撞的几率高很多。您可以做的是返回一大组散列,其中 1 将导致匹配,但这需要大量计算;如果你想比较你最好把你的结果通过getString并比较不太安全的散列(甚至比 MD5 更不安全)。

于 2017-06-03T20:07:47.887 回答
2

我尝试编写一些代码来找到所有可能的组合,结果发现比我预期的要少得多。在这种情况下只有 2 个。而且只需很少的时间就能找到它们。

import java.util.ArrayList;
import java.util.Arrays;

public class Comb {
  static long combinations(String str, int startIdx, int numBytes, ArrayList<byte[]> combinations, byte[] combination) {
    if(startIdx >= str.length()) {
       if(numBytes == 16) {
         combinations.add(combination.clone());
         return 1;
       } else return 0;
    }
    if(numBytes > 15) return 0;
    combination[numBytes] = (byte)(str.charAt(startIdx) - '0');
    long result = combinations(str, startIdx + 1, numBytes + 1, combinations, combination);
    if(startIdx < str.length() - 1 && str.charAt(startIdx) != '0') {
      combination[numBytes] = (byte)((str.charAt(startIdx) - '0') * 10 + (str.charAt(startIdx + 1) - '0'));
      result += combinations(str, startIdx + 2, numBytes + 1, combinations, combination);
    }
    if(startIdx < str.length() - 2) {
      combination[numBytes] = (byte)((str.charAt(startIdx) - '0') * 100 + (str.charAt(startIdx + 1) - '0') * 10 + (str.charAt(startIdx + 2) - '0'));
      if(str.charAt(startIdx) == '1') result += combinations(str, startIdx + 3, numBytes + 1, combinations, combination);
      if(str.charAt(startIdx) == '2' &&
        (str.charAt(startIdx + 1) < '5' || str.charAt(startIdx + 1) == '5' && str.charAt(startIdx + 2) < '6')) {
          result += combinations(str, startIdx + 3, numBytes + 1, combinations, combination);
      }
    }
    return result;
  }

  public static void main(String[] args) {
    ArrayList<byte[]> combinations = new ArrayList<>();
    System.out.println(combinations("1518918615824625494170109603025017352201241", 0, 0, combinations, new byte[16]));
    for(byte[] c: combinations) {
      System.out.println(Arrays.toString(c));
    }
  }
}

这个的输出是:

2
[15, -67, -70, -98, -10, -2, 94, -86, 109, 60, 30, -6, -83, 52, -55, -15]
[-105, 89, -70, -98, -10, -2, 94, -86, 109, 60, 30, -6, -83, 52, -55, -15]

它找到的第二种解决方案确实是正确的。

于 2017-06-03T21:01:32.573 回答