如何在java中使用md5进行计算哈希

时间:2014-12-23 05:16:15

标签: java encryption hash md5 hashcode

我有将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,10,10,105,14,7,1]

hashedBytes得到这些字节 [149,229,240,182,152,142,199,3,232,50,23,47,112,206,125,199]

哈希码: 95e5f0b69asdda3e832172f70ce7dc7

爪哇:

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

for bytes1 am [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中获取精确的散列字节。请帮我解决问题

1 个答案:

答案 0 :(得分:2)

请勿忘记致电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