MD5加密

 1         /// <summary>
 2         /// 计算给定字符串的MD5的值
 3         /// </summary>
 4         /// <param name="str"></param>
 5         /// <returns></returns>
 6         public static string CalcMD5(string str)
 7         {
 8             byte[] bytes = System.Text.Encoding.UTF8.GetBytes(str);
 9             return CalcMD5(bytes);
10         }
11 
12         public static string CalcMD5(byte[] bytes)
13         {
14             using (MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider())
15             {
16                 StringBuilder sb = new StringBuilder();
17                 //获取字符串的byte
18 
19                 //计算byte产生新的byte
20                 bytes = md5.ComputeHash(bytes);
21                 for (int i = 0; i < bytes.Length; i++)
22                 {
23                     sb.Append(bytes[i].ToString("x2").ToLower());
24                 }
25                 return sb.ToString();
26             }
27         }

还有一个较为简单的:直接调用C#内置的方法:

 

 1 string pwd2 = FormsAuthentication.HashPasswordForStoringInConfigFile(pwd, "md5"); 

 

posted @ 2016-10-24 15:32  墨水心2016  阅读(138)  评论(0编辑  收藏  举报