下面的代码实现了一个可逆加密的方法。可以用于对Cookie,QueryString等加密处理。
前台EncryptString.aspx
后台EncryptString.aspx.cs

using ...System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Text;
using System.Security.Cryptography;
namespace eMeng.Exam 
...{ 
/**//// <summary>
/// </summary>
public class EncryptString : System.Web.UI.Page 
...{
protected System.Web.UI.WebControls.TextBox TextBox1;
protected System.Web.UI.WebControls.RadioButtonList RadioButtonList1;
protected System.Web.UI.WebControls.TextBox TextBox2;
protected System.Web.UI.WebControls.TextBox Textbox3;
protected System.Web.UI.HtmlControls.HtmlForm Form1;
private void Page_Load(object sender, System.EventArgs e) 
...{
// 在此处放置用户代码以初始化页面
if(!this.IsPostBack) 
...{
ArrayList MyList = new ArrayList();
MyList.Add("加密");
MyList.Add("解密");
RadioButtonList1.DataSource = MyList;
RadioButtonList1.DataBind();
}
}

Web 窗体设计器生成的代码#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e) 
...{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
} 
/**//// <summary>
/// </summary>
private void InitializeComponent() 
...{
this.RadioButtonList1.SelectedIndexChanged += new System.EventHandler(this.RadioButtonList1_SelectedIndexChanged);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void RadioButtonList1_SelectedIndexChanged(object sender, System.EventArgs e) 
...{
if( RadioButtonList1.SelectedIndex == 0)
TextBox2.Text = EncryptText(TextBox1.Text);
else
Textbox3.Text = DecryptText(TextBox2.Text);
}
// 加密
public string EncryptText(String strText) 
...{
return Encrypt(strText, "&%#@?,:*");
}
//'解密
public String DecryptText(String strText) 
...{
return Decrypt(strText, "&%#@?,:*");
}
//'加密函数
private String Encrypt(String strText, String strEncrKey) 
...{
Byte[] byKey = ...{};
Byte[] IV = ...{0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF};
try 
...{
byKey = System.Text.Encoding.UTF8.GetBytes(strEncrKey.Substring(0, 8));
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
Byte[] inputByteArray = Encoding.UTF8.GetBytes(strText);
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(byKey, IV), CryptoStreamMode.Write);
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
return Convert.ToBase64String(ms.ToArray());
}
catch(Exception ex) 
...{
return ex.Message;
}
}
//'解密函数
private String Decrypt(String strText, String sDecrKey) 
...{
Byte[] byKey = ...{};
Byte[] IV = ...{0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF};
Byte[] inputByteArray = new byte[strText.Length];
try 
...{
byKey = System.Text.Encoding.UTF8.GetBytes(sDecrKey.Substring(0, 8));
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
inputByteArray = Convert.FromBase64String(strText);
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(byKey, IV), CryptoStreamMode.Write);
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
System.Text.Encoding encoding = System.Text.Encoding.UTF8;
return encoding.GetString(ms.ToArray());
}
catch(Exception ex) 
...{
return ex.Message;
}
}
}
}