首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

C#的DES加密

Posted on 2007-10-26 09:13  停留的风  阅读(12058)  评论(0编辑  收藏  举报
  1 第一种:
  2 using System;
  3 using System.Text;
  4 using System.Security.Cryptography;
  5 using System.IO;
  6 
  7 /// <summary>
  8 /// Summary description for EncryptAndDecrypt
  9 /// </summary>
 10 public class EncryptAndDecrypt
 11 {
 12     //默认密钥向量
 13     private static byte[] Keys = { 0x120x340x560x780x900xAB0xCD0xEF };
 14     /**//// <summary>
 15     /// DES加密字符串
 16     /// </summary>
 17     /// <param name="encryptString">待加密的字符串</param>
 18     /// <param name="encryptKey">加密密钥,要求为8位</param>
 19     /// <returns>加密成功返回加密后的字符串,失败返回源串</returns>
 20     public static string EncryptDES(string encryptString, string encryptKey)
 21     {
 22         try
 23         {           
 24             byte[] rgbKey = Encoding.UTF8.GetBytes(encryptKey.Substring(08));
 25             byte[] rgbIV = Keys;
 26             byte[] inputByteArray = Encoding.UTF8.GetBytes(encryptString);
 27              DESCryptoServiceProvider dCSP = new DESCryptoServiceProvider();
 28              MemoryStream mStream = new MemoryStream();
 29              CryptoStream cStream = new CryptoStream(mStream, dCSP.CreateEncryptor(rgbKey, rgbIV), CryptoStreamMode.Write);
 30              cStream.Write(inputByteArray, 0, inputByteArray.Length);
 31              cStream.FlushFinalBlock();
 32             return Convert.ToBase64String(mStream.ToArray());
 33          }
 34         catch
 35         {
 36             return encryptString;
 37         }
 38     }
 39 
 40     /**//// <summary>
 41     /// DES解密字符串
 42     /// </summary>
 43     /// <param name="decryptString">待解密的字符串</param>
 44     /// <param name="decryptKey">解密密钥,要求为8位,和加密密钥相同</param>
 45     /// <returns>解密成功返回解密后的字符串,失败返源串</returns>
 46     public static string DecryptDES(string decryptString, string decryptKey)
 47     {
 48         try
 49         {
 50             byte[] rgbKey = Encoding.UTF8.GetBytes(decryptKey);
 51             byte[] rgbIV = Keys;
 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 第二种:
 67 
 68 //名称空间
 69 using System;
 70 using System.Security.Cryptography;
 71 using System.IO;
 72 using System.Text; 
 73 
 74 /// <summary>
 75 /// Summary description for Encryption
 76 /// </summary>
 77 public class Encryption
 78 {
 79     /// <summary>
 80     /// Default Key
 81     /// </summary>
 82     public const string Key = "bmc.1001";
 83     /// <summary>
 84     /// Initial
 85     /// </summary>
 86     public Encryption()
 87     {
 88        ///
 89     }
 90     /// <summary>
 91     /// 加密方法
 92     /// </summary>
 93     /// <param name="pToEncrypt"></param>
 94     /// <param name="sKey"></param>
 95     /// <returns></returns>
 96     public string Encrypt(string pToEncrypt, string sKey)
 97     {
 98         DESCryptoServiceProvider des = new DESCryptoServiceProvider();
 99         //把字符串放到byte数组中
100         //原来使用的UTF8编码,我改成Unicode编码了,不行
101         byte[] inputByteArray = Encoding.Default.GetBytes(pToEncrypt);
102         //byte[]  inputByteArray=Encoding.Unicode.GetBytes(pToEncrypt); 
103 
104         //建立加密对象的密钥和偏移量
105         //原文使用ASCIIEncoding.ASCII方法的GetBytes方法
106         //使得输入密码必须输入英文文本
107         des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
108         des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
109         //创建其支持存储区为内存的流
110         MemoryStream ms = new MemoryStream();
111         //将数据流链接到加密转换的流
112         CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
113         //Write  the  byte  array  into  the  crypto  stream 
114         //(It  will  end  up  in  the  memory  stream) 
115         cs.Write(inputByteArray, 0, inputByteArray.Length);
116         //用缓冲区的当前状态更新基础数据源或储存库,随后清除缓冲区
117         cs.FlushFinalBlock();
118         //Get  the  data  back  from  the  memory  stream,  and  into  a  string 
119         byte[] EncryptData = (byte[])ms.ToArray();
120         return System.Convert.ToBase64String(EncryptData, 0, EncryptData.Length);
121     }
122     /// <summary>
123     /// 解密方法
124     /// </summary>
125     /// <param name="pToDecrypt"></param>
126     /// <param name="sKey"></param>
127     /// <returns></returns> 
128     public string Decrypt(string pToDecrypt, string sKey)
129     {
130         DESCryptoServiceProvider des = new DESCryptoServiceProvider();
131         //Put  the  input  string  into  the  byte  array 
132         byte[] inputByteArray = Convert.FromBase64String(pToDecrypt);
133        
134         //建立加密对象的密钥和偏移量,此值重要,不能修改
135         des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
136         des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
137         MemoryStream ms = new MemoryStream();
138         CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
139         //Flush  the  data  through  the  crypto  stream  into  the  memory  stream 
140         cs.Write(inputByteArray, 0, inputByteArray.Length);
141         cs.FlushFinalBlock();
142         return System.Text.Encoding.Default.GetString(ms.ToArray());
143     }
144 }
145