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); } /// <summary> /// 解密字符串 /// </summary> /// <param name="valueString"></param> /// <returns></returns> public static String Decrypt(String valueString) { if(valueString != "") { ///创建解密的provider DESCryptoServiceProvider desprovider = new DESCryptoServiceProvider(); byte[] buffer = Convert.FromBase64String(valueString); MemoryStream memoryStream = new MemoryStream(); ///解密给定的字符串 CryptoStream cryptoStream = new CryptoStream(memoryStream,desprovider.CreateEncryptor (Key64,IV64),CryptoStreamMode.Read); StreamReader readerStream = new StreamReader(cryptoStream); return(readerStream.ReadToEnd()); } return(null); } /// <summary> /// DES加密方法 /// </summary> /// <param name="valueString"></param> /// <returns></returns> public static String EncryptTripleDES(String valueString) { if(valueString != "") { ///创建加密的provider TripleDESCryptoServiceProvider triprovider = new TripleDESCryptoServiceProvider(); ///创建加密的流 MemoryStream memoryStream = new MemoryStream(); CryptoStream cryptoStream = new CryptoStream(memoryStream,triprovider.CreateEncryptor (Key192,IV192),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); } /// <summary> /// DES解密方法 /// </summary> /// <param name="valueString"></param> /// <returns></returns> public static String DecryptTripleDES(String valueString) { if(valueString != "") { ///创建加密的provider TripleDESCryptoServiceProvider triprovider = new TripleDESCryptoServiceProvider(); ///创建加密的流 byte[] buffer = Convert.FromBase64String(valueString); MemoryStream memoryStream = new MemoryStream(); CryptoStream cryptoStream = new CryptoStream(memoryStream,triprovider.CreateEncryptor (Key192,IV192),CryptoStreamMode.Read); StreamReader readerStream = new StreamReader(cryptoStream); return(readerStream.ReadToEnd()); } return(null); } } public class CookieEncrypt { /// <summary> /// 设置Cookie /// </summary> /// <param name="cookie"></param> public static void SetCookie(HttpCookie cookie) { HttpContext.Current.Response.Cookies.Set(cookie); } /// <summary> /// 设置Cookie /// </summary> /// <param name="key"></param> /// <param name="valueString"></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); } /// <summary> /// 设置Cookie /// </summary> /// <param name="key"></param> /// <param name="valueString"></param> /// <param name="expires"></param> public static void SetCookie(String key,String valueString,DateTime expires) { ///获取关键字和值 key = HttpContext.Current.Server.UrlEncode(key); valueString = HttpContext.Current.Server.UrlEncode(valueString); ///设置Cookie HttpCookie cookie = new HttpCookie(key,valueString); cookie.Expires = expires; SetCookie(cookie); } /// <summary> /// 设置使用DES方法加密之后的Cookie /// </summary> /// <param name="key"></param> /// <param name="valueString"></param> public static void SetTripleDESEncryptedCookie(String key,String valueString) { ///获取关键字和值 key = EncryptString.EncryptTripleDES(key); valueString = EncryptString.EncryptTripleDES(valueString); ///设置Cookie SetCookie(key,valueString); } /// <summary> /// 设置使用DES方法加密之后的Cookie /// </summary> /// <param name="key"></param> /// <param name="valueString"></param> /// <param name="expires"></param> public static void SetTripleDESEncryptedCookie(String key,String valueString,DateTime expires) { ///获取关键字和值 key = EncryptString.EncryptTripleDES(key); valueString = EncryptString.EncryptTripleDES(valueString); ///设置Cookie SetCookie(key,valueString,expires); } /// <summary> /// 设置加密之后的Cookie /// </summary> /// <param name="key"></param> /// <param name="valueString"></param> public static void SetEncryptedCookie(String key,String valueString) { ///获取关键字和值 key = EncryptString.Encrypt(key); valueString = EncryptString.Encrypt(valueString); ///设置Cookie SetCookie(key,valueString); } /// <summary> /// 设置加密之后的Cookie /// </summary> /// <param name="key"></param> /// <param name="valueString"></param> /// <param name="expires"></param> public static void SetEncryptedCookie(String key,String valueString,DateTime expires) { ///获取关键字和值 key = EncryptString.Encrypt(key); valueString = EncryptString.Encrypt(valueString); ///设置Cookie SetCookie(key,valueString,expires); } /// <summary> /// 获取DES方法加密之后的Cookie /// </summary> /// <param name="key"></param> /// <returns></returns> public static String GetTripleDESEncryptedCookieValue(String key) { ///获取关键字和值 key = EncryptString.EncryptTripleDES(key); String valueString = GetCookieValue(key); ///获取Cookie valueString = EncryptString.DecryptTripleDES(valueString); return(valueString); } /// <summary> /// 获取DES方法加密之后的Cookie /// </summary> /// <param name="key"></param> /// <returns></returns> public static String GetEncryptedCookieValue(String key) { ///获取关键字和值 key = EncryptString.Encrypt(key); String valueString = GetCookieValue(key); ///获取Cookie valueString = EncryptString.Decrypt(valueString); return(valueString); } /// <summary> /// 获取Cookie /// </summary> /// <param name="key"></param> /// <returns></returns> public static HttpCookie GetCookie(String key) { ///获取关键字和值 key = HttpContext.Current.Server.UrlEncode(key); ///获取Cookie return(HttpContext.Current.Request.Cookies.Get(key)); } /// <summary> /// 获取Cookie /// </summary> /// <param name="key"></param> /// <returns></returns> public static String GetCookieValue(String key) { ///获取关键字和值 String valueString = GetCookie(key).Value; ///获取Cookie valueString = HttpContext.Current.Server.UrlDecode(valueString); return(valueString); } } }
- cookie操作类(加密,获取,删除)
- cookie操作类(加密,获取,删除)
- cookie的操作 添加 获取 删除cookie
- js保存,获取,删除cookie的操作
- js保存,获取,删除cookie的操作
- js保存,获取,删除cookie的操作
- js 操作cookie 添加, 获取, 删除
- javaScript对Cookie的添加,获取,删除等操作
- 设置、获取、删除cookie
- cookie设置、获取、删除
- cookie设置,获取,删除。
- js 设置cookie 删除cookie 获取cookie
- 添加cookie,获取cookie,删除cookie
- jquery 设置cookie、删除cookie、获取cookie
- Jquery和js实现cookie操作手机浮层广告;附加:js获取、添加、删除cookie
- Cookie操作类、 包括创建、读取、修改、获取、销毁cookie
- Cookie操作类、 包括创建、读取、修改、获取、销毁cookie
- Jquery cookie操作示例,写入cookie,读取cookie,删除cookie
- 正则表达式
- [2_4_shortest_path_text] BFS => Shortest Path for Graphs with many Vertices buf few Edges
- jQuery提交多个表单
- 把IList和泛型数组转换为DataTable
- 无法打开用户默认数据库(用户 计算机名字\Administrator登录失败) 解决方法之一
- cookie操作类(加密,获取,删除)
- 禁用ViewState
- C#的is和as规则
- ajax回调函数执行顺序带来的同步异步问题
- android 如何动态设置View的margin和padding
- 访问DataSet中的表、行、列 的简单例子
- 我的程序员之路(二)
- Asp.net中Json数据的转化,读取与应用
- 图像处理的几何变换