2006-10-20 16:26���� Cookies Response.Cookies["askCRM"]["USERID"] = Tools.Encrypt(strUserName.Trim(), Tools.myKey); ���� string strUid = Request.Cookies["askCRM"]["USERID"]; strUid = Tools.Decrypt(strUid, Tools.myKey); ������ ܽ ���õķ��� using System; using System.Data; using System.Configuration; 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.Security.Cryptography; using System.IO; using System.Text; /// <summary> /// Tools �� Ҫ �� /// public class Tools { /// <summary> /// ��ǰ���������ʹ�õ��� /// public static readonly string myKey = "q0m3sd8l"; #region ������ ܷ /// <summary> /// ������ ܷ /// /// <param name="pToEncrypt">��Ҫ�����ַ� /// <param name="sKey">�� /// ������ַ ܺ � public static string Encrypt(string pToEncrypt, string sKey) { try { DESCryptoServiceProvider des = new DESCryptoServiceProvider(); //���ַ�ŵ�byte������
// 4ʹ�õ�UTF8���룬�Ҹij�Unicode�����ˣ����� byte[] inputByteArray = Encoding.Default.GetBytes(pToEncrypt);
//��b��������� ܶ ��ƫ�� //ʹ�����������������Ӣ���õ� 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); cs.Write(inputByteArray, 0, inputByteArray.Length); cs.FlushFinalBlock(); StringBuilder ret = new StringBuilder(); foreach (byte b in ms.ToArray()) { ret.AppendFormat("{0:X2}", b); } ret.ToString(); return ret.ToString();
} catch (Exception ex) {
JS.Alert("д��������Ϣʧ����ܣϸ��Ϣ��" + ex.Message.Replace("\r\n", "").Replace("'", "")); } return ""; } #endregion #region ������ ܷ /// <summary> /// ������ ܷ /// /// <param name="pToDecrypt">��Ҫ�����ַ ܵ � /// <param name="sKey">�� ܳ /// ������ַ ܺ � public static string Decrypt(string pToDecrypt, string sKey) { try { DESCryptoServiceProvider des = new DESCryptoServiceProvider(); 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; } //��b��������� ܶ ��ƫ�� ����ֵ��Ҫ������� � des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
des.IV = ASCIIEncoding.ASCII.GetBytes(sKey); MemoryStream ms = new MemoryStream(); CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write); cs.Write(inputByteArray, 0, inputByteArray.Length); cs.FlushFinalBlock(); //��bStringBuild����CreateDecryptʹ�õ������� ���ѽ����� ܺ õ��������� StringBuilder ret = new StringBuilder(); return System.Text.Encoding.Default.GetString(ms.ToArray()); } catch (Exception ex) { JS.Alert("�� ������Ϣʧ����ܣϸ��Ϣ��" + ex.Message.Replace("\r\n", "").Replace("'", "")); } return ""; } #endregion