CryptoStream cryptoStream = new CryptoStream(memoryStream,
triprovider.CreateEncryptor(Key64,IV64),
CryptoStreamMode.Read);
//定义读IO流
StreamReader readerStream = new StreamReader(cryptoStream);
//返回解密后的字符串
return(readerStream.ReadToEnd());
}
return(null);
}
}
3.创建处理Cookie的类CookieEncrypt
在应用程序Example_12_6中的类文件EncryptString.cs中添加类CookieEncrypt,该类用来处理应用程序的Cookie,如加密Cookie、获取Cookie、解密Cookie等。类CookieEncrypt的程序代码如下:
public class CookieEncrypt
{
public static void SetCookie(HttpCookie cookie)
{ //设置Cookie
HttpContext.Current.Response.Cookies.Set(cookie);
}
public static void SetCookie(String key,String valueString)
{ //设置加密后的Cookie
key = HttpContext.Current.Server.UrlEncode(key);
valueString = HttpContext.Current.Server.UrlEncode(valueString);
HttpCookie cookie = new HttpCookie(key,valueString);
SetCookie(cookie);
}
public static void SetCookie(String key,String valueString,
DateTime expires)
{ //设置加密后的Cookie,并设置Cookie的有效时间
key = HttpContext.Current.Server.UrlEncode(key);
valueString = HttpContext.Current.Server.UrlEncode(valueString);
HttpCookie cookie = new HttpCookie(key,valueString);
cookie.Expires = expires;
SetCookie(cookie);
}
public static void SetTripleDESEncryptedCookie(String key,
String valueString)
{ //设置使用TripleDES加密后的Cookie
key = EncryptString.EncryptTripleDES(key);
valueString = EncryptString.EncryptTripleDES(valueString);
SetCookie(key,valueString);
}
public static void SetTripleDESEncryptedCookie(String key,
String valueString,DateTime expires)
{ //设置使用TripleDES加密后的Cookie,并设置Cookie的有效时间
key = EncryptString.EncryptTripleDES(key);
valueString = EncryptString.EncryptTripleDES(valueString);
SetCookie(key,valueString,expires);
}
文章来源于领测软件测试网 https://www.ltesting.net/