我试图使用下面的代码加密和解密cookie。我能够加密但解密功能接受字节输入。我怎样才能将cookie转换为解密字节?该错误是加密cookies
不能隐式转换型 “System.Web.HttpCookie” 到“字节[]
误差线是 “字节[]加密=的myCookie;” 我怎样才能转换解密之前的 “cookie” 为byte [] Enryption:
HttpCookie myCookie = new HttpCookie("co");
myCookie.Values.Add("customerId", dr["customerId"].ToString());
using (AesCryptoServiceProvider myAes = new AesCryptoServiceProvider())
{
byte[] encrypted = AesEncryption.EncryptStringToBytes_Aes(myCookie.ToString(), myAes.Key, myAes.IV);
}
Response.Cookies.Add(myCookie);
能解密:
HttpCookie myCookie = Request.Cookies["co"];
byte[] encrypted = myCookie;
using (AesCryptoServiceProvider myAes = new AesCryptoServiceProvider())
{
string decripted = AesEncryption.DecryptStringFromBytes_Aes(myCookie.ToString(), myAes.Key, myAes.IV);
}
你的加密值反向没有被添加到摆在首位的饼干吗? – driis
不应该使用byte [] encrypted = myCookie.ToString ..dont加密cookie,而是加密存在于其中的信息。 – thunderbird
MachineKey.Protect可能是在ASP.NET中加密数据的更好选择。 http://msdn.microsoft.com/en-us/library/system.web.security.machinekey.protect.aspx – spender