关于网上常见的几种MD5加密的区别

(1)

using System.Security.Cryptography;

       byte[] result = Encoding.Default.GetBytes(yps);    //tbPass为输入密码的文本框
            MD5 md5 = new MD5CryptoServiceProvider();
            byte[] output = md5.ComputeHash(result);
            string str = BitConverter.ToString(output).Replace("-", "");
            if(str.Length!=32)
                System.Diagnostics.Debug.WriteLine("开始调试");
            return str;

(2)

using System.Security.Cryptography;

            string cl = yps;
            string pwd = "";
            MD5 md5 = MD5.Create();//实例化一个md5对像
            // 加密后是一个字节类型的数组,这里要注意编码UTF8/Unicode等的选择 
            byte[] s = md5.ComputeHash(Encoding.UTF8.GetBytes(cl));
            if (s.Length != 16)
                System.Diagnostics.Debug.WriteLine("开始调试");
            // 通过使用循环,将字节类型的数组转换为字符串,此字符串是常规字符格式化所得
            for (int i = 0; i < s.Length; i++)
            {
                // 将得到的字符串使用十六进制类型格式。格式后的字符是小写的字母,如果使用大写(X)则格式后的字符是大写字符 
                pwd = pwd + s[i].ToString("X");

            }
            if (pwd.Length != 32)
                System.Diagnostics.Debug.WriteLine("开始调试");
            return pwd;

(3)

using System.Web.Security;

password = FormsAuthentication.HashPasswordForStoringInConfigFile(cpassword, "MD5");

上述三种方式的MD5加密,具体原理不知了。说说微小区别

(1)与(3)的Md5值是一样的。(2)与(1)、(3)的Md5值有微小区别,原因是

pwd = pwd + s[i].ToString("X"); 该语句转化为16进制时,如果转化值为一位,比如3等,另外两种方法此时转化后的值为03,造成
最后的Md5值小于等于32位,从而造成(2)与(1)、(3)的Md5值不一定相等
所以,如果Md5加密,最好使用同一种方式。
原文地址:https://www.cnblogs.com/DayDreamEveryWhere/p/4774187.html