asp MD5加密采用动网论坛的加密算法,具体参考动网论坛 inc/md5.asp
asp.net的MD5加密算法:
public string MD5_1(string str)
...{
return System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(this.oldTxt.Text.Trim(), "MD5").ToLower();
}
public string MD5_2(string str)
...{
System.Security.Cryptography.MD5CryptoServiceProvider hashmd5;
hashmd5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
return BitConverter.ToString(hashmd5.ComputeHash(System.Text.Encoding.Default.GetBytes(str))).Replace("-","").ToLower();
}
asp加密结果:
admin - MD5加密的结果(32位):21232f297a57a5a743894a0e4a801fc3
中国 - 加密后结果(32):cf0832dedf7457bbcbfa00bbd87b300a
asp.net 加密结果:
(1)加密字符串-admin,加密结果如下:
21232f297a57a5a743894a0e4a801fc3 (MD5_1)
21232f297a57a5a743894a0e4a801fc3 (MD5_2)
(2)加密字符串-中国,加密结果:
c13dceabcb143acd6c9298265d618a9f (MD5_1)
cf0832dedf7457bbcbfa00bbd87b300a (MD5_2)
asp与asp.net MD5加密结果比较总结
(1)在加密英文字符时,aspMD5加密与asp.netMD5加密结果一样。
(2)在加密中文字符或者其它双字节字符时,sp.net加密结果与字符编码有关。
更详细测试:
前台页面

<%...@ Page Language="C#" AutoEventWireup="true" CodeFile="md5.aspx.cs" Inherits="md5" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>MD5测试</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="Label2" runat="server" Text="原始字符串"></asp:Label>
<asp:TextBox ID="oldTxt" runat="server" Width="287px"></asp:TextBox><br />
<br />
ASP MD5加密结果(动网论坛MD5加密32位)<br />
admin 加密后结果: 21232f297a57a5a743894a0e4a801fc3<br />
admin123 加密后结果: 0192023a7bbd73250516f069df18b500<br />
admin中国 加密后结果:b15d57a81157056fe4e24d89da136234<br />
中国 加密后结果: cf0832dedf7457bbcbfa00bbd87b300a<br />
<br />
<br />
ASP.NET MD5加密结果<br />
<asp:ListBox ID="ListBox1" runat="server" Height="145px" Width="306px" ></asp:ListBox><br />
<br />
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="加密1" />
<asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="加密2" /> <asp:Button
ID="Button3" runat="server" OnClick="Button3_Click" Text="加密3" />
<asp:Button ID="Button4" runat="server" OnClick="Button4_Click" Text="加密4" />
<asp:Button ID="Button5" runat="server" OnClick="Button5_Click" Text="清除结果" /></div>
</form>
</body>
</html>后台代码
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class md5 : System.Web.UI.Page
...{
protected void Page_Load(object sender, EventArgs e)
...{
}
public string MD5_1(string str)
...{
return System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(this.oldTxt.Text.Trim(), "MD5").ToLower();
}
public string MD5_2(string str)
...{
System.Security.Cryptography.MD5CryptoServiceProvider hashmd5;
hashmd5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
return BitConverter.ToString(hashmd5.ComputeHash(System.Text.Encoding.Default.GetBytes(str))).Replace("-","").ToLower();
}
public string MD5_3(string str)
...{
System.Security.Cryptography.MD5CryptoServiceProvider hashmd5;
hashmd5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
return BitConverter.ToString(hashmd5.ComputeHash(System.Text.Encoding.Unicode.GetBytes(str))).Replace("-", "").ToLower();
}
public string MD5_4(string str)
...{
System.Security.Cryptography.MD5CryptoServiceProvider hashmd5;
hashmd5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
return BitConverter.ToString(hashmd5.ComputeHash(System.Text.Encoding.UTF8.GetBytes(str))).Replace("-", "").ToLower();
}
protected void Button1_Click(object sender, EventArgs e)
...{
this.ListBox1.Items.Add(MD5_1(oldTxt.Text.Trim()));
}


protected void Button2_Click(object sender, EventArgs e)
...{
this.ListBox1.Items.Add(MD5_2(this.oldTxt.Text.Trim()));
}
protected void Button3_Click(object sender, EventArgs e)
...{
this.ListBox1.Items.Add(MD5_3(this.oldTxt.Text.Trim()));
}
protected void Button4_Click(object sender, EventArgs e)
...{
this.ListBox1.Items.Add(MD5_4(this.oldTxt.Text.Trim()));
}
protected void Button5_Click(object sender, EventArgs e)
...{
this.ListBox1.Items.Clear();
}
}
本文对比了ASP和ASP.NET中MD5加密算法的实现及加密结果,特别是在处理英文和中文字符时的不同表现,并提供了详细的代码示例。
201





