一、新建项目web项目(“WebAppDemo”)。
二、添加“index.aspx”页,写入
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>字符加密解密</title>
</head>
<body>
<form id="form1" runat="server">
<div style="text-align:center">
<asp:TextBox ID="TextBox1" runat="server" Width="549px" Height="52px" TextMode="MultiLine">需要加密的内容</asp:TextBox> <br />
<br /><br />
<center>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="加密" Width="64px" />
<asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="解密" Width="67px" />
</center>
<br /><br />
<asp:TextBox ID="TextBox2" runat="server" Height="52px" TextMode="MultiLine" Width="549px">加密后的结果</asp:TextBox>
</div>
</form>
</body>
在后置代码中写入两个按钮的事件。
protected void Button1_Click(object sender, EventArgs e)
{ //加密
this.TextBox2.Text = DESEncrypt.Encrypt(this.TextBox1.Text);
}
protected void Button2_Click(object sender, EventArgs e)
{ //解密
this.TextBox2.Text = DESEncrypt.Decrypt(this.TextBox1.Text);
}
三、添加“DESEncrypt.cs”类。
/// <summary>
/// MD5 加密和解密的算法 类
/// </summary>
public class DESEncrypt
{
// <summary>
/// 解析密文
/// </summary>
//private static string my_PassWord = "tygps";// = "1865";
private static string my_PassWord = "1865";
//===========================================以下是加密算法=========================================================
/// <summary>
/// 加密某字符串--用默认Key
/// </summary>
/// <param name="Text">原始字符串</param>
/// <returns>加密后字符串</returns>
public static string Encrypt(string Text)
{
return Encrypt(Text, my_PassWord);
}
/// <summary>
/// 加密某字符串--提供Key
/// </summary>
/// <param name="Text">原始字符串</param>
/// <param name="sKey">加密码</param>
/// <returns>加密后字符串</returns>
public static string Encrypt(string Text, string sKey)
{
try
{
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
byte[] inputByteArray;
inputByteArray = Encoding.Default.GetBytes(Text);
des.Key = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));
des.IV = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));
System.IO.MemoryStream ms = new System.IO.MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
StringBuilder ret = new StringBuilder();
foreach (byte b in ms.ToArray())
{
ret.AppendFormat("{0:X2}", b);
}
return ret.ToString();
}
catch
{
return String.Empty;
}
}
//===========================================以下是解密算法=========================================================
/// <summary>
/// 解密字符串--用默认Key
/// </summary>
/// <param name="Text">原始字符串</param>
/// <returns>解密后字符串</returns>
public static string Decrypt(string Text)
{
return Decrypt(Text, my_PassWord);
}
/// <summary>
/// 解密字符串--提供Key
/// </summary>
/// <param name="Text">原始字符串</param>
/// <param name="sKey">解密码</param>
/// <returns>解密后字符串</returns>
public static string Decrypt(string Text, string sKey)
{
try
{
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
int len;
len = Text.Length / 2;
byte[] inputByteArray = new byte[len];
int x, i;
for (x = 0; x < len; x++)
{
i = Convert.ToInt32(Text.Substring(x * 2, 2), 16);
inputByteArray[x] = (byte)i;
}
des.Key = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));
des.IV = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));
System.IO.MemoryStream ms = new System.IO.MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
return Encoding.Default.GetString(ms.ToArray());
}
catch
{
return String.Empty;
}
}
}
运行即可。效果如下: