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 构造函数#region 构造函数
112
113 /**//// <summary>
114 /// TripleDESProcess的构造函数
115 /// </summary>
116 /// <param name="desKey">指定的密钥(Key)</param>
117 /// <param name="desIV">初始化向量(IV)</param>
118 public TripleDESProcess(string desKey, string desIV)
119 : this(desKey, desIV, '\r')
120 {
121 }
122
123 /**//// <summary>
124 /// TripleDESProcess的构造函数
125 /// </summary>
126 /// <param name="desKey">指定的密钥(Key)</param>
127 /// <param name="desIV">初始化向量(IV)</param>
128 /// <param name="fillChar">空白字节的填充字符</param>
129 public TripleDESProcess(string desKey, string desIV, char fillChar)
130 {
131 this.bytDesKey = Encoding.ASCII.GetBytes(desKey);
132 this.bytDesIV = Encoding.ASCII.GetBytes(desIV);
133 this.chrFillChar = fillChar;
134 this.strErrorMessage = "";
135 }
136
137 #endregion
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 私有方法#region 私有方法
216
217 /**//// <summary>
218 /// 检查内容是否为空
219 /// </summary>
220 /// <param name="content">待检查的文字内容</param>
221 /// <returns>当内容为空时返回true,当内容非空时返回false</returns>
222 private bool IsEmpty(string content)
223 {
224 if (content == null || content == string.Empty)
225 {
226 strErrorMessage = "需要加密的文字内容不能为空";
227 return true;
228 }
229 else
230 {
231 return false;
232 }
233 }
234
235 /**//// <summary>
236 /// 检查内容是否溢出
237 /// </summary>
238 /// <param name="content">待检查的文字内容</param>
239 /// <param name="length">内容的最大允许长度</param>
240 /// <returns>当内容溢出时返回true,当内容正常时返回false</returns>
241 private bool IsOverflow(byte[] content, int length)
242 {
243 if (content.Length > length)
244 {
245 strErrorMessage = "需要加密的文字内容最大长度为" + length.ToString() + "个字节";
246 return true;
247 }
248 else
249 {
250 return false;
251 }
252 }
253
254 /**//// <summary>
255 /// 检查加密用的【指定的密钥(Key)】和【初始化向量(IV)】是否都满足16位的长度
256 /// </summary>
257 /// <param name="key">需要检查的key</param>
258 /// <returns>当key的长度错误时返回true,当key的长度正确时返回false</returns>
259 private bool IsKeyError(byte[] key)
260 {
261 if (key.Length != KeyLength)
262 {
263 strErrorMessage = "请检查【指定的密钥(Key)】和【初始化向量(IV)】的长度是否都为" + KeyLength + "位";
264 return true;
265 }
266 else
267 {
268 return false;
269 }
270 }
271
272 #endregion
273 }
274}
275