MD5普通加密方法(这种方法一般不做为用户密码的加密)
<%@ Page Language="C#" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <script runat="server"> protected void Page_Load(object sender, EventArgs e) { //获取要加密的字段,并转化为Byte[]数组 byte[] data = System.Text.Encoding.Unicode.GetBytes(source.Text.ToCharArray()); //建立加密服务 System.Security.Cryptography.MD5 md5 = new System.Security.Cryptography.MD5CryptoServiceProvider(); //加密Byte[]数组 byte[] result = md5.ComputeHash(data); //将加密后的数组转化为字段 string sResult = System.Text.Encoding.Unicode.GetString(result); //显示出来 pass_1.Text = sResult.ToString() ; } </script> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title>MD5加密 www.itstudy.cn</title> </head> <body> <form id="form1" runat="server"> MD5普通加密:<asp:label id="pass_1" runat="server"></asp:label> <br /> <asp:textbox ID="source" runat="server" Text="test" AutoPostBack="true" /> (回车) </form> </body> </html> |
MD5密码加密(这是常用的方法)
页面:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> <!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"> |
后台代码:
using System; public partial class _Default : System.Web.UI.Page
if (code == 32) //32位加密 return "00000000000000000000000000000000"; |
注:除了采用单一的加密方法,还可以使用多个加密方法,取多个加密后的某几位组成一个新的加密结果。