其实在.net 有一个最简单实现MD5的方法 <summary> 方法2: using System.Security.Cryptography; public static string MD5(string str) 方法3: asp.net自带了一个MD5和SHA1加密类库! ===================== public string GetMD5(string strData) public string GetEncrypt(string strData,string strType)
\n
\n
/**//// MD5加密
///
</summary>
/// <param name=”toCryString”>被加密字符串</param>
/// <returns>加密后的字符串</returns>
public static string MD5(string toCryString)
{
return FormsAuthentication.HashPasswordForStoringInConfigFile(toCryString, “MD5″);
}
\n
\n
\n
{
MD5 md5 = new MD5CryptoServiceProvider();
byte[] result = md5.ComputeHash(System.Text.Encoding.Default.GetBytes(str));
string str2 = “”;
for(int i=0;i<result.Length;i++)
{
str2 += string.Format(“{0:x}”,result[i]);
}
return str2;
}
\n
\n
下面是调用此类库的两种加密方法:
\n
\n
{
//使用MD5加密方法:
System.Security.Cryptography.MD5 md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
byte[] md5Bytes = System.Text.Encoding.Default.GetBytes(strData);
byte[] cryString = md5.ComputeHash(md5Bytes);
string md5Str = string.Empty;
for (int i=0;i<cryString.Length;i++)
{
md5Str += cryString[i].ToString(“X2″);
}
return md5Str;
}
\n
{
//使用MD5或SHA1的加密方法:
string strCryData = string.Empty;
if (strType.ToUpper() == “SHA1″)
{
strCryData = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(strData,”SHA1″);
}
else if (strType.ToUpper() == “MD5″)
{
strCryData = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(strData,”MD5″);
}
return strCryData;
}
来源:csdn