当前位置:首页>网络学院>程序开发>ASP.NET教程>文章内容

ASP.NET实例:MD5简单加密

[ 来源:www.it55.com | 作者: | 时间:2007-07-15 | 收藏 | 推荐 ] 【

其实在.net 有一个最简单实现MD5的方法 it55.com

<summary>
/**//// MD5加密
///
</summary>
/// <param name="toCryString">被加密字符串</param>
/// <returns>加密后的字符串</returns>
public static string MD5(string toCryString)
{
return FormsAuthentication.HashPasswordForStoringInConfigFile(toCryString, "MD5");
}
http://www.it55.com/

方法2:

http://www.it55.com/

using System.Security.Cryptography; it55.com

public static string MD5(string str)
{
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;
}
www.it55.com在线教程

方法3: www.it55.com

asp.net自带了一个MD5和SHA1加密类库!
下面是调用此类库的两种加密方法:
45398 www.it55.com it55学习IT知识,享受IT生活 4dfkjn

===================== www.it55.com

public string GetMD5(string strData)
{
//使用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;
}
IT资讯之家 www.it55.com

public string GetEncrypt(string strData,string strType)
{
//使用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

http://www.it55.com/

(编辑:IT资讯之家 www.it55.com

返回顶部
 

网友评论

[以下评论为网友观点,不代表本站。请自觉遵守互联网相关政策法规,所有连带责任均有评论者自负。]
[不超过250字]

图片文章