ASP.NET Core优化MD5加密

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);
        }