2013-12-11 16 views
0

我试图将字符串转换为它的MD5表示与此代码:转换字符串的MD5给人增添位数

public static void main(String[] args) throws NoSuchAlgorithmException { 
     String s = "oshai"; 
     MessageDigest m = MessageDigest.getInstance("MD5"); 
     m.update(s.getBytes(),0,s.length()); 
     String md5 = new BigInteger(1,m.digest()).toString(16); 
     System.out.println(md5.length()); 
} 

返回的字符串添加的数字(31号,所以它可以是一个十六进制数)。我究竟做错了什么?

+1

注:它可能是31个数字,因为它不填充。如果散列是一个小数字,它不会有前导零。下面的答案都正确填充十六进制数字。 – slipperyseal

+1

如果第三方库是公平游戏,那么使用[Guava](https://code.google.com/p/guava-libraries/)就会更简单(也更正确):'Hashing.md5()。hashString (s,Charsets.UTF_8).toString()'返回UTF-8编码字符串正确的十六进制编码的MD5散列。 –

+0

谢谢,我总是喜欢别人写代码,番石榴是我的最爱之一:-) – oshai

回答

2

你不想使用BigInteger。尝试更传统的方法toHexString ..

public static void main(String[] args) throws NoSuchAlgorithmException { 
     String s = "oshai"; 
     MessageDigest m = MessageDigest.getInstance("MD5"); 
     m.update(s.getBytes(),0,s.length()); 
     String string = toHexString(m.digest()); 
     System.out.println(string); 
} 

public static String toHexString(byte[] bytes) { 
    StringBuilder sb = new StringBuilder(); 
    for(byte b : bytes) { 
     sb.append(String.format("%02x", b)); 
    } 
    return sb.toString(); 
} 
+0

是因为零填充? – oshai

+0

我刚刚回答了上面的问题。我会猜测,是的:) – slipperyseal

2

此方法适用于确定:

private String hashWithMD5(String text) throws UnsupportedEncodingException, NoSuchAlgorithmException { 
    MessageDigest messageDigest = MessageDigest.getInstance("MD5"); 
    byte[] digest = messageDigest.digest(text.getBytes("UTF-8")); 

    StringBuilder sb = new StringBuilder(); 
    for (int i = 0; i < digest.length; i++) { 
     sb.append(Integer.toString((digest[i] & 0xff) + 0x100, 16).substring(1)); 
    } 

    return sb.toString(); 
} 
+0

我检查了它,它似乎像填充零的数字。你能说出原因吗?这是正确的做法吗? – oshai

+0

是的,对于散列值等值的填充值 – slipperyseal

-2

使用这种方法:

公共静态字符串的MD5(字符串输入){

String md5 = null; 

    if (null == input) 
     return null; 

    try { 

     // Create MessageDigest object for MD5 
     MessageDigest digest = MessageDigest.getInstance("MD5"); 

     // Update input string in message digest 
     digest.update(input.getBytes(), 0, input.length()); 

     // Converts message digest value in base 16 (hex) 
     md5 = new BigInteger(1, digest.digest()).toString(16); 

    } catch (NoSuchAlgorithmException e) { 

     e.printStackTrace(); 
    } 
    return md5; 
} 
+0

与我的方法有什么不同? – oshai

+0

这种方法的工作原理,即差异; D – Bourkadi

+0

为什么减去?谢谢 – Bourkadi

0

我所面临的误差与在左侧缺失“00”,而将字符串转换到加密的格式。

通常情况下,通过使用通用md5方法,您不会在应用程序中发现错误。

所以,请用字符串“sandeep”(我已经使用它,因为它在左边有一个“00”)测试你的应用程序。

这个问题搞砸了我的时间,最后我从链接中找到了以下解决方案。

“我曾与MD5串中的错误与00在莱夫特赛德,即一个字符串‘桑迪普’转换为‘DCF16D903E5890AABA465B0B1BA51F’比实际的“00DCF16D903E5890AABA465B0B1BA51F

我结束了这种方法,该工作很酷在我的应用程序中。“

public static String md5(String inputString) { 
    try { 
     // Create MD5 Hash 
     MessageDigest msgDigest = java.security.MessageDigest.getInstance("MD5"); 
     msgDigest.update(inputString.getBytes()); 
     byte msgDigestBytes[] = msgDigest.digest(); 

     // Create Hex String 
     StringBuffer hexString = new StringBuffer(); 
     for (int i = 0; i < msgDigestBytes.length; i++) { 
      String h = Integer.toHexString(0xFF & msgDigestBytes[i]); 
      while (h.length() < 2) 
       h = "0" + h; 
      hexString.append(h); 
     } 
     return hexString.toString(); 

    } catch (NoSuchAlgorithmException e) { 
     e.printStackTrace(); 
    } 
    return ""; 
} 

编号:http://www.coderexception.com/CbHuB1uHPWxXUQXi/converting-string-to-md5-gives-add-number-of-digits