using System;
using System.IO;
using System.Text;
using System.Diagnostics;
using System.Web.Security;
using System.Security;
using System.Security.Cryptography;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace ASPNET3DBBook
{
public class EncryptString
{ ///定义64位和192位的Kev和IV
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};
/// <summary>
/// 加密字符串
/// </summary>
/// <param name="valueString"></param>
/// <returns></returns>
public static String Encrypt(String valueString)
{
if(valueString != "")
{ ///创建加密的provider
DESCryptoServiceProvider desprovider = new DESCryptoServiceProvider();
///创建加密的流
MemoryStream memoryStream = new MemoryStream();
CryptoStream cryptoStream = new CryptoStream(memoryStream,desprovider.CreateEncryptor
(Key64,IV64),CryptoStreamMode.Write);
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);
}
/// <summary>
/// 解密字符串
/// </summary>
/// <param name="valueString"></param>
/// <returns></returns>
public static String Decrypt(String valueString)
{
if(valueString != "")
{ ///创建解密的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);
StreamReader readerStream = new StreamReader(cryptoStream);
return(readerStream.ReadToEnd());
}
return(null);
}
/// <summary>
/// DES加密方法
/// </summary>
/// <param name="valueString"></param>
/// <returns></returns>
public static String EncryptTripleDES(String valueString)
{
if(valueString != "")
{
///创建加密的provider
TripleDESCryptoServiceProvider triprovider = new TripleDESCryptoServiceProvider();
///创建加密的流
MemoryStream memoryStream = new MemoryStream();
CryptoStream cryptoStream = new CryptoStream(memoryStream,triprovider.CreateEncryptor
(Key192,IV192),CryptoStreamMode.Write);
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);
}
/// <summary>
/// DES解密方法
/// </summary>
/// <param name="valueString"></param>
/// <returns></returns>
public static String DecryptTripleDES(String valueString)
{
if(valueString != "")
{ ///创建加密的provider
TripleDESCryptoServiceProvider triprovider = new TripleDESCryptoServiceProvider();
///创建加密的流
byte[] buffer = Convert.FromBase64String(valueString);
MemoryStream memoryStream = new MemoryStream();
CryptoStream cryptoStream = new CryptoStream(memoryStream,triprovider.CreateEncryptor
(Key192,IV192),CryptoStreamMode.Read);
StreamReader readerStream = new StreamReader(cryptoStream);
return(readerStream.ReadToEnd());
}
return(null);
}
}
public class CookieEncrypt
{
/// <summary>
/// 设置Cookie
/// </summary>
/// <param name="cookie"></param>
public static void SetCookie(HttpCookie cookie)
{
HttpContext.Current.Response.Cookies.Set(cookie);
}
/// <summary>
/// 设置Cookie
/// </summary>
/// <param name="key"></param>
/// <param name="valueString"></param>
public static void SetCookie(String key,String valueString)
{ ///获取关键字和值
key = HttpContext.Current.Server.UrlEncode(key);
valueString = HttpContext.Current.Server.UrlEncode(valueString);
///设置Cookie
HttpCookie cookie = new HttpCookie(key,valueString);
SetCookie(cookie);
}
/// <summary>
/// 设置Cookie
/// </summary>
/// <param name="key"></param>
/// <param name="valueString"></param>
/// <param name="expires"></param>
public static void SetCookie(String key,String valueString,DateTime expires)
{ ///获取关键字和值
key = HttpContext.Current.Server.UrlEncode(key);
valueString = HttpContext.Current.Server.UrlEncode(valueString);
///设置Cookie
HttpCookie cookie = new HttpCookie(key,valueString);
cookie.Expires = expires;
SetCookie(cookie);
}
/// <summary>
/// 设置使用DES方法加密之后的Cookie
/// </summary>
/// <param name="key"></param>
/// <param name="valueString"></param>
public static void SetTripleDESEncryptedCookie(String key,String valueString)
{
///获取关键字和值
key = EncryptString.EncryptTripleDES(key);
valueString = EncryptString.EncryptTripleDES(valueString);
///设置Cookie
SetCookie(key,valueString);
}
/// <summary>
/// 设置使用DES方法加密之后的Cookie
/// </summary>
/// <param name="key"></param>
/// <param name="valueString"></param>
/// <param name="expires"></param>
public static void SetTripleDESEncryptedCookie(String key,String valueString,DateTime expires)
{
///获取关键字和值
key = EncryptString.EncryptTripleDES(key);
valueString = EncryptString.EncryptTripleDES(valueString);
///设置Cookie
SetCookie(key,valueString,expires);
}
/// <summary>
/// 设置加密之后的Cookie
/// </summary>
/// <param name="key"></param>
/// <param name="valueString"></param>
public static void SetEncryptedCookie(String key,String valueString)
{ ///获取关键字和值
key = EncryptString.Encrypt(key);
valueString = EncryptString.Encrypt(valueString);
///设置Cookie
SetCookie(key,valueString);
}
/// <summary>
/// 设置加密之后的Cookie
/// </summary>
/// <param name="key"></param>
/// <param name="valueString"></param>
/// <param name="expires"></param>
public static void SetEncryptedCookie(String key,String valueString,DateTime expires)
{
///获取关键字和值
key = EncryptString.Encrypt(key);
valueString = EncryptString.Encrypt(valueString);
///设置Cookie
SetCookie(key,valueString,expires);
}
/// <summary>
/// 获取DES方法加密之后的Cookie
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public static String GetTripleDESEncryptedCookieValue(String key)
{
///获取关键字和值
key = EncryptString.EncryptTripleDES(key);
String valueString = GetCookieValue(key);
///获取Cookie
valueString = EncryptString.DecryptTripleDES(valueString);
return(valueString);
}
/// <summary>
/// 获取DES方法加密之后的Cookie
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public static String GetEncryptedCookieValue(String key)
{
///获取关键字和值
key = EncryptString.Encrypt(key);
String valueString = GetCookieValue(key);
///获取Cookie
valueString = EncryptString.Decrypt(valueString);
return(valueString);
}
/// <summary>
/// 获取Cookie
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public static HttpCookie GetCookie(String key)
{
///获取关键字和值
key = HttpContext.Current.Server.UrlEncode(key);
///获取Cookie
return(HttpContext.Current.Request.Cookies.Get(key));
}
/// <summary>
/// 获取Cookie
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public static String GetCookieValue(String key)
{
///获取关键字和值
String valueString = GetCookie(key).Value;
///获取Cookie
valueString = HttpContext.Current.Server.UrlDecode(valueString);
return(valueString);
}
}
}