2013-12-11 22 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