<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<data.Length ;i++)
{
sb[i*2]=HEX_TABLE[data[i]>>4];
sb[i*2+1]=HEX_TABLE[data[i] & 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>