簡體   English   中英

如何在Java中使用MD5進行哈希計算

[英]How to do compute hash using md5 in java

我有一個將md5哈希碼轉換為字節碼的問題。 使用C#,服務端從哈希碼開始,他們正在生成一些字節碼,但我無法獲得相同的字節碼。 請幫助我如何獲取相同的字節碼。

C#:

 MD5CryptoServiceProvider md5Hasher = new MD5CryptoServiceProvider();
 byte[] hashedBytes = null;
 UTF8Encoding encoder = new UTF8Encoding();
 hashedBytes = md5Hasher.ComputeHash(encoder.GetBytes(Loginentities.Password));

encoder.GetBytes(Loginentities.Password)

為此行正在獲取字節,例如[19、15、101、15、4、104、105、14、7、1]

hashedBytes正在獲取這些字節[149,229,240,182,152,142,199,3,232,50,23,47,112,206,125,199]

哈希碼: 95e5f0b69asdda3e832172f70ce7dc7

Java:

String str1 ="ramprasad";
byte[] bytes1 = str1.getBytes();
System.out.println("uno shriram is "+Arrays.toString(bytes1));

為bytes1正在獲取[117、110、111、115、104、114、105、114、97、109]

     try {
                    MessageDigest md = MessageDigest.getInstance("MD5");
                    byte[] array = md.digest(bytes);
                    StringBuffer sb = new StringBuffer();
                    for (int i = 0; i < array.length; ++i) {
                      sb.append(Integer.toHexString((array[i] & 0xFF) | 0x100).substring(1,3));
                   }
                    System.out.println( "0X"+sb.toString());
OUTPUT:0X95e5f0b21398asdas03e832172f70ce7dc7
                   String x= "0x"+sb.toString();
                    System.out.println(x.getBytes() );
OUTPUT:[B@1174b07
                    System.out.println("uno shriram is "+Arrays.toString(x.getBytes()));
OUTPUT:uno shriram is [48, 120, 57, 53, 101, 53, 102, 48, 98, 54, 57, 56, 56, 101, 99, 55, 48, 51, 101, 56, 51, 50, 49, 55, 50, 102, 55, 48, 99, 101, 55, 100, 99, 55]
                    try
                    {
                    byte[] bytesasd = x.getBytes("UTF-8");
                    MessageDigest m = MessageDigest.getInstance("MD5");

                    byte[] hashedbyte = m.digest(bytesasd);

                    System.out.println(hashedbyte);
                    }
                    catch(Exception e)
                    {
                        e.printStackTrace();
                    }

                } catch (java.security.NoSuchAlgorithmException e) {
                }
            try 
            {
                String doc2 = new String(bytes, "UTF-8");
                System.out.println(""+doc2);
            } catch (UnsupportedEncodingException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

如何在Java中獲取精確的哈希字節。 請幫助我如何獲得它

不要忘記調用getBytes("UTF-8")否則您的編碼可能已關閉。 就像是

String str1 = "unoshriram";
String outHash = "95e5f0b6988ec703e832172f70ce7dc7";
try {
    MessageDigest md = MessageDigest.getInstance("MD5");
    byte[] array = md.digest(str1.getBytes("UTF-8"));
    StringBuilder sb = new StringBuilder();
    for (byte b : array) {
        sb.append(String.format("%02X", b));
    }
    System.out.println(sb.toString());
    System.out.println(sb.toString().equalsIgnoreCase(outHash));
} catch (Exception e) {
    e.printStackTrace();
}

輸出是

95E5F0B6988EC703E832172F70CE7DC7
true

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM