C#与JAVA采用MD5加密结果不一致解决方法

2018-10-23  本文已影响0人  安易学车

JAVA代码:

public static String MD5Encode(String origin) {

StringBuffer sb = new StringBuffer();

try {

MessageDigest md = MessageDigest.getInstance("MD5");

md.update(origin.getBytes("utf8"));

byte[] result = md.digest();

for (int i = 0; i < result.length; i++) {

//int val = result[i] & 0xff;

//sb.append(Integer.toHexString(val));

int val = (result[i] & 0x000000ff) | 0xffffff00;

sb.append(Integer.toHexString(val).substring(6));

}

} catch (Exception e) {

e.printStackTrace();

}

return sb.toString();

}

C#代码:

//str 需要加密的字符串 

public static String MD5(String str) 

    byte[] result = ((HashAlgorithm)CryptoConfig.CreateFromName("MD5")).ComputeHash(Encoding.UTF8.GetBytes(str)); 

    StringBuilder output = new StringBuilder(16); 

    for (int i = 0; i < result.Length; i++) 

    { 

        // convert from hexa-decimal to character 

        output.Append((result[i]).ToString("x2", System.Globalization.CultureInfo.InvariantCulture)); 

    } 

    return output.ToString(); 

}

上一篇下一篇

猜你喜欢

热点阅读