熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

使用ASP.Net加密Cookie[3]

2013-11-13 11:04:56  來源: .NET編程 
    ——此文章摘自《ASPNET網絡數據庫開發實例精解》定價 特價 詳細>>

    CryptoStream cryptoStream = new CryptoStream(memoryStream
    triproviderCreateEncryptor(KeyIV)
    CryptoStreamModeRead);
    //定義讀IO流
    StreamReader readerStream = new StreamReader(cryptoStream);
    //返回解密後的字符串
    return(readerStreamReadToEnd());
    }
    return(null);
    }
    }

    .創建處理Cookie的類CookieEncrypt

    在應用程序Example__中的類文件EncryptStringcs中添加類CookieEncrypt該類用來處理應用程序的Cookie如加密Cookie獲取Cookie解密Cookie等類CookieEncrypt的程序代碼如下
    public class CookieEncrypt
    {
    public static void SetCookie(HttpCookie cookie)
    {   //設置Cookie
    HttpContextCurrentResponseCookiesSet(cookie);
    }
    public static void SetCookie(String keyString valueString)
    {   //設置加密後的Cookie
    key = HttpContextCurrentServerUrlEncode(key);
    valueString = HttpContextCurrentServerUrlEncode(valueString);
    HttpCookie cookie = new HttpCookie(keyvalueString);
    SetCookie(cookie);
    }
    public static void SetCookie(String keyString valueString
    DateTime expires)
    {   //設置加密後的Cookie並設置Cookie的有效時間
    key = HttpContextCurrentServerUrlEncode(key);
    valueString = HttpContextCurrentServerUrlEncode(valueString);
    HttpCookie cookie = new HttpCookie(keyvalueString);
    cookieExpires = expires;
    SetCookie(cookie);
    }
    public static void SetTripleDESEncryptedCookie(String key
    String valueString)
    {   //設置使用TripleDES加密後的Cookie
    key = EncryptStringEncryptTripleDES(key);
    valueString = EncryptStringEncryptTripleDES(valueString);
    SetCookie(keyvalueString);
    }
    public static void SetTripleDESEncryptedCookie(String key
    String valueStringDateTime expires)
    {   //設置使用TripleDES加密後的Cookie並設置Cookie的有效時間
    key = EncryptStringEncryptTripleDES(key);
    valueString = EncryptStringEncryptTripleDES(valueString);
    SetCookie(keyvalueStringexpires);
    }

[]  []  []  []  []  


From:http://tw.wingwit.com/Article/program/net/201311/15122.html
  • 上一篇文章:

  • 下一篇文章:
  • 推薦文章
    Copyright © 2005-2013 電腦知識網 Computer Knowledge   All rights reserved.