ASP.NET 2.0 加密 Cookies

iDotNetSpace發表於2008-09-24

//加密 Cookies
//Response.Cookies["askCRM"]["USERID"] = Tools.Encrypt(strUserName.Trim(), Tools.myKey);
//解密
//string strUid = Request.Cookies["askCRM"]["USERID"];
//strUid = Tools.Decrypt(strUid, Tools.myKey);

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Security.Cryptography;
using System.IO;
using System.Text;

///


/// Tools 的摘要說明


///
///
namespace HuiTong.Common
{
    public class Tools
    {


        ///


        /// 當前程式加密所使用的金鑰

        ///
        public static readonly string sKey = "w0x3hd89";

        #region 加密方法
        ///


        /// 加密方法
        ///

        /// 需要加密字串
        /// 金鑰
        /// 加密後的字串
        public static string Encrypt(string pToEncrypt)
        {
            try
            {
                DESCryptoServiceProvider des = new DESCryptoServiceProvider();
                //把字串放到byte陣列中


                //原來使用的UTF8編碼,我改成Unicode編碼了,不行
                byte[] inputByteArray = Encoding.Default.GetBytes(pToEncrypt);

                //建立加密物件的金鑰和偏移量


                //使得輸入密碼必須輸入英文文字
                des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
                des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
                MemoryStream ms = new MemoryStream();
                CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);

                cs.Write(inputByteArray, 0, inputByteArray.Length);
                cs.FlushFinalBlock();
                StringBuilder ret = new StringBuilder();
                foreach (byte b in ms.ToArray())
                {
                    ret.AppendFormat("{0:X2}", b);
                }
                ret.ToString();
                return ret.ToString();
            }
            catch (Exception ex)
            {
     
            }

            return "";
        }
        #endregion

        #region 解密方法
        ///


        /// 解密方法
        ///

        /// 需要解密的字串
        /// 密匙
        /// 解密後的字串
        public static string Decrypt(string pToDecrypt)
        {
            try
            {
                DESCryptoServiceProvider des = new DESCryptoServiceProvider();
                byte[] inputByteArray = new byte[pToDecrypt.Length / 2];
                for (int x = 0; x < pToDecrypt.Length / 2; x++)
                {
                    int i = (Convert.ToInt32(pToDecrypt.Substring(x * 2, 2), 16));
                    inputByteArray[x] = (byte)i;
                }

                //建立加密物件的金鑰和偏移量,此值重要,不能修改
                des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
                des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
                MemoryStream ms = new MemoryStream();
                CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
                cs.Write(inputByteArray, 0, inputByteArray.Length);
                cs.FlushFinalBlock();
                //建立StringBuild物件,CreateDecrypt使用的是流物件,必須把解密後的文字變成流物件
                StringBuilder ret = new StringBuilder();
                return System.Text.Encoding.Default.GetString(ms.ToArray());
            }
            catch (Exception ex)
            {
 
            }
            return "";
        }
        #endregion
    }
}

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/12639172/viewspace-462851/,如需轉載,請註明出處,否則將追究法律責任。

相關文章