C# MD5算法

来源:互联网 发布:miui免费网络短信 编辑:程序博客网 时间:2024/05/16 17:50


首先引入命名空间:


using System.Security.Cryptography;using System.Text;

下面就可以直接写出MD5的算法:


string MD5(string strToEncrypt)    {        UTF8Encoding ue = new UTF8Encoding();        byte[] bytes = ue.GetBytes(strToEncrypt);        // encrypt bytes        MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();        byte[] hashBytes = md5.ComputeHash(bytes);        // Convert the encrypted bytes back to a string (base 16)        string hashString = "";        for (int i = 0; i < hashBytes.Length; i++)        {            hashString += System.Convert.ToString(hashBytes[i], 16).PadLeft(2, '0');        }        return hashString.PadLeft(32, '0');    }

其中把要MD5计算的字符串放入MD5 的参数中,得出的结果就是加密的结果。

0 0
原创粉丝点击