zoukankan      html  css  js  c++  java
  • Cookie练习

            <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加密的字符串,解密失败!";             }

            }

  • 相关阅读:
    在win7系统下安装把Ubuntu17.04安装在另一个硬盘开机无法进入Ubuntu问题的一种解决办法。【转】
    快速上手Ubuntu之安装篇——安装win7,Ubuntu16.04双系统【转】
    win7 64位系统与Ubuntu14.04 64位系统双系统安装【转】
    kernel中对文件的读写【学习笔记】【原创】
    快速解决Android中的selinux权限问题【转】
    Android如何配置init.rc中的开机启动进程(service)【转】
    linux内核驱动中对文件的读写 【转】
    很好的 DHCP协议与dhcpcd分析【转】
    android DHCP流程【转】
    Android wifi 从连接态自动断开的解决办法(dhcp导致)【转】
  • 原文地址:https://www.cnblogs.com/qiqiBoKe/p/3024536.html
Copyright © 2011-2022 走看看