源文件下载 des.rar
Code
1using sytem;
2using System.Collections.Generic;
3using System.Text;
4
5using System.Security.Cryptography; //添加使用类
6using System.IO;
7
8/**//*
9 * 本类功能:
10 * Des加密
11 * 返回Des加密字符串
12 * 输出文件加密文件
13 */
14public partial class Des
15{
16 私有成员#region 私有成员
17 /**//**/
18 /**//// <summary>
19 /// 输入字符串
20 /// </summary>
21 private string inputString = null;
22 /**//**/
23 /**//// <summary>
24 /// 输出字符串
25 /// </summary>
26 private string outString = null;
27 /**//**/
28 /**//// <summary>
29 /// 输入文件路径
30 /// </summary>
31 private string inputFilePath = null;
32 /**//**/
33 /**//// <summary>
34 /// 输出文件路径
35 /// </summary>
36 private string outFilePath = null;
37 /**//**/
38 /**//// <summary>
39 /// 加密密钥
40 /// </summary>
41 private string encryptKey = null;
42 /**//**/
43 /**//// <summary>
44 /// 解密密钥
45 /// </summary>
46 private string decryptKey = null;
47 /**//**/
48 /**//// <summary>
49 /// 提示信息
50 /// </summary>
51 private string noteMessage = null;
52 #endregion
53
54 公共属性#region 公共属性
55 /**//**/
56 /**//// <summary>
57 /// 输入字符串
58 /// </summary>
59 public string InputString
60 {
61 get { return inputString; }
62 set { inputString = value; }
63 }
64 /**//**/
65 /**//// <summary>
66 /// 输出字符串
67 /// </summary>
68 public string OutString
69 {
70 get { return outString; }
71 set { outString = value; }
72 }
73 /**//**/
74 /**//// <summary>
75 /// 输入文件路径
76 /// </summary>
77 public string InputFilePath
78 {
79 get { return inputFilePath; }
80 set { inputFilePath = value; }
81 }
82 /**//**/
83 /**//// <summary>
84 /// 输出文件路径
85 /// </summary>
86 public string OutFilePath
87 {
88 get { return outFilePath; }
89 set { outFilePath = value; }
90 }
91 /**//**/
92 /**//// <summary>
93 /// 加密密钥
94 /// </summary>
95 public string EncryptKey
96 {
97 get { return encryptKey; }
98 set { encryptKey = value; }
99 }
100 /**//**/
101 /**//// <summary>
102 /// 解密密钥
103 /// </summary>
104 public string DecryptKey
105 {
106 get { return decryptKey; }
107 set { decryptKey = value; }
108 }
109 /**//**/
110 /**//// <summary>
111 /// 错误信息
112 /// </summary>
113 public string NoteMessage
114 {
115 get { return noteMessage; }
116 set { noteMessage = value; }
117 }
118 #endregion
119
120 构造函数#region 构造函数
121 public Des()
122 {
123 //
124 // TODO: 在此处添加构造函数逻辑
125 //
126 }
127 #endregion
128
129 DES加密字符串#region DES加密字符串
130 /**//**//**//// <summary>
131 /// 加密字符串
132 /// 注意:密钥必须为8位
133 /// </summary>
134 /// <param name="strText">字符串</param>
135 /// <param name="encryptKey">密钥</param>
136 public void DesEncrypt()
137 {
138 byte[] byKey = null;
139 byte[] IV = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
140 try
141 {
142 byKey = System.Text.Encoding.UTF8.GetBytes(this.encryptKey.Substring(0, 8));
143 DESCryptoServiceProvider des = new DESCryptoServiceProvider();
144 byte[] inputByteArray = Encoding.UTF8.GetBytes(this.inputString);
145 MemoryStream ms = new MemoryStream();
146 CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(byKey, IV), CryptoStreamMode.Write);
147 cs.Write(inputByteArray, 0, inputByteArray.Length);
148 cs.FlushFinalBlock();
149 this.outString = Convert.ToBase64String(ms.ToArray());
150 }
151 catch (System.Exception error)
152 {
153 this.noteMessage = error.Message;
154 }
155 }
156
157 public string DesEncrypt(string parStr)
158 {
159 byte[] byKey = null;
160 byte[] IV = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
161 try
162 {
163 byKey = System.Text.Encoding.UTF8.GetBytes(this.encryptKey.Substring(0, 8));
164 DESCryptoServiceProvider des = new DESCryptoServiceProvider();
165 byte[] inputByteArray = Encoding.UTF8.GetBytes(parStr);
166 MemoryStream ms = new MemoryStream();
167 CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(byKey, IV), CryptoStreamMode.Write);
168 cs.Write(inputByteArray, 0, inputByteArray.Length);
169 cs.FlushFinalBlock();
170 this.outString = Convert.ToBase64String(ms.ToArray());
171 }
172 catch (System.Exception error)
173 {
174 this.noteMessage = error.Message;
175 }
176 return outString;
177 }
178
179 public string DesEncrypt(string parStr, string KeyStr)
180 {
181 byte[] byKey = null;
182 byte[] IV = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
183 try
184 {
185 byKey = System.Text.Encoding.UTF8.GetBytes(KeyStr.Substring(0, 8));
186 DESCryptoServiceProvider des = new DESCryptoServiceProvider();
187 byte[] inputByteArray = Encoding.UTF8.GetBytes(parStr);
188 MemoryStream ms = new MemoryStream();
189 CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(byKey, IV), CryptoStreamMode.Write);
190 cs.Write(inputByteArray, 0, inputByteArray.Length);
191 cs.FlushFinalBlock();
192 this.outString = Convert.ToBase64String(ms.ToArray());
193 }
194 catch (System.Exception error)
195 {
196 this.noteMessage = error.Message;
197 }
198 return outString;
199 }
200 #endregion
201
202 DES解密字符串#region DES解密字符串
203 /**//**/
204 /**//// <summary>
205 /// 解密字符串
206 /// </summary>
207 /// <param name="this.inputString">加了密的字符串</param>
208 /// <param name="decryptKey">密钥</param>
209 public void DesDecrypt()
210 {
211 byte[] byKey = null;
212 byte[] IV = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
213 byte[] inputByteArray = new Byte[this.inputString.Length];
214 try
215 {
216 byKey = System.Text.Encoding.UTF8.GetBytes(decryptKey.Substring(0, 8));
217 DESCryptoServiceProvider des = new DESCryptoServiceProvider();
218 inputByteArray = Convert.FromBase64String(this.inputString);
219 MemoryStream ms = new MemoryStream();
220 CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(byKey, IV), CryptoStreamMode.Write);
221 cs.Write(inputByteArray, 0, inputByteArray.Length);
222 cs.FlushFinalBlock();
223 System.Text.Encoding encoding = new System.Text.UTF8Encoding();
224 this.outString = encoding.GetString(ms.ToArray());
225 }
226 catch (System.Exception error)
227 {
228 this.noteMessage = error.Message;
229 }
230 }
231 public string DesDecrypt(string parStr)
232 {
233 byte[] byKey = null;
234 byte[] IV = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
235 byte[] inputByteArray = new Byte[parStr.Length];
236 try
237 {
238 byKey = System.Text.Encoding.UTF8.GetBytes(decryptKey.Substring(0, 8));
239 DESCryptoServiceProvider des = new DESCryptoServiceProvider();
240 inputByteArray = Convert.FromBase64String(parStr);
241 MemoryStream ms = new MemoryStream();
242 CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(byKey, IV), CryptoStreamMode.Write);
243 cs.Write(inputByteArray, 0, inputByteArray.Length);
244 cs.FlushFinalBlock();
245 System.Text.Encoding encoding = new System.Text.UTF8Encoding();
246 this.outString = encoding.GetString(ms.ToArray());
247 }
248 catch (System.Exception error)
249 {
250 this.noteMessage = error.Message;
251 }
252 return outString;
253 }
254 public string DesDecrypt(string parStr, string KeyStr)
255 {
256 byte[] byKey = null;
257 byte[] IV = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
258 byte[] inputByteArray = new Byte[parStr.Length];
259 try
260 {
261 byKey = System.Text.Encoding.UTF8.GetBytes(KeyStr.Substring(0, 8));
262 DESCryptoServiceProvider des = new DESCryptoServiceProvider();
263 inputByteArray = Convert.FromBase64String(parStr);
264 MemoryStream ms = new MemoryStream();
265 CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(byKey, IV), CryptoStreamMode.Write);
266 cs.Write(inputByteArray, 0, inputByteArray.Length);
267 cs.FlushFinalBlock();
268 System.Text.Encoding encoding = new System.Text.UTF8Encoding();
269 this.outString = encoding.GetString(ms.ToArray());
270 }
271 catch (System.Exception error)
272 {
273 this.noteMessage = error.Message;
274 }
275 return outString;
276 }
277 #endregion
278
279 DES加密文件#region DES加密文件
280 /**//**/
281 /**//// <summary>
282 /// DES加密文件
283 /// </summary>
284 /// <param name="this.inputFilePath">源文件路径</param>
285 /// <param name="this.outFilePath">输出文件路径</param>
286 /// <param name="encryptKey">密钥</param>
287 public void FileDesEncrypt()
288 {
289 byte[] byKey = null;
290 byte[] IV = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
291 try
292 {
293 byKey = System.Text.Encoding.UTF8.GetBytes(this.encryptKey.Substring(0, 8));
294 FileStream fin = new FileStream(this.inputFilePath, FileMode.Open, FileAccess.Read);
295 FileStream fout = new FileStream(this.outFilePath, FileMode.OpenOrCreate, FileAccess.Write);
296 fout.SetLength(0);
297 //Create variables to help with read and write.
298 byte[] bin = new byte[100]; //This is intermediate storage for the encryption.
299 long rdlen = 0; //This is the total number of bytes written.
300 long totlen = fin.Length; //This is the total length of the input file.
301 int len; //This is the number of bytes to be written at a time.
302 DESCryptoServiceProvider des = new DESCryptoServiceProvider();
303 CryptoStream encStream = new CryptoStream(fout, des.CreateEncryptor(byKey, IV), CryptoStreamMode.Write);
304
305 //Read from the input file, then encrypt and write to the output file.
306 while (rdlen < totlen)
307 {
308 len = fin.Read(bin, 0, 100);
309 encStream.Write(bin, 0, len);
310 rdlen = rdlen + len;
311 }
312
313 encStream.Close();
314 fout.Close();
315 fin.Close();
316 }
317 catch (System.Exception error)
318 {
319 this.noteMessage = error.Message.ToString();
320 }
321 }
322 #endregion
323
324 DES解密文件#region DES解密文件
325 /**//**/
326 /**//// <summary>
327 /// 解密文件
328 /// </summary>
329 /// <param name="this.inputFilePath">加密了的文件路径</param>
330 /// <param name="this.outFilePath">输出文件路径</param>
331 /// <param name="decryptKey">密钥</param>
332 public void FileDesDecrypt()
333 {
334 byte[] byKey = null;
335 byte[] IV = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
336 try
337 {
338 byKey = System.Text.Encoding.UTF8.GetBytes(decryptKey.Substring(0, 8));
339 FileStream fin = new FileStream(this.inputFilePath, FileMode.Open, FileAccess.Read);
340 FileStream fout = new FileStream(this.outFilePath, FileMode.OpenOrCreate, FileAccess.Write);
341 fout.SetLength(0);
342 //Create variables to help with read and write.
343 byte[] bin = new byte[100]; //This is intermediate storage for the encryption.
344 long rdlen = 0; //This is the total number of bytes written.
345 long totlen = fin.Length; //This is the total length of the input file.
346 int len; //This is the number of bytes to be written at a time.
347 DESCryptoServiceProvider des = new DESCryptoServiceProvider();
348 CryptoStream encStream = new CryptoStream(fout, des.CreateDecryptor(byKey, IV), CryptoStreamMode.Write);
349
350
351 //Read from the input file, then encrypt and write to the output file.
352 while (rdlen < totlen)
353 {
354 len = fin.Read(bin, 0, 100);
355 encStream.Write(bin, 0, len);
356 rdlen = rdlen + len;
357 }
358
359 encStream.Close();
360 fout.Close();
361 fin.Close();
362 }
363 catch (System.Exception error)
364 {
365 this.noteMessage = error.Message.ToString();
366 }
367 }
368 #endregion
369}
370
371