|M| 原贴[请大加给一个Cookie的加密和解密的过程] 可是在我的机子上都不成功大家帮帮运行一下代码看是不是我的机子有问题 谢谢
原贴:http://community.csdn.net/Expert/topic/5098/5098482.xml?temp=3.179568E-02
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Security;
using System.Security.Cryptography;
using System.Text;
public partial class Default4 : System.Web.UI.Page
{
public static byte[] DESKey = new byte[] { 0x82, 0xBC, 0xA1, 0x6A, 0xF5, 0x87, 0x3B, 0xE6, 0x59, 0x6A, 0x32, 0x64, 0x7F, 0x3A, 0x2A, 0xBB, 0x2B, 0x68, 0xE2, 0x5F, 0x06, 0xFB, 0xB8, 0x2D, 0x67, 0xB3, 0x55, 0x19, 0x4E, 0xB8, 0xBF, 0xDD };
/// <summary>
/// DES加密
/// </summary>
/// <param name="strSource">待加密字串</param>
/// <param name="key">32位Key值</param>
/// <returns>加密后的字符串</returns>
public string DESEncrypt(string strSource)
{
return DESEncrypt(strSource, DESKey);
}
public string DESEncrypt(string strSource, byte[] key)
{
SymmetricAlgorithm sa = Rijndael.Create();
sa.Key = key;
sa.Mode = CipherMode.ECB;
sa.Padding = PaddingMode.Zeros;
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, sa.CreateEncryptor(), CryptoStreamMode.Write);
byte[] byt = Encoding.Unicode.GetBytes(strSource);
cs.Write(byt, 0, byt.Length);
cs.FlushFinalBlock();
cs.Close();
return Convert.ToBase64String(ms.ToArray());
}
/// <summary>
/// DES解密
/// </summary>
/// <param name="strSource">待解密的字串</param>
/// <param name="key">32位Key值</param>
/// <returns>解密后的字符串</returns>
public string DESDecrypt(string strSource)
{
return DESDecrypt(strSource, DESKey);
}
public string DESDecrypt(string strSource, byte[] key)
{
SymmetricAlgorithm sa = Rijndael.Create();
sa.Key = key;
sa.Mode = CipherMode.ECB;
sa.Padding = PaddingMode.Zeros;
ICryptoTransform ct = sa.CreateDecryptor();
byte[] byt = Convert.FromBase64String(strSource);
MemoryStream ms = new MemoryStream(byt);
CryptoStream cs = new CryptoStream(ms, ct, CryptoStreamMode.Read);
StreamReader sr = new StreamReader(cs, Encoding.Unicode);
return sr.ReadToEnd();
}
protected void Page_Load(object sender, EventArgs e)
{
string s;
s = DESEncrypt("one two three four");
s = DESDecrypt(s); //读出为"one two three four\0\0\0\0\0\0"
}
}