纠正:HashPasswordForStoringInConfigFile中的Md5算法并非常用的Md5算法

本来我也以为System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile中的MD5和常用的一样

可今天一试,结果有很大不同,
比如test,HashPasswordForStoringInConfigFile编码成
C8059E2EC7419F590E79D7F1B774BFE6
而应该是098f6bcd4621d373cade4e832627b4f6

而且不同的机器不同的结果,有些结果正确
一看MSDN的解释,原来是
Given a password and a string identifying the hash type, this routine produces a hash password suitable for storing in a configuration file.

为了和以前的代码兼容和平台兼容,只好自己重新写了MD5的算法,利用System.Security.Cryptography.MD5CryptoServiceProvider
代码如下,大家执行一下就知道了,我就不多说了。

 

  1. <%@ Page Language="C#" %>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <script runat="server">
  4.     string MD5Crypto(string str)
  5.     {
  6.         byte[] b = System.Text.Encoding.Default.GetBytes(str);
  7.         b = new System.Security.Cryptography.MD5CryptoServiceProvider().ComputeHash(b);
  8.         string ret = "";
  9.         for (int i = 0; i < b.Length; i++)
  10.             ret += b[i].ToString("x").PadLeft(2, '0');
  11.         return ret;
  12.     }
  13.     public void encryptString(Object sender, EventArgs e)
  14.     {
  15.         myMD5.Text = MD5Crypto(txtClear.Text);
  16.         MD5.Text = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(txtClear.Text, "MD5");
  17.     } 
  18. </script>
  19. <html xmlns="http://www.w3.org/1999/xhtml" >
  20. <head runat="server">
  21.     <title>无标题页</title>
  22. </head>
  23. <body>
  24.     <form id="form1" runat="server">
  25.         <div>
  26.             明文:<asp:TextBox ID="txtClear" runat="server" />
  27.             <asp:Button runat="server" Text="Md5摘要" OnClick="encryptString" ID="Button1" />
  28.             <br />
  29.             通常用的 MD5:
  30.             <br />
  31.             <asp:Label ID="myMD5" runat="server" />
  32.             <br />
  33.             <br />
  34.             HashPasswordForStoringInConfigFile中的 MD5:
  35.             <br />
  36.             <asp:Label ID="MD5" runat="server" />
  37.         </div>
  38.     </form>
  39. </body>
  40. </html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

冷月宫主

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值