DES对称加解密方法

改了一个DES对称加解密的代码,见者有份,即拿即用(注意看注释使用)。^_^

  1 using System;
  2 using System.IO;
  3 using System.Text;
  4 using System.Xml;
  5 using System.Configuration;
  6 using System.Security.Cryptography;
  7 
  8 namespace SymmetryEncrypt
  9 {
 10     /// <summary>
 11     /// DES对称加密方法
 12     /// Author:Fancy[tony]
 13     /// Create Time :2006-04-1
 14     /// Last Modify Time:2006-04-20
 15     /// </summary>
 16     public class DESEncryptData
 17     {
 18         #region DES对称加密方法
 19 
 20         /// <summary>
 21         /// DES对称加密方法
 22         /// </summary>
 23         /// <param name="InitData">原始待加密数据</param>
 24         public static string EncryptData(object InitData)
 25         {
 26             try
 27             {
 28                 string SecretKey = "desecret";
 29                 DESCryptoServiceProvider des = new DESCryptoServiceProvider();  
 30                 //把字符串放到byte数组中
 31                 Byte[] inputByteArray = Encoding.Default.GetBytes(InitData.ToString());
 32                 //建立加密对象的密钥和偏移量
 33                 des.Key = ASCIIEncoding.ASCII.GetBytes(SecretKey);  
 34                 //原文使用ASCIIEncoding.ASCII方法的GetBytes方法 
 35                 des.IV = ASCIIEncoding.ASCII.GetBytes(SecretKey); 
 36                 //使得输入密码必须输入英文文本
 37                 MemoryStream ms = new MemoryStream();     
 38                 CryptoStream cs = new CryptoStream(ms,des.CreateEncryptor(),CryptoStreamMode.Write);
 39                 cs.Write(inputByteArray, 0, inputByteArray.Length);
 40                 cs.FlushFinalBlock();
 41                 StringBuilder ret = new  StringBuilder();  
 42                 foreach(Byte b in ms.ToArray())  
 43                 {  
 44                     ret.AppendFormat("{0:X2}", b);
 45                 }
 46                 ret.ToString();
 47                 return  ret.ToString(); 
 48             }
 49             catch
 50             {
 51                 return "";
 52             }
 53         }
 54 
 55         /// <summary>
 56         /// DES对称加密方法
 57         /// </summary>
 58         /// <param name="InitData">原始待加密数据</param>
 59         /// <param name="SecretKey">加密密钥,密钥长度必须为八位有效英文字符,例如"12secret"</param>
 60         public static string EncryptData(object InitData ,string SecretKey)
 61         {
 62             try
 63             {
 64                 DESCryptoServiceProvider des = new DESCryptoServiceProvider();  
 65                 //把字符串放到byte数组中    
 66                 Byte[] inputByteArray = Encoding.Default.GetBytes(InitData.ToString());
 67                 //建立加密对象的密钥和偏移量
 68                 des.Key = ASCIIEncoding.ASCII.GetBytes(SecretKey); 
 69                 //原文使用ASCIIEncoding.ASCII方法的GetBytes方法 
 70                 des.IV = ASCIIEncoding.ASCII.GetBytes(SecretKey);  
 71                 //使得输入密码必须输入英文文本
 72                 MemoryStream ms = new MemoryStream();     
 73                 CryptoStream cs = new CryptoStream(ms,des.CreateEncryptor(),CryptoStreamMode.Write);
 74                 cs.Write(inputByteArray, 0, inputByteArray.Length);
 75                 cs.FlushFinalBlock();
 76                 StringBuilder ret = new  StringBuilder();  
 77                 foreach(Byte b in ms.ToArray())  
 78                 {  
 79                     ret.AppendFormat("{0:X2}", b);
 80                 }
 81                 ret.ToString();  
 82                 return  ret.ToString(); 
 83             }
 84             catch
 85             {
 86                 return "";
 87             }
 88         }
 89 
 90         #endregion
 91 
 92         #region DES对称解密方法        
 93 
 94         /// <summary>
 95         /// DES对称解密方法
 96         /// </summary>
 97         /// <param name="EncryptedData">待解密数据</param>
 98         public static string DecryptData(object EncryptedData)
 99         {
100             try
101             {
102                 string SecretKey = "desecret";
103                 string pToDecrypt = EncryptedData.ToString();
104                 DESCryptoServiceProvider des = new DESCryptoServiceProvider(); 
105                 Byte[] inputByteArray = new byte[pToDecrypt.Length / 2];  
106                 for(int x = 0; x < pToDecrypt.Length / 2; x++)  
107                 {  
108                     int i = (Convert.ToInt32(pToDecrypt.Substring(x * 22), 16));  
109                     inputByteArray[x]  =  (byte)i;  
110                 }
111                 //建立加密对象的密钥和偏移量,此值重要,不能修改  
112                 des.Key = ASCIIEncoding.ASCII.GetBytes(SecretKey);  
113                 des.IV  = ASCIIEncoding.ASCII.GetBytes(SecretKey);  
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                 return System.Text.Encoding.Default.GetString(ms.ToArray());  
119             }
120             catch
121             {
122                 return "";
123             }
124         }
125 
126         /// <summary>
127         /// DES对称解密方法
128         /// </summary>
129         /// <param name="EncryptedData">待解密数据</param>
130         /// <param name="SecretKey">解密密钥,必须是加密时的密钥,密钥长度必须为八位有效英文字符,例如"12secret"</param>
131         public static string DecryptData(object EncryptedData ,string SecretKey)
132         {
133             try
134             {
135                 string pToDecrypt = EncryptedData.ToString();
136                 DESCryptoServiceProvider des = new DESCryptoServiceProvider(); 
137                 Byte[] inputByteArray = new byte[pToDecrypt.Length / 2];  
138                 for(int x = 0; x < pToDecrypt.Length / 2; x++)  
139                 {  
140                     int i = (Convert.ToInt32(pToDecrypt.Substring(x * 22), 16));  
141                     inputByteArray[x]  =  (byte)i;  
142                 }
143                 //建立加密对象的密钥和偏移量,此值重要,不能修改 
144                 des.Key = ASCIIEncoding.ASCII.GetBytes(SecretKey);   
145                 des.IV  = ASCIIEncoding.ASCII.GetBytes(SecretKey);  
146                 MemoryStream ms = new MemoryStream();  
147                 CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(),CryptoStreamMode.Write); 
148                 cs.Write(inputByteArray, 0, inputByteArray.Length);  
149                 cs.FlushFinalBlock();
150                 return System.Text.Encoding.Default.GetString(ms.ToArray());  
151             }
152             catch
153             {
154                 return "";
155             }
156         }
157 
158         #endregion
159     }
160 }
posted @ 2007-04-03 17:44  MSDI  阅读(697)  评论(0编辑  收藏  举报