日期:2014-05-18 浏览次数:20935 次
/// <summary> /// 实现对字符串加密解密 /// </summary> /// <remarks></remarks> public class Encrypt { private SymmetricAlgorithm mCSP; private const string CIV = "kXwL7X2+fgm=";//密钥 private const string CKEY = "FwGQWRRgKCI=";//初始化向量 /// <summary> /// 初始化类的新实例。 /// </summary> /// <remarks></remarks> public Encrypt() { mCSP = new DESCryptoServiceProvider(); } /// <summary> /// 对字符串加密 /// </summary> /// <param name="value">要加密的字符串</param> /// <returns>string</returns> /// <remarks></remarks> public string EncyptString(string value) { ICryptoTransform ct; //加密器 MemoryStream ms; CryptoStream cs; byte[] byt; ct = mCSP.CreateEncryptor(Convert.FromBase64String(CKEY), Convert.FromBase64String(CIV)); byt = Encoding.UTF8.GetBytes(value);//将加密的字符串放到字节流中 ms = new MemoryStream(); cs = new CryptoStream(ms, ct, CryptoStreamMode.Write); cs.Write(byt, 0, byt.Length); cs.FlushFinalBlock(); cs.Close(); return Convert.ToBase64String(ms.ToArray()); } /// <summary> /// 解密 /// </summary> /// <param name="value">The value.</param> /// <returns></returns> /// <remarks></remarks> public string Decryption(string value) { ICryptoTransform ct; //加密器 MemoryStream ms; CryptoStream cs; byte[] byt; ct = mCSP.CreateDecryptor((Convert.FromBase64String(CKEY)), Convert.FromBase64String(CIV)); byt=Convert.FromBase64String(value); ms=new MemoryStream(); cs = new CryptoStream(ms, ct, CryptoStreamMode.Write);//解密流 cs.Write(byt,0,byt.Length); cs.FlushFinalBlock(); cs.Close(); return Encoding.UTF8.GetString(ms.ToArray()); }