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

            }

  • 相关阅读:
    Python | PyQt5编写计时器与倒计时应用程序
    AI文件与PS文件相互导入,并分层可编辑
    maple解方程组
    有限元数值分析
    常用Latex编辑数学公式
    notion
    总结一下ANSYS中不同单元之间选择与连接问题
    参考文献的引用方法
    Abaqus CAE笔记本
    几种大文件传输的平台
  • 原文地址:https://www.cnblogs.com/qiqiBoKe/p/3024536.html
Copyright © 2011-2022 走看看