首页 > 代码库 > 加解密方法

加解密方法

加解密Code如下:

 1         /// <summary> 2         /// 加密字符串:根据传入的字符串和密匙返回加密后的字串 3       /// </summary> 4         public static string Encrypt(string value, string key) 5         { 6             try 7             { 8                 key += "!@#$%^&*()"; 9                 Byte[] byteCrypt = { 0x10, 0x29, 38, 0x47, 0x56 };10                 Byte[] byteKey = System.Text.Encoding.UTF8.GetBytes(key.Substring(0, 10));11                 DESCryptoServiceProvider des = new DESCryptoServiceProvider();12                 Byte[] inputByteArray = System.Text.Encoding.UTF8.GetBytes(value);13                 MemoryStream ms = new MemoryStream();14                 CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(byteKey, byteCrypt), CryptoStreamMode.Write);15                 cs.Write(inputByteArray, 0, inputByteArray.Length);16                 cs.FlushFinalBlock();17                 return Convert.ToBase64String(ms.ToArray());18             }19             catch (Exception ex)20             {21                 throw ex;22             }23         }24 25         /// <summary>26         /// 加密字符串:根据传入的字符串和密匙返回加密后的字串27       /// </summary>28         public static string Encrypt(string value)29         {30             return Encrypt(value, string.Empty);31         }32 33         /// <summary>34         /// 解密字符串:根据传入的字符串和密匙返回解密后的字串35       /// </summary>36         public static string Decrypt(string value, string key)37         {38             try39             {40                 key += "!@#$%^&*()";41                 Byte[] byteCrypt = { 0x10, 0x29, 38, 0x47, 0x56 };42                 Byte[] byteKey = System.Text.Encoding.UTF8.GetBytes(key.Substring(0, 10));43                 DESCryptoServiceProvider des = new DESCryptoServiceProvider();44                 Byte[] inputByteArray = Convert.FromBase64String(value);45                 MemoryStream ms = new MemoryStream();46                 CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(byteKey, byteCrypt), CryptoStreamMode.Write);47                 cs.Write(inputByteArray, 0, inputByteArray.Length);48                 cs.FlushFinalBlock();49                 System.Text.Encoding encoding = System.Text.Encoding.UTF8;50                 return encoding.GetString(ms.ToArray());51             }52             catch (Exception ex)53             {54                 throw ex;55             }56         }57 58         /// <summary>59         /// 解密字符串:根据传入的字符串和密匙返回解密后的字串60       /// </summary>61         public static string Decrypt(string value)62         {63             return Decrypt(value, string.Empty);64         }
View Code

 

加解密方法