如:
创建时的方法:
{
HttpCookie cookie = new HttpCookie ("mmm") ;
cookie["m1"] = "abc"; //加密???? Response . Cookies . Add ( cookie ) ;
}读时的方法:
{
HttpCookie cookie = Request.Cookies [ "mmm" ] ; //解密???? string name = cookie["m1"].ToString();
}
我不想用FormsAuthentication.HashPasswordForStoringInConfigFile之类asp.net自带的cookie加密方法,因为那要用到web.config等等文件。
我就想自己写一个加/解密的cookie方法。如上.md5加密好像不行,因为它不可逆阿。!
创建时的方法:
{
HttpCookie cookie = new HttpCookie ("mmm") ;
cookie["m1"] = "abc"; //加密???? Response . Cookies . Add ( cookie ) ;
}读时的方法:
{
HttpCookie cookie = Request.Cookies [ "mmm" ] ; //解密???? string name = cookie["m1"].ToString();
}
我不想用FormsAuthentication.HashPasswordForStoringInConfigFile之类asp.net自带的cookie加密方法,因为那要用到web.config等等文件。
我就想自己写一个加/解密的cookie方法。如上.md5加密好像不行,因为它不可逆阿。!
public string UnEncryptString(string strSource)
{
if (strSource == string.Empty)
{
return string.Empty;
}
else
{
byte[] bytes = Encoding.Unicode.GetBytes(strSource);
byte[] key = {43, 136, 76, 107, 172, 255, 227, 114};
byte[] iv = {102, 117, 69, 24, 131, 225, 93, 170};
DESCryptoServiceProvider desc = new DESCryptoServiceProvider();
ICryptoTransform detrans = desc.CreateDecryptor(key, iv);
byte[] de = detrans.TransformFinalBlock(bytes,0,bytes.Length);
return Encoding.Unicode.GetString(de);
}
} public string EncryptString(string strSource)
{
if (strSource == string.Empty)
{
return string.Empty;
}
else
{
byte[] bytes = Encoding.Unicode.GetBytes(strSource);
byte[] key = {43, 136, 76, 107, 172, 255, 227, 114};
byte[] iv = {102, 117, 69, 24, 131, 225, 93, 170};
DESCryptoServiceProvider desc = new DESCryptoServiceProvider();
ICryptoTransform entrans = desc.CreateEncryptor(key, iv);
byte[] en = entrans.TransformFinalBlock(bytes, 0, bytes.Length);
return Encoding.Unicode.GetString(en);
}
}
的方法加/解密一般的字符串可以,
但如果要读写cookie却不行,
报如下错误:
行 117: byte[] de = detrans.TransformFinalBlock(bytes,0,bytes.Length);
不正确的数据。 and
net_lover(孟子E章) ( 一级钻石) 信誉:140
的方法却要求必须是base64字符!!!!!
啊!!!!
,,,,,,,,
各位还有没有别的完美一点的方法?
thanks!....
看看这里呢
如
System.Security.Cryptography.DES
System.Security.Cryptography.DESCryptoServiceProvider
http://yefei520.cnblogs.com/archive/2006/07/14/450982.html