怎样c# java md5值保持一致
c#方式
/// <summary>
/// 获得字符串md5.
/// </summary>
/// <param name="myString"></param>
/// <returns></returns>
public static string GetMD5(string myString)
{
MD5 md5 = new MD5CryptoServiceProvider();
byte[] fromData = System.Text.Encoding.UTF8.GetBytes(myString);
byte[] targetData = md5.ComputeHash(fromData);
string byte2String = null;
for (int i = 0; i < targetData.Length; i++)
{
byte2String += targetData[i].ToString("x");
}
return byte2String;
}
java方式
public static String md5(String url) throws NoSuchAlgorithmException, UnsupportedEncodingException {
MessageDigest md = MessageDigest.getInstance("MD5");
md.update(url.getBytes("utf-8"));
byte[] result = md.digest();
StringBuffer sb = new StringBuffer();
for (int i = 0; i < result.length; i++) {
int val = result[i] & 0xff;
sb.append(Integer.toHexString(val));
}
return sb.toString();
}
上边两种方式c# javamd5值一样。
记录一下