日期:2014-05-18  浏览次数:20438 次

cookie问题!!兼散分!!
请问那位大家指点一下如何对cookie进行加密。最好能给一个具体的例子。万分感谢!

------解决方案--------------------
可以 把Cookie转成Base64String 或者 Hash一下 但Hash后怎么解 还不清楚中
------解决方案--------------------
加密就是障眼法。
随便把用
点函数换换字符的表现方式
加密
public static string ToBase64(string psClearString)
{
byte[] data;
data = System.Text.ASCIIEncoding.ASCII.GetBytes(psClearString);
return Convert.ToBase64String(data);
}
解密
public static string FromBase64(string ps64String)
{
byte[] data;
data = Convert.FromBase64String(ps64String);
return System.Text.ASCIIEncoding.ASCII.GetString(data);
}


还有的直接用明码,但是加个识别。
把用户名的ID跟密码还有一个字符串(自己设置)合起来,然后MD5加密起来。

验证的时候自己在加密一个,对比一下。
------解决方案--------------------
/// <summary>
/// DES加密
/// </summary>
/// <param name= "pToEncrypt "> 要加密的字符串 </param>
/// <param name= "sKey "> 密钥 </param>
/// <returns> 密文 </returns>
public string Encrypt(string pToEncrypt, string sKey)
{
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
//把字符串放到byte数组中
//原来使用的UTF8编码,我改成Unicode编码了,不行
byte[] inputByteArray = Encoding.Default.GetBytes(pToEncrypt);
//byte[] inputByteArray=Encoding.Unicode.GetBytes(pToEncrypt);

//建立加密对象的密钥和偏移量
//原文使用ASCIIEncoding.ASCII方法的GetBytes方法
//使得输入密码必须输入英文文本
des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
//Write the byte array into the crypto stream
//(It will end up in the memory stream)
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
//Get the data back from the memory stream, and into a string
StringBuilder ret = new StringBuilder();
foreach (byte b in ms.ToArray())
{
//Format as hex
ret.AppendFormat( "{0:X2} ", b);
}
ret.ToString();
return ret.ToString();
}

/// <summary> DES解密 </summary>
/// <param name= "pToDecrypt "> 要还原的字符串 </param>
/// <param name= "sKey "> 密钥 </param>
/// <returns> 原文 </returns>
public string Decrypt(string pToDecrypt, string sKey)
{
DESCryptoServiceProvider des = new DESCryptoServiceProvider();

//Put the input string into the byte array
byte[] inputByteArray = new byte[pToDecrypt.Length / 2];
for (int x = 0; x < pToDecrypt.Length / 2; x++)
{
int i = (Convert.ToInt32(pToDecrypt.Substring(x * 2, 2), 16));
inputByteArray[x] = (byte)i;
}

//建立加密对象的密钥和偏移量,此值重要,不能修改
des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
MemoryStream