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

王朝c#·作者佚名  2006-12-17
窄屏简体版  字體: |||超大  

本来我也以为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

代码如下,大家执行一下就知道了,我就不多说了。

<script language="C#" runat="server">

string qswhMD5(string str){

/************qiushuiwuhen(2002-9-27)***************/

byte[] b=System.Text.Encoding.Default.GetBytes(str);

b=new System.Security.Cryptography.MD5CryptoServiceProvider().ComputeHash(b);

string ret="";

for(int i=0;i<b.Length;i++)

ret+=b[i].ToString("x").PadLeft(2,'0');

return ret;

}

public void encryptString(Object sender, EventArgs e)

{

myMD5.Text=qswhMD5(txtClear.Text);

MD5.Text =System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(txtClear.Text, "MD5") ;

}

</script>

<body onload=document.all.txtClear.select();>

<form runat="server">

明文:<asp:Textbox id="txtClear" runat="server" />

<asp:Button runat="server" text="Md5摘要" onClick="encryptString" ID="Button1" />

<br/>通常用的 MD5:

<br/><asp:label id="myMD5" runat="server" /> <br/>

<br/>HashPasswordForStoringInConfigFile中的 MD5:

<br/><asp:label id="MD5" runat="server" />

</form>

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有  導航