采用TripleDES加密类给 cookie 进行加密

using System;
using System.Security;
using System.Security.Cryptography;
using System.Diagnostics;
using System.Text;
using System.IO;
using System.Web;

namespace PSMCryptoUtil
{
 /// <summary>
 /// 采用 TripleDES 加密
 /// 捷 2005-5-23
 /// CryptoUtil 的摘要说明。
 /// </summary>
 public class CryptoUtil
 {
  //对TripleDES,采取24字节或192位的密钥和初始向量
  private static byte[] Key_192 ;
  private static byte[] IV_192 ;
  public CryptoUtil()
  {
   if(Key_192 == null)
   {
    Key_192 = CreateByte(24);
   }
   if(IV_192 == null)
   {
    IV_192 = CreateByte(24);
   }
  }
  public CryptoUtil(byte[] K,byte[] V)
  {
   Key = K;
   IV = V;
  }
  public byte[] Key
  {
   get{return Key_192;}
   set{Key_192 = value;}
  }
  public byte[] IV
  {
   get{return IV_192;}
   set{IV_192 = value;}
  }
  /// <summary>
  /// 加密
  /// </summary>
  /// <param name="values"></param>
  /// <returns></returns>
  public string EnCrypt(string values)
  {
   string rstr = "";
   string v = values;
   if(values.Length > 0)
   {
    TripleDESCryptoServiceProvider cryptoP = new TripleDESCryptoServiceProvider();
    MemoryStream ms = new MemoryStream();
    CryptoStream cs = new CryptoStream(ms,cryptoP.CreateEncryptor(Key_192,IV_192),CryptoStreamMode.Write);
    StreamWriter sw = new StreamWriter(cs);
    sw.Write(v);
    sw.Flush();
    cs.FlushFinalBlock();
    ms.Flush();
    rstr = Convert.ToBase64String(ms.GetBuffer(),0,Convert.ToInt32(ms.Length));
    rstr = HttpUtility.UrlEncode(rstr);
   }
   return rstr;
  }
  /// <summary>
  /// 解密
  /// </summary>
  /// <param name="values"></param>
  /// <returns></returns>
  public string DeCrypt(string values)
  {
   string rstr = "";
   string v = values;
   if(values == null)
   {
    return rstr;
   }
   if(values.Length > 0)
   {
    v = HttpUtility.UrlDecode(v);
    TripleDESCryptoServiceProvider cryptoP = new TripleDESCryptoServiceProvider();
    byte[] buffer = Convert.FromBase64String(v);
    MemoryStream ms = new MemoryStream(buffer);
    CryptoStream cs = new CryptoStream(ms,cryptoP.CreateDecryptor(Key_192,IV_192),CryptoStreamMode.Read);
    StreamReader sr = new StreamReader(cs);
    rstr = sr.ReadToEnd();
    ms.Flush();
    cs.Flush();
    sr = null;
   }
   return rstr;
  }
  /// <summary>
  /// 取得密匙
  /// </summary>
  /// <param name="len"></param>
  /// <returns></returns>
  private byte[] CreateByte(int len)
  {
   byte[] random = new Byte[len];
   System.Security.Cryptography.RNGCryptoServiceProvider rng = new System.Security.Cryptography.RNGCryptoServiceProvider();
   rng.GetBytes(random);
   return random;
  }
 }
}

using System;
using System.Web;
using System.Text;

namespace PSMCookieUtil
{
 /// <summary>
 /// CookieUtil 的摘要说明。
 /// 用 PSMCryptoUtil 对 cookie 进行加密
 /// 捷 2005-5-23
 /// </summary>
 public class CookieUtil
 {
  private string _CookieName = "PSManage";

  public string CookieName
  {
   get{return _CookieName;}
   set{_CookieName = value;}
  }
  public CookieUtil()
  {
  }
  public CookieUtil(string cookiename)
  {
   CookieName = cookiename;
  }
  /// <summary>
  /// 取得键为 key 的 Cookie 值
  /// </summary>
  /// <param name="key"></param>
  /// <returns></returns>
  public string GetCookieValue(string key)
  {
   string rstr = "";
   PSMCryptoUtil.CryptoUtil cutil = new PSMCryptoUtil.CryptoUtil();
   string dekey = cutil.EnCrypt(key);
   if(HttpContext.Current.Request.Cookies[_CookieName] != null)
   {
    rstr = HttpContext.Current.Request.Cookies[_CookieName][dekey];
    rstr = cutil.DeCrypt(rstr);
   }
   return rstr;
  }
  public void SetCookie(string key,string values)
  {
   HttpCookie cookie = null;
   string k,v;
   PSMCryptoUtil.CryptoUtil cutil = new PSMCryptoUtil.CryptoUtil();
   k = cutil.EnCrypt(key);
   v = cutil.EnCrypt(values);
   if(HttpContext.Current.Request.Cookies[_CookieName] == null)
   {
    cookie = new HttpCookie(_CookieName);
   }
   else
   {
    cookie = HttpContext.Current.Request.Cookies[_CookieName];
   }
   cookie[k] = v;
   HttpContext.Current.Response.AppendCookie(cookie);
  }
 }
}

阅读更多
个人分类: ASP.NET
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭