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

            }

  • 相关阅读:
    tomcat控制台运行窗口中文乱码
    jquery html5 file 上传图片显示图片
    修改输入框placeholder文字默认颜色-webkit-input-placeholder
    H5移动端知识点总结
    PostgreSQL
    PostgreSQL
    Hexo系列(2)
    SQL
    Java笔记-序列化的注意点
    js、css外部文件的相对路径问题
  • 原文地址:https://www.cnblogs.com/qiqiBoKe/p/3024536.html
Copyright © 2020-2023  润新知