扫一扫
关注微信公众号

Web程序与应用程序通用Md5
2005-12-07   

<P><b>Web中:</b></P>
<P>using System.Web.Security;</P>
<P>public string md5(string baseString)
{
return FormsAuthentication.HashPasswordForStoringInConfigFile(baseString,"MD5");
}</P>
<P><b>应用程序中:</b></P>
<P>using  System.Security.Cryptography;  </P>
<P>  public static string GetMD5(Stream stream)
{
const string HEX_TABLE = "0123456789ABCDEF";
MD5 md5 = new MD5CryptoServiceProvider();
//Calculate MD5 Checksum
byte[]data = md5.ComputeHash(stream);
//convert to string
StringBuilder sb=new StringBuilder();
sb.Length =data.Length *2;
for(int i=0;i&lt;data.Length ;i++)
{
sb[i*2]=HEX_TABLE[data[i]&gt;&gt;4];
sb[i*2+1]=HEX_TABLE[data[i] &amp; 0xF];
}
return sb.ToString();
}
public static string GetMD5(string s)
{
byte[] data=ASCIIEncoding.ASCII.GetBytes(s);
MemoryStream stream=new MemoryStream(data);
//stream.Write(data,0,data.Length);
return GetMD5(stream);
}
</P>

热词搜索:

上一篇:11种反垃圾邮件软件大比拼
下一篇:使用ASP.NET加密口令

分享到: 收藏