博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Cookie练习
阅读量:4663 次
发布时间:2019-06-09

本文共 2507 字,大约阅读时间需要 8 分钟。

        <asp:Button ID="Button1" runat="server" Text="写入Cookie" OnClick="Button1_Click" />         <asp:Button ID="Button3" runat="server" Text="读出Cookie" OnClick="Button3_Click" />         <asp:Button ID="Button2" runat="server" Text="删除Cookie" OnClick="Button2_Click" />

 

后台代码:

 

        protected void Button1_Click(object sender, EventArgs e)         {

            //特殊加密算法             HttpCookie cookie = new HttpCookie("name");             cookie.Value = Encode("shang", "Rainight");             cookie.Expires = DateTime.Now.AddDays(1);             HttpContext.Current.Response.Cookies.Add(cookie);             Response.Write("写入成功");                   }

        protected void Button2_Click(object sender, EventArgs e)         {             HttpCookie cookie = new HttpCookie("name");             cookie.Expires = DateTime.Now.AddDays(-1);             HttpContext.Current.Response.Cookies.Add(cookie);

        }

        protected void Button3_Click(object sender, EventArgs e)         {

            if (Request.Cookies["name"] != null)             {                 Response.Write(Decode(Request.Cookies["name"].Value, "Rainight"));             }             else             {                 Response.Write("您还没有写入值!");             }         }

        //加密         public static string Encode(string str, string key)         {             try             {                 DESCryptoServiceProvider provider = new DESCryptoServiceProvider();                 provider.Key = Encoding.ASCII.GetBytes(key.Substring(0, 8));                 provider.IV = Encoding.ASCII.GetBytes(key.Substring(0, 8));                 byte[] bytes = Encoding.GetEncoding("GB2312").GetBytes(str);                 MemoryStream stream = new MemoryStream();                 CryptoStream stream2 = new CryptoStream(stream, provider.CreateEncryptor(), CryptoStreamMode.Write);                 stream2.Write(bytes, 0, bytes.Length);                 stream2.FlushFinalBlock();                 StringBuilder builder = new StringBuilder();                 foreach (byte num in stream.ToArray())                 {                     builder.AppendFormat("{0:X2}", num);                 }                 stream.Close();                 return builder.ToString();             }             catch             {                 return "不是用Encrypt加密的字符串,加密失败!";             }         }         //解密         public static string Decode(string str, string key)         {             try             {                 DESCryptoServiceProvider provider = new DESCryptoServiceProvider();                 provider.Key = Encoding.ASCII.GetBytes(key.Substring(0, 8));                 provider.IV = Encoding.ASCII.GetBytes(key.Substring(0, 8));                 byte[] buffer = new byte[str.Length / 2];                 for (int i = 0; i < (str.Length / 2); i++)                 {                     int num2 = Convert.ToInt32(str.Substring(i * 2, 2), 0x10);                     buffer[i] = (byte)num2;                 }                 MemoryStream stream = new MemoryStream();                 CryptoStream stream2 = new CryptoStream(stream, provider.CreateDecryptor(), CryptoStreamMode.Write);                 stream2.Write(buffer, 0, buffer.Length);                 stream2.FlushFinalBlock();                 stream.Close();                 return Encoding.GetEncoding("GB2312").GetString(stream.ToArray());             }             catch             {                 return "不是用Encrypt加密的字符串,解密失败!";             }

        }

 

转载于:https://www.cnblogs.com/qiqiBoKe/archive/2013/04/16/3024536.html

你可能感兴趣的文章
序列化
查看>>
输出一个三角形
查看>>
2016年(第15届)中国软件业务收入前百家企业名单(zz)
查看>>
Jamie Zawinski访谈:在折腾中成长 (zz.IS2120)
查看>>
Linq to JSON/Json.NET
查看>>
HDU 1242 Rescue(BFS)
查看>>
POJ 2104 K-th Number 划分树
查看>>
HTTP协议详解
查看>>
软件调试修炼之道之——山重水复疑无路
查看>>
内核模式驱动程序的网络结构 .
查看>>
BZOJ3837 : [Pa2013]Filary
查看>>
BZOJ3095 : 二元组
查看>>
[Leetcode] Container with Most Water
查看>>
服务消费者(Feign-下)
查看>>
Redis事务
查看>>
了解什么是WebLogic Server 多数据源(Multi-DataSource)
查看>>
搭建高性能计算环境(四)、应用软件的安装之VASP
查看>>
Kudu基本操作及概念
查看>>
python爬虫CSDN文章抓取
查看>>
【CC2530入门教程-04】CC2530的定时/计数器原理与应用
查看>>