All articles(网络文学目录) All Pictures(图片目录) All Softwares(软件目录)

 
ASP.NET实例:MD5简单加密_[Asp.Net教程]

Writer: delv Article type: Programming skills(编程技巧) Time: 2014/1/23 3:11:07 Browse times: 328 Comment times: 0

ASP.NET实例:MD5简单加密_[Asp.Net教程]


Head photo

Go homepage
Upload pictures
Write articles

ASP.NET实例:MD5简单加密_[Asp.Net教程]

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


/**//// MD5加密
///

/// 被加密字符串
/// 加密后的字符串
public static string MD5(string toCryString)
{
return FormsAuthentication.HashPasswordForStoringInConfigFile(toCryString, "MD5");
}

方法2:

using System.Security.Cryptography;

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 {
str2 += string.Format("{0:x}",result[i]);
}
return str2;
}

方法3:

asp.net自带了一个MD5和SHA1加密类库!
下面是调用此类库的两种加密方法:

=====================

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{
md5Str += cryString[i].ToString("X2");
}
return md5Str;
}

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





There are 0 records,
Comment:
Must be registered users to comment(必须是注册用户才能发表评论)

Disclaimer Privacy Policy About us Site Map
Copyright ©2011-
uuhomepage.com, Inc. All rights reserved.