c# 字符串加密解密

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Security;  
  6. using System.Security.Cryptography;  
  7. using System.IO;  
  8. namespace ConsoleApplication1  
  9. {  
  10.     class Program  
  11.     {  
  12.         static void Main(string[] args)  
  13.         {  
  14.             Console.WriteLine(EncryptDES("aaaaaaaaaaaaaaaa""22222222"));//加密   
  15.             Console.WriteLine(DecryptDES(EncryptDES("aaaaaaaaaaaaaaaa""22222222"), "22222222"));//解密   
  16.         }  
  17.         //默认密钥向量   
  18.         private static byte[] Keys = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };         
  19.         /// DES加密字符串           
  20.         /// 待加密的字符串   
  21.         /// 加密密钥,要求为8位   
  22.         /// 加密成功返回加密后的字符串,失败返回源串    
  23.         public static string EncryptDES(string encryptString, string encryptKey)  
  24.         {  
  25.             try  
  26.             {  
  27.                 byte[] rgbKey = Encoding.UTF8.GetBytes(encryptKey.Substring(0, 8));  
  28.                 byte[] rgbIV = Keys;  
  29.                 byte[] inputByteArray = Encoding.UTF8.GetBytes(encryptString);  
  30.                 DESCryptoServiceProvider dCSP = new DESCryptoServiceProvider();  
  31.                 MemoryStream mStream = new MemoryStream();  
  32.                 CryptoStream cStream = new CryptoStream(mStream, dCSP.CreateEncryptor(rgbKey, rgbIV), CryptoStreamMode.Write);  
  33.                 cStream.Write(inputByteArray, 0, inputByteArray.Length);  
  34.                 cStream.FlushFinalBlock();  
  35.                 return Convert.ToBase64String(mStream.ToArray());  
  36.             }  
  37.             catch  
  38.             {  
  39.                 return encryptString;  
  40.             }  
  41.         }  
  42.         ///    
  43.         /// DES解密字符串           
  44.         /// 待解密的字符串   
  45.         /// 解密密钥,要求为8位,和加密密钥相同   
  46.         /// 解密成功返回解密后的字符串,失败返源串   
  47.         public static string DecryptDES(string decryptString, string decryptKey)  
  48.         {  
  49.             try  
  50.             {  
  51.                 byte[] rgbKey = Encoding.UTF8.GetBytes(decryptKey);  
  52.                 byte[] rgbIV = Keys;  
  53.                 byte[] inputByteArray = Convert.FromBase64String(decryptString);  
  54.                 DESCryptoServiceProvider DCSP = new DESCryptoServiceProvider();  
  55.                 MemoryStream mStream = new MemoryStream();  
  56.                 CryptoStream cStream = new CryptoStream(mStream, DCSP.CreateDecryptor(rgbKey, rgbIV), CryptoStreamMode.Write);  
  57.                 cStream.Write(inputByteArray, 0, inputByteArray.Length);  
  58.                 cStream.FlushFinalBlock();  
  59.                 return Encoding.UTF8.GetString(mStream.ToArray());  
  60.             }  
  61.             catch  
  62.             {  
  63.                 return decryptString;  
  64.             }  
  65.         }  
  66.     }  
  67. }   
posted @ 2012-07-24 12:43  keyen  阅读(549)  评论(0编辑  收藏  举报