MD5是咱们经常使用的一种加密方式,可是有朋友和我说C#自带的MD5方法碰撞阻力过低,担忧安全问题json
而后我这里开源一下我平常使用的优化后的MD5加密方法安全
代码中先建立出MD5对象后对字符串先进行MD5加密,对加密出的内容再次进行按位运算以增长MD5的安全性。优化
public static string byte2hex(byte[] abyte0) { StringBuilder sb = new StringBuilder(); for (int i = 0; i<abyte0.Length; i++) { if ((abyte0[i] & 0xff) < 16) { sb.Append("0"); } sb.Append(Convert.ToString((long)abyte0[i] & (long)255, 16)); } return sb.ToString(); } public static string MD5Encrypt(string json) { MD5 md5Hash = MD5.Create(); // Convert the input string to a byte array and compute the hash. byte[] data = md5Hash.ComputeHash(Encoding.UTF8.GetBytes(json)); return byte2hex(data); }