日期:2014-05-18 浏览次数:20618 次
using System; using System.Diagnostics; using System.Security.Cryptography; using System.Text; using System.IO; using System.Web; namespace Press { public class CryptoUtil { public static byte[] KEY_64 = { 42, 16, 93, 56, 78, 4, 218, 223 }; public static byte[] IV_64 = { 55, 103, 46, 79, 36, 89, 167, 3 }; private static byte[] KEY_192 = { 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[] IV_192 = { 55, 103, 246, 79, 36, 99, 167, 3, 42, 5, 62, 83, 184, 7, 209, 13, 145, 23, 200, 58, 173, 10, 121, 222 }; //标准的DES加密 public static string Encrypt(string value1) { if (value1 != "") { DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider(); MemoryStream ms = new MemoryStream(); CryptoStream cs = new CryptoStream(ms, cryptoProvider.CreateEncryptor(KEY_64, IV_64), CryptoStreamMode.Write); StreamWriter sw = new StreamWriter(cs); sw.Write(value1); sw.Flush(); cs.FlushFinalBlock(); ms.Flush(); return Convert.ToBase64String(ms.GetBuffer(), 0, (int)ms.Length); } return ""; } //标准的DES解密 public static string Decrypt(string value1) { if (value1 != "") { DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider(); byte[] buffer = Convert.FromBase64String(value1); MemoryStream ms = new MemoryStream(buffer); CryptoStream cs = new CryptoStream(ms, cryptoProvider.CreateDecryptor(KEY_64, IV_64), CryptoStreamMode.Read); StreamReader sr = new StreamReader(cs); return sr.ReadToEnd(); } return ""; } } public class CookieUtil { public const string COOKIENULL = null; public static void SetEncryptedCookie(string key, string val) { key = CryptoUtil.Encrypt(key); val = CryptoUtil.Encrypt(val); SetCookie(key, val); } public static void SetEncryptedCookie(string key, string val, DateTime expires) { key = CryptoUtil.Encrypt(key); val = CryptoUtil.Encrypt(val); SetCookie(key, val, expires); } public static void SetEncryptedCookie(string key, string val, int DayNum) { key = CryptoUtil.Encrypt(key); val = CryptoUtil.Encrypt(val); DateTime expires = DateTime.Now.AddDays(DayNum); SetCookie(key, val, expires); } #region///SetCookie private static void SetCookie(string key, string val) { key = HttpContext.Current.Server.UrlEncode(key); val = HttpContext.Current.Server.UrlEncode(val); HttpCookie cookie = new HttpCookie(key, val); SetCookie(cookie); } private static void SetCookie(string key, string val, DateTime expires) {