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

            }

  • 相关阅读:
    【C++】小心使用文件读写模式:回车(' ') 换行(' ')问题的一次纠结经历
    小记同学一次奇葩的DNS欺骗实验失败经历
    IE的BHO通过IHTMLDocument2接口获得网页源代码
    HTML5离线缓存攻击测试(二)
    HTML5离线缓存攻击测试
    PHP防止SQL注入的方法
    Linux系统环境变量的四个配置文件的关系
    CentOS 7 上搭建LNMP环境
    [Linux][Nginx][02]Config
    [Linux][Nginx][01]Install
  • 原文地址:https://www.cnblogs.com/qiqiBoKe/p/3024536.html
Copyright © 2020-2023  润新知