ASP.NET Core優化MD5加密
阿新 • • 發佈:2018-11-26
MD5是我們常用的一種加密方式,但是有朋友和我說C#自帶的MD5方法碰撞阻力太低,擔心安全問題
然後我這裡開源一下我日常使用的優化後的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); }