本实例介绍如何使用在ASP.NET应用程序中的加密Cookie的方法。实例中采用的加密方法为DES和TripleDES两种方法,程序中均实现了 DES和TripleDES的加密和解密方法,同时还设置了创建处理Cookie的类CookieEncrypt。
1.创建新ASP.NET应用程序
在Visual Studio .NET 2003集成开发环境中创建新的ASP.NET Web应用程序,命名为Example_12_6。
2.创建加密Cookie的类EncryptString
在应用程序Example_12_6中添加类文件EncryptString.cs,该文件实现使用DES方法加密、解密数据和使用TripleDES方法加密、解密数据的方法,还定义了用来加密、解密的密钥Key以及IV。类文件 EncryptString.cs的程序代码如下:
以下为引用的内容:
public class EncryptString
{
private static byte[] Key64 = {42, 16, 93, 156, 78, 4, 218, 32};
private static byte[] IV64 = {55, 103, 246, 79, 36, 99, 167, 3};
private static byte[] Key192 = {42, 16, 93, 156, 78, 4, 218, 32,15, 167,
44,80, 26, 250, 155, 112,2, 94, 11, 204, 119, 35, 184, 197};
private static byte[] IV192 = {55, 103, 246, 79, 36, 99, 167, 3,42,
5, 62,83, 184, 7, 209, 13,145, 23, 200, 58, 173, 10, 121, 222};
public static String Encrypt(String valueString)
{
if(valueString != "")
{ //定义DES的Provider
DESCryptoServiceProvider desprovider =
new DESCryptoServiceProvider();
//定义内存流
MemoryStream memoryStream = new MemoryStream();
//定义加密流
CryptoStream cryptoStream = new CryptoStream(memoryStream,
desprovider.CreateEncryptor(Key64,IV64),
CryptoStreamMode.Write);
//定义写IO流
StreamWriter writerStream = new StreamWriter(cryptoStream);
//写入加密后的字符流
writerStream.Write(valueString);
writerStream.Flush();
cryptoStream.FlushFinalBlock();
memoryStream.Flush();
//返回加密后的字符串
return(Convert.ToBase64String(memoryStream.GetBuffer(),0,
(int)memoryStream.Length));
}
return(null);
}
public static String Decrypt(String valueString)
{
if(valueString != "")
{ //定义DES的Provider
DESCryptoServiceProvider desprovider =
new DESCryptoServiceProvider();
//转换解密的字符串为二进制
byte[] buffer = Convert.FromBase64String(valueString);
//定义内存流
MemoryStream memoryStream = new MemoryStream();
//定义加密流
CryptoStream cryptoStream = new CryptoStream(memoryStream,
desprovider.CreateEncryptor(Key64,IV64),
CryptoStreamMode.Read);
//定义读IO流
StreamReader readerStream = new StreamReader(cryptoStream);
//返回解密后的字符串
return(readerStream.ReadToEnd());
}
return(null);
}
public static String EncryptTripleDES(String valueString)
{
if(valueString != "")
{ //定义TripleDES的Provider
TripleDESCryptoServiceProvider triprovider =
new TripleDESCryptoServiceProvider();
//定义内存流
MemoryStream memoryStream = new MemoryStream();
//定义加密流
CryptoStream cryptoStream = new CryptoStream(memoryStream,
triprovider.CreateEncryptor(Key192,IV192),
CryptoStreamMode.Write);
//定义写IO流
StreamWriter writerStream = new StreamWriter(cryptoStream);
//写入加密后的字符流
writerStream.Write(valueString);
writerStream.Flush();
cryptoStream.FlushFinalBlock();
memoryStream.Flush();
//返回加密后的字符串
return(Convert.ToBase64String(memoryStream.GetBuffer(),0,
(int)memoryStream.Length));
}
return(null);
}
public static String DecryptTripleDES(String valueString)
{
if(valueString != "")
{ //定义TripleDES的Provider
TripleDESCryptoServiceProvider triprovider =
new TripleDESCryptoServiceProvider();
//转换解密的字符串为二进制
byte[] buffer = Convert.FromBase64String(valueString);
//定义内存流
MemoryStream memoryStream = new MemoryStream();
//定义加密流
CryptoStream cryptoStream = new CryptoStream(memoryStream,
triprovider.CreateEncryptor(Key64,IV64),
CryptoStreamMode.Read);
//定义读IO流
StreamReader readerStream = new StreamReader(cryptoStream);
//返回解密后的字符串
return(readerStream.ReadToEnd());
}
return(null);
}
}