c# Des加密解密
1 using System; 2 using System.Collections.Generic; 3 using System.IO; 4 using System.Linq; 5 using System.Security.Cryptography; 6 using System.Text; 7 8 namespace Des加密 9 { 10 public static class DesClass 11 { 12 /// <summary> 13 /// des加密类 14 /// </summary> 15 /// <param name="encryptString">待加密字符</param> 16 /// <param name="key">加密所需 Key</param> 17 /// <param name="iv">加密所需 IV</param> 18 /// <returns>加密后的结果</returns> 19 public static string DesEncrypt(string encryptString, string key, string iv) 20 { 21 try 22 { 23 byte[] rgbKey = Encoding.UTF8.GetBytes(key); 24 byte[] rgbIV = Encoding.UTF8.GetBytes(iv); 25 byte[] inputByteArray = Encoding.UTF8.GetBytes(encryptString); 26 DESCryptoServiceProvider dCSP = new DESCryptoServiceProvider(); 27 MemoryStream mStream = new MemoryStream(); 28 CryptoStream cStream = new CryptoStream(mStream, dCSP.CreateEncryptor(rgbKey, rgbIV), CryptoStreamMode.Write); 29 cStream.Write(inputByteArray, 0, inputByteArray.Length); 30 cStream.FlushFinalBlock(); 31 return Convert.ToBase64String(mStream.ToArray()); 32 } 33 catch 34 { 35 return encryptString; 36 } 37 } 38 39 /// <summary> 40 /// des解密类 41 /// </summary> 42 /// <param name="decryptString">待加密字符</param> 43 /// <param name="key">加密所需 Key</param> 44 /// <param name="iv">加密所需 IV</param> 45 /// <returns>解密后的结果</returns> 46 public static string DesDecrypt(string decryptString, string key, string iv) 47 { 48 try 49 { 50 byte[] rgbKey = Encoding.UTF8.GetBytes(key); 51 byte[] rgbIV = Encoding.UTF8.GetBytes(iv); 52 byte[] inputByteArray = Convert.FromBase64String(decryptString); 53 DESCryptoServiceProvider DCSP = new DESCryptoServiceProvider(); 54 MemoryStream mStream = new MemoryStream(); 55 CryptoStream cStream = new CryptoStream(mStream, DCSP.CreateDecryptor(rgbKey, rgbIV), CryptoStreamMode.Write); 56 cStream.Write(inputByteArray, 0, inputByteArray.Length); 57 cStream.FlushFinalBlock(); 58 return Encoding.UTF8.GetString(mStream.ToArray()); 59 } 60 catch 61 { 62 return decryptString; 63 } 64 } 65 } 66 }
以沫浅夏----奔跑的孩子
个人博客地址:http://www.blog.liyang.love
个人博客地址:http://www.blog.liyang.love

浙公网安备 33010602011771号