C#DES加密和解密

努力的藍精靈發表於2013-11-26
  1. using System;  
  2. using System.IO;  
  3. using System.Text;  
  4. using System.Globalization;  
  5. using System.Collections.Generic;  
  6. using System.Security.Cryptography;  
  7.   
  8. namespace EncryptionTest  
  9. {  
  10.     class Program  
  11.     {  
  12.         static void Main(string[] args)  
  13.         {  
  14.             Program pr = new Program();  
  15.             string key = pr.GenerateKey();  
  16.   
  17.             string s1 = "測試字串";  
  18.             string s2 = pr.EncryptString(s1,key);  
  19.             string s3 = pr.DecryptString(s2,key);  
  20.             Console.WriteLine("原字串:[{0}]",s1);  
  21.             Console.WriteLine("加密後:[{0}]", s2);  
  22.             Console.WriteLine("解密後:[{0}]", s3);  
  23.   
  24.         }  
  25.   
  26.         /// <summary>  
  27.         /// 建立Key  
  28.         /// </summary>  
  29.         /// <returns></returns>  
  30.         public string GenerateKey()  
  31.         {  
  32.             DESCryptoServiceProvider desCrypto = (DESCryptoServiceProvider)DESCryptoServiceProvider.Create();  
  33.             return ASCIIEncoding.ASCII.GetString(desCrypto.Key);  
  34.         }  
  35.         /// <summary>  
  36.         /// 加密字串  
  37.         /// </summary>  
  38.         /// <param name="sinputString"></param>  
  39.         /// <param name="Skey"></param>  
  40.         /// <returns></returns>  
  41.         public string EncryptString(string sinputString, string Skey)  
  42.         {  
  43.             byte[] data = Encoding.UTF8.GetBytes(sinputString);  
  44.             DESCryptoServiceProvider DES = new DESCryptoServiceProvider();  
  45.             DES.Key = ASCIIEncoding.ASCII.GetBytes(Skey);  
  46.             DES.IV = ASCIIEncoding.ASCII.GetBytes(Skey);  
  47.             ICryptoTransform desEncrypt = DES.CreateEncryptor();  
  48.             byte[] result = desEncrypt.TransformFinalBlock(data, 0, data.Length);  
  49.             return BitConverter.ToString(result);  
  50.         }  
  51.         /// <summary>  
  52.         /// 解密字串  
  53.         /// </summary>  
  54.         /// <param name="sinputString"></param>  
  55.         /// <param name="sKey"></param>  
  56.         /// <returns></returns>  
  57.         public string DecryptString(string sinputString, string sKey)  
  58.         {  
  59.             string[] sinput = sinputString.Split("-".ToCharArray());  
  60.             byte[] data = new byte[sinput.Length];  
  61.             for (int i = 0; i < sinput.Length; i++)  
  62.             {  
  63.                 data[i] = byte.Parse(sinput[i], NumberStyles.HexNumber);  
  64.             }  
  65.             DESCryptoServiceProvider DES = new DESCryptoServiceProvider();  
  66.             DES.Key = ASCIIEncoding.ASCII.GetBytes(sKey);  
  67.             DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey);  
  68.             ICryptoTransform desencrypt = DES.CreateDecryptor();  
  69.             byte[] result = desencrypt.TransformFinalBlock(data, 0, data.Length);  
  70.             return Encoding.UTF8.GetString(result);  
  71.         }  
  72.   
  73.         /// <summary>  
  74.         /// DES加密  
  75.         /// </summary>  
  76.         /// <param name="pToEncrypt"></param>  
  77.         /// <param name="sKey"></param>  
  78.         /// <returns></returns>  
  79.         public string MD5Encrypt(string pToEncrypt, string sKey)  
  80.         {  
  81.             DESCryptoServiceProvider des = new DESCryptoServiceProvider();  
  82.             byte[] inputByteArray = Encoding.Default.GetBytes(pToEncrypt);  
  83.             des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);  
  84.             des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);  
  85.             MemoryStream ms = new MemoryStream();  
  86.             CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);  
  87.             cs.Write(inputByteArray, 0, inputByteArray.Length);  
  88.             cs.FlushFinalBlock();  
  89.             StringBuilder ret = new StringBuilder();  
  90.             foreach (byte b in ms.ToArray())  
  91.             {  
  92.                 ret.AppendFormat("{0:X2}", b);  
  93.             }  
  94.             ret.ToString();  
  95.             return ret.ToString();  
  96.         }  
  97.         /// <summary>  
  98.         /// DES解密  
  99.         /// </summary>  
  100.         /// <param name="pToDecrypt"></param>  
  101.         /// <param name="sKey"></param>  
  102.         /// <returns></returns>  
  103.         public string MD5Decrypt(string pToDecrypt, string sKey)  
  104.         {  
  105.             DESCryptoServiceProvider des = new DESCryptoServiceProvider();  
  106.             byte[] inputByteArray = new byte[pToDecrypt.Length / 2];  
  107.             for (int x = 0; x < pToDecrypt.Length / 2; x++)  
  108.             {  
  109.                 int i = (Convert.ToInt32(pToDecrypt.Substring(x * 2, 2), 16));  
  110.                 inputByteArray[x] = (byte)i;  
  111.             }  
  112.             des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);  
  113.             des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);  
  114.             MemoryStream ms = new MemoryStream();  
  115.             CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);  
  116.             cs.Write(inputByteArray, 0, inputByteArray.Length);  
  117.             cs.FlushFinalBlock();  
  118.             StringBuilder ret = new StringBuilder();  
  119.             return System.Text.Encoding.Default.GetString(ms.ToArray());  
  120.         }  
  121.     }  
  122.   
  123.   
  124. }  

相關文章