1 #region ========加密======== 2 3 /// <summary> 4 /// 加密 5 /// </summary> 6 /// <param name="Text"></param> 7 /// <returns></returns> 8 public static string Encrypt(string Text) 9 { 10 if (Text == null) 11 return Text; 12 return Encrypt(Text, "123456789abcd"); 13 } 14 /// <summary> 15 /// 加密数据 16 /// </summary> 17 /// <param name="Text"></param> 18 /// <param name="sKey"></param> 19 /// <returns></returns> 20 public static string Encrypt(string Text, string sKey) 21 { 22 DESCryptoServiceProvider des = new DESCryptoServiceProvider(); 23 byte[] inputByteArray; 24 inputByteArray = Encoding.Default.GetBytes(Text); 25 des.Key = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8)); 26 des.IV = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8)); 27 System.IO.MemoryStream ms = new System.IO.MemoryStream(); 28 CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write); 29 cs.Write(inputByteArray, 0, inputByteArray.Length); 30 cs.FlushFinalBlock(); 31 StringBuilder ret = new StringBuilder(); 32 foreach (byte b in ms.ToArray()) 33 { 34 ret.AppendFormat("{0:X2}", b); 35 } 36 return ret.ToString(); 37 } 38 39 #endregion 40 41 #region ========解密======== 42 43 44 /// <summary> 45 /// 解密 46 /// </summary> 47 /// <param name="Text"></param> 48 /// <returns></returns> 49 public static string Decrypt(string Text) 50 { 51 if (Text == null) 52 return Text; 53 return Decrypt(Text, "123456789abcd"); 54 } 55 /// <summary> 56 /// 解密数据 57 /// </summary> 58 /// <param name="Text"></param> 59 /// <param name="sKey"></param> 60 /// <returns></returns> 61 public static string Decrypt(string Text, string sKey) 62 { 63 DESCryptoServiceProvider des = new DESCryptoServiceProvider(); 64 int len; 65 len = Text.Length / 2; 66 byte[] inputByteArray = new byte[len]; 67 int x, i; 68 for (x = 0; x < len; x++) 69 { 70 i = Convert.ToInt32(Text.Substring(x * 2, 2), 16); 71 inputByteArray[x] = (byte)i; 72 } 73 des.Key = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8)); 74 des.IV = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8)); 75 System.IO.MemoryStream ms = new System.IO.MemoryStream(); 76 CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write); 77 cs.Write(inputByteArray, 0, inputByteArray.Length); 78 cs.FlushFinalBlock(); 79 return Encoding.Default.GetString(ms.ToArray()); 80 } 98 197 #endregion