md5加密原文地址:http://lintex.blog.sohu.com/4456438.html

来源:互联网 发布:rds恢复到自建数据库 编辑:程序博客网 时间:2024/05/17 06:12

原文地址:http://lintex.blog.sohu.com/4456438.html

网上很多代码都是这样写的,是要导入“System.Web.Security”命名空间,导入后,仍然不行。我仔细看了看MSDN中的“System.Web.Security”命名空间,始终找不到“CookieAuthentication”类。不知道为什么,找了很多代码都是这样写的,这里换成“FormsAuthentication”类就对了。

这里总结两种使用MD5加密的方法:

第一种方法,比较简单,就是上面这种方法,导入“System.Web.Security”命名空间,再使用FormsAuthentication.HashPasswordForStoringInConfigFile("需要加密的字符串", "MD5"),就行了。“MD5”换成“SHA1”可以使用SHA1算法。

第二种方法,需要用到“System.Security.Cryptography”和“System.Text”命名空间。源代码如下:


    MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();    byte[] encryptedBytes = md5.ComputeHash(System.Text.Encoding.ASCII.GetBytes("需要加密的字符串"));    StringBuilder sb = new StringBuilder();    for (int i = 0; i < encryptedBytes.Length; i++)    {        sb.AppendFormat("{0:x2}", encryptedBytes[i]);    }    string 结果 = sb.ToString();关于hash、MD5和SHA1,找了一篇文章,写得不错。http://lintex.blog.sohu.com/4457169.html

 

原创粉丝点击