asp.net与C#应用程序的md5加密怎么不一样呢?

来源:百度知道 编辑:UC知道 时间:2024/05/11 14:00:25
asp.net C#网站程序的md5加密函数:
public string md5(string str)
{
string md5str = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(str, "md5");
return md5str;
}

C#应用程序的md5加密函数:
public string md5(string passWord)
{
Byte[] clearBytes = new UnicodeEncoding().GetBytes(passWord);
Byte[] hashedBytes = ((HashAlgorithm)CryptoConfig.CreateFromName("MD5")).ComputeHash(clearBytes);
return BitConverter.ToString(hashedBytes).Replace("-", "");
}
我给出一个字符串:admin,使用两种方法加密结果如下:
asp.net C#网站程序的md5结果为:21232F297A57A5A743894A0E4A801FC3
C#应用程序的md5结果为: 19A2854144B63A8F7617A6F225019B12

是字符的编码不一样,把下面那个方法改成这样就行了
public string md5(string passWord)
{
Byte[] clearBytes = Encoding.Default.GetBytes(passWord);
Byte[] hashedBytes = ((HashAlgorithm)CryptoConfig.CreateFromName("MD5")).ComputeHash(clearBytes);
return BitConverter.ToString(hashedBytes).Replace("-", "");
}

你代码写啰嗦了~ 留个邮箱 我把我的代码给你~