龙盟编程博客 | 无障碍搜索 | 云盘搜索神器
快速搜索
主页 > web编程 > asp.net编程 >

C#版可逆des加密解密算法源代码

时间:2009-12-21 11:47来源:未知 作者:admin 点击:
分享到:
先介绍一下什么是DES加密? DES是Data Encryption Standard(数据加密标准)的缩写。它是一种用56位密钥来加密64位数据的方法。 des Code [http://www.xueit.com] 1 public class EncryptUtility 2 { 3 #region DE

先介绍一下什么是DES加密?

DES是Data Encryption Standard(数据加密标准)的缩写。它是一种用56位密钥来加密64位数据的方法。

des Code [http://www.xueit.com]
 1 public class EncryptUtility
 2 {
 3         #region DES
 4         /// <summary>
 5         /// DES加密
 6         /// </summary>
 7         /// <param name="code">加密字符串</param>
 8         /// <param name="key">密钥</param>
 9         /// <returns></returns>
10         public static string DesEncrypt(string code, string key)
11         {
12             string iv = StringUtility.Reverse(key);
13             return DesEncrypt(code, key, iv);
14         }
15 
16         /// <summary>
17         /// DES加密
18         /// </summary>
19         /// <param name="code">加密字符串</param>
20         /// <param name="key">密钥</param>
21         /// <param name="iv">初始化向量</param>
22         /// <returns></returns>
23         public static string DesEncrypt(string code, string key, string iv)
24         {
25             DESCryptoServiceProvider des = new DESCryptoServiceProvider();
26             byte[] inputByteArray = Encoding.Default.GetBytes(code);
27             des.Key = ASCIIEncoding.ASCII.GetBytes(key);
28             des.IV = ASCIIEncoding.ASCII.GetBytes(iv);
29             MemoryStream ms = new MemoryStream();
30             CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
31             cs.Write(inputByteArray, 0, inputByteArray.Length);
32             cs.FlushFinalBlock();
33             StringBuilder ret = new StringBuilder();
34             foreach (byte b in ms.ToArray())
35             {
36                 ret.AppendFormat("{0:X2}", b);
37             }
38             ms.Dispose();
39             cs.Dispose();
40             //ret.ToString();
41             return ret.ToString();
42         }
43 
44 
45         /// <summary>
46         /// DES解密
47         /// </summary>
48         /// <param name="code">解密字符串</param>
49         /// <param name="key">密钥</param>
50         /// <param name="key"></param>
51         /// <returns></returns>
52         public static string DesDecrypt(string code, string key)
53         {
54             string iv = StringUtility.Reverse(key);
55             return DesDecrypt(code, key, iv);
56         }
57 
58 
59         /// <summary>
60         /// DES解密
61         /// </summary>
62         /// <param name="code">解密字符串</param>
63         /// <param name="key">密钥</param>
64         /// <param name="iv">初始化向量</param>
65         /// <returns></returns>
66         public static string DesDecrypt(string code, string key, string iv)
67         {
68             DESCryptoServiceProvider des = new DESCryptoServiceProvider();
69             byte[] inputByteArray = new byte[code.Length / 2];
70             for (int x = 0; x < code.Length / 2; x  )
71             {
72                 int i = (Convert.ToInt32(code.Substring(x * 2, 2), 16));
73                 inputByteArray[x] = (byte)i;
74             }
75             des.Key = ASCIIEncoding.ASCII.GetBytes(key);
76             des.IV = ASCIIEncoding.ASCII.GetBytes(iv);
77             MemoryStream ms = new MemoryStream();
78             CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
79             cs.Write(inputByteArray, 0, inputByteArray.Length);
80             cs.FlushFinalBlock();
81             cs.Dispose();
82             StringBuilder ret = new StringBuilder();
83             return System.Text.Encoding.Default.GetString(ms.ToArray());
84         }
85         #endregion
86 }
精彩图集

赞助商链接