使用TripleDES进行加密和解密的方法
1using System;
2using System.Collections.Generic;
3using System.Text;
4using System.IO;
5using System.Security.Cryptography;
6
7namespace Sun.Yi.Csharp.Security
8{
9 /// <summary>
10 /// 使用TripleDES处理加密和解密的方法
11 /// </summary>
12 class TripleDESProcess
13 {
14 #region 内部私有变量
15
16 /// <summary>
17 /// 指定的密钥(Key)
18 /// </summary>
19 private byte[] bytDesKey;
20
21 /// <summary>
22 /// 初始化向量(IV)
23 /// </summary>
24 private byte[] bytDesIV;
25
26 /// <summary>
27 /// 空白字节的填充字符
28 /// </summary>
29 private char chrFillChar;
30
31 /// <summary>
32 /// 发生错误后的错误信息
33 /// </summary>
34 private string strErrorMessage;
35
36 /// <summary>
37 /// 已经被加密的文字长度(需要解密的文字内容,长度必须小于该值)
38 /// </summary>
39 private const int EncryptLength = 56;
40 /// <summary>
41 /// 没有被加密的文字长度(需要加密的文字内容,长度必须小于该值)
42 /// </summary>
43 private const int DecryptLength = 50;
44 /// <summary>
45 /// 密钥和初始化向量的长度(密钥和初始化向量的长度必须等于该值)
46 /// </summary>
47 private const int KeyLength = 16;
48
49 #endregion
50
51 #region 公共属性
52
53 /// <summary>
54 /// 指定的密钥(Key)
55 /// </summary>
56 public string DesKey
57 {
58 get
59 {
60 return Encoding.ASCII.GetString(this.bytDesKey);
61 }
62 set
63 {
64 this.bytDesKey = Encoding.ASCII.GetBytes(value);
65 }
66 }
67
68 /// <summary>
69 /// 初始化向量(IV)
70 /// </summary>
71 public string DesIV
72 {
73 get
74 {
75 return Encoding.ASCII.GetString(this.bytDesIV);
76 }
77 set
78 {
79 this.bytDesIV = Encoding.ASCII.GetBytes(value);
80 }
81 }
82
83 /// <summary>
84 /// 空白字节的填充字符
85 /// </summary>
86 public char FillChar
87 {
88 get
89 {
90 return this.chrFillChar;
91 }
92 set
93 {
94 this.chrFillChar = value;
95 }
96 }
97
98 /// <summary>
99 /// 发生错误后的错误信息
100 /// </summary>
101 public string ErrorMessage
102 {
103 get
104 {
105 return this.strErrorMessage;
106 }
107 }
108
109 #endregion
110
111 构造函数
138
139 #region 公共方法
140
141 /// <summary>
142 /// 使用TripleDES加密
143 /// </summary>
144 /// <param name="content">需要加密的文字内容</param>
145 /// <returns>加密后的文字内容,当出现错误时返回null</returns>
146 public string Encrypt(string content)
147 {
148 try
149 {
150 if (IsEmpty(content) || IsKeyError(bytDesKey) || IsKeyError(bytDesIV))
151 {
152 return null;
153 }
154 byte[] bInContent = Encoding.ASCII.GetBytes(content.PadRight(50, this.chrFillChar));
155 if (IsOverflow(bInContent, DecryptLength))
156 {
157 return null;
158 }
159 byte[] bOutContent = new byte[EncryptLength];
160 MemoryStream memoryStream = new MemoryStream(bOutContent);
161 TripleDESCryptoServiceProvider provider = new TripleDESCryptoServiceProvider();
162 using (CryptoStream cryptoStream =
163 new CryptoStream(memoryStream, provider.CreateEncryptor(bytDesKey, bytDesIV), CryptoStreamMode.Write))
164 {
165 cryptoStream.Write(bInContent, 0, bInContent.Length);
166 cryptoStream.Close();
167 }
168 return Convert.ToBase64String(bOutContent);
169 }
170 catch (Exception ex)
171 {
172 strErrorMessage = ex.Message;
173 return null;
174 }
175 }
176
177 /// <summary>
178 /// 使用TripleDES解密
179 /// </summary>
180 /// <param name="content">需要解密的文字内容</param>
181 /// <returns>解密后的文字内容,当出现错误时返回null</returns>
182 public string Decrypt(string content)
183 {
184 try
185 {
186 if (IsEmpty(content) || IsKeyError(bytDesKey) || IsKeyError(bytDesIV))
187 {
188 return null;
189 }
190 byte[] bInContent = Convert.FromBase64String(content);
191 if (IsOverflow(bInContent, EncryptLength))
192 {
193 return null;
194 }
195 byte[] bOutContent = new byte[DecryptLength];
196 MemoryStream memoryStream = new MemoryStream(bInContent);
197 TripleDESCryptoServiceProvider provider = new TripleDESCryptoServiceProvider();
198 using (CryptoStream cryptoStream
199 = new CryptoStream(memoryStream, provider.CreateDecryptor(bytDesKey, bytDesIV), CryptoStreamMode.Read))
200 {
201 cryptoStream.Read(bOutContent, 0, bOutContent.Length);
202 cryptoStream.Close();
203 }
204 return Encoding.ASCII.GetString(bOutContent).TrimEnd(this.chrFillChar);
205 }
206 catch (Exception ex)
207 {
208 strErrorMessage = ex.Message;
209 return null;
210 }
211 }
212
213 #endregion
214
215 私有方法
273 }
274}
275