public static void SetEncryptedCookie(String key,String valueString)
{ //设置使用DES加密后的Cookie
key = EncryptString.Encrypt(key);
valueString = EncryptString.Encrypt(valueString);
SetCookie(key,valueString);
}
public static void SetEncryptedCookie(String key,
String valueString,DateTime expires)
{ //设置使用DES加密后的Cookie,并设置Cookie的有效时间
key = EncryptString.Encrypt(key);
valueString = EncryptString.Encrypt(valueString);
SetCookie(key,valueString,expires);
}
public static String GetTripleDESEncryptedCookieValue(String key)
{ //获取使用TripleDES解密后的Cookie
key = EncryptString.EncryptTripleDES(key);
String valueString = GetCookieValue(key);
valueString = EncryptString.DecryptTripleDES(valueString);
return(valueString);
}
public static String GetEncryptedCookieValue(String key)
{ //获取使用DES解密后的Cookie
key = EncryptString.Encrypt(key);
String valueString = GetCookieValue(key);
valueString = EncryptString.Decrypt(valueString);
return(valueString);
}
public static HttpCookie GetCookie(String key)
{ //通过关键字获取Cookie
key = HttpContext.Current.Server.UrlEncode(key);
return(HttpContext.Current.Request.Cookies.Get(key));
}
public static String GetCookieValue(String key)
{ //通过关键字获取Cookie的value
String valueString = GetCookie(key).Value;
valueString = HttpContext.Current.Server.UrlDecode(valueString);
return(valueString);
}
}
4.设计页面EncryptCookies.aspx
把应用程序Example_12_6的默认页面WebForm1.aspx重命名为 EncryptCookies.aspx,并在页面上添加3个Label控件,它们的名称分别为TripleDESCookie、 EncryptCookie和myCookie,分别用来显示使用TripleDES加密后的Cookie的值、使用DES加密后的Cookie的值和 Cookie的原始值。页面EncryptCookies.aspx的设计界面如图12-17所示。
页面EncryptCookies.aspx的HTML设计代码如下:
<asp:Label ID="myCookie" Runat="server"></asp:Label>
<asp:Label ID="EncryptCookie" Runat="server"></asp:Label>
<asp:Label ID="TripleDESCookie" Runat="server"></asp:Label>
文章来源于领测软件测试网 https://www.ltesting.net/