本来我也以为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<script language="C#" runat="server">
2string qswhMD5(string str){
3/************qiushuiwuhen(2002-9-27)***************/
4byte[] b=System.Text.Encoding.Default.GetBytes(str);
5b=new System.Security.Cryptography.MD5CryptoServiceProvider().ComputeHash(b);
6string ret="";
7for(int i=0;i<b.Length;i++)
8ret+=b[i].ToString("x").PadLeft(2,'0');
9return ret;
10}
11public void encryptString(Object sender, EventArgs e)
12{
13myMD5.Text=qswhMD5(txtClear.Text);
14MD5.Text =System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(txtClear.Text, "MD5") ;
15}
16</script>
1<body onload="document.all.txtClear.select();">
2<form runat="server">
3明文:<asp:textbox id="txtClear" runat="server"></asp:textbox>
4<asp:button id="Button1" onclick="encryptString" runat="server" text="Md5摘要"></asp:button>
5
6通常用的 MD5:
7
8<asp:label id="myMD5" runat="server"></asp:label>
9
10
11HashPasswordForStoringInConfigFile中的 MD5:
12
13<asp:label id="MD5" runat="server"></asp:label>
14</form></body>