天天看點

cookie操作類(加密,擷取,删除)

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);

  }

  /// 解密字元串

  public static string decrypt(string valuestring)

   {   ///建立解密的provider

    byte[] buffer = convert.frombase64string(valuestring);

    memorystream memorystream = new memorystream();

    ///解密給定的字元串

                                (key64,iv64),cryptostreammode.read);

    streamreader readerstream = new streamreader(cryptostream);

    return(readerstream.readtoend());

  /// des加密方法

  public static string encrypttripledes(string valuestring)

   {  

    ///建立加密的provider

    tripledescryptoserviceprovider triprovider = new tripledescryptoserviceprovider();

    cryptostream cryptostream = new cryptostream(memorystream,triprovider.createencryptor

                               (key192,iv192),cryptostreammode.write);

  /// des解密方法

  public static string decrypttripledes(string valuestring)

                                (key192,iv192),cryptostreammode.read);

 }

 public class cookieencrypt

 {  

  /// 設定cookie

  /// <param name="cookie"></param>

  public static void setcookie(httpcookie cookie)

   httpcontext.current.response.cookies.set(cookie);

  /// <param name="key"></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);

  /// <param name="expires"></param>

  public static void setcookie(string key,string valuestring,datetime expires)

   cookie.expires = expires;

  /// 設定使用des方法加密之後的cookie

  public static void settripledesencryptedcookie(string key,string valuestring)

  {  

   ///擷取關鍵字和值

   key = encryptstring.encrypttripledes(key);

   valuestring = encryptstring.encrypttripledes(valuestring);

   setcookie(key,valuestring);

  public static void settripledesencryptedcookie(string key,string valuestring,datetime expires)

   setcookie(key,valuestring,expires);

  /// 設定加密之後的cookie

  public static void setencryptedcookie(string key,string valuestring)

   key = encryptstring.encrypt(key);

   valuestring = encryptstring.encrypt(valuestring);

  public static void setencryptedcookie(string key,string valuestring,datetime expires)

  /// 擷取des方法加密之後的cookie

  public static string gettripledesencryptedcookievalue(string key)

   string valuestring = getcookievalue(key);

   ///擷取cookie

   valuestring = encryptstring.decrypttripledes(valuestring);

   return(valuestring);   

  public static string getencryptedcookievalue(string key)

   valuestring = encryptstring.decrypt(valuestring);

  /// 擷取cookie

  public static httpcookie getcookie(string key)

   return(httpcontext.current.request.cookies.get(key));

  public static string getcookievalue(string key)

   string valuestring = getcookie(key).value;

   valuestring = httpcontext.current.server.urldecode(valuestring);

   return(valuestring);

}

繼續閱讀