哈希加密:
MD5:
SHA1:
可逆加密算法:
DES:
代码
MD5:
byte[] data = new byte[DATA_SIZE];
// This is one implementation of the abstract class MD5.
MD5 md5 = new MD5CryptoServiceProvider();
byte[] result = md5.ComputeHash(data);
// This is one implementation of the abstract class MD5.
MD5 md5 = new MD5CryptoServiceProvider();
byte[] result = md5.ComputeHash(data);
SHA1:
byte[] data = new byte[DATA_SIZE];
byte[] result;
SHA1 sha = new SHA1CryptoServiceProvider();
// This is one implementation of the abstract class SHA1.
result = sha.ComputeHash(data);
byte[] result;
SHA1 sha = new SHA1CryptoServiceProvider();
// This is one implementation of the abstract class SHA1.
result = sha.ComputeHash(data);
可逆加密算法:
DES:
private static void EncryptData(String inName, String outName, byte[] desKey, byte[] desIV)
{
//Create the file streams to handle the input and output files.
FileStream fin = new FileStream(inName, FileMode.Open, FileAccess.Read);
FileStream fout = new FileStream(outName, FileMode.OpenOrCreate, FileAccess.Write);
fout.SetLength(0);
//Create variables to help with read and write.
byte[] bin = new byte[100]; //This is intermediate storage for the encryption.
long rdlen = 0; //This is the total number of bytes written.
long totlen = fin.Length; //This is the total length of the input file.
int len; //This is the number of bytes to be written at a time.
DES des = new DESCryptoServiceProvider();
CryptoStream encStream = new CryptoStream(fout, des.CreateEncryptor(desKey, desIV), CryptoStreamMode.Write);
Console.WriteLine("Encrypting
");
//Read from the input file, then encrypt and write to the output file.
while(rdlen < totlen)
{
len = fin.Read(bin, 0, 100);
encStream.Write(bin, 0, len);
rdlen = rdlen + len;
Console.WriteLine("{0} bytes processed", rdlen);
}
encStream.Close();
fout.Close();
fin.Close();
}
{
//Create the file streams to handle the input and output files.
FileStream fin = new FileStream(inName, FileMode.Open, FileAccess.Read);
FileStream fout = new FileStream(outName, FileMode.OpenOrCreate, FileAccess.Write);
fout.SetLength(0);
//Create variables to help with read and write.
byte[] bin = new byte[100]; //This is intermediate storage for the encryption.
long rdlen = 0; //This is the total number of bytes written.
long totlen = fin.Length; //This is the total length of the input file.
int len; //This is the number of bytes to be written at a time.
DES des = new DESCryptoServiceProvider();
CryptoStream encStream = new CryptoStream(fout, des.CreateEncryptor(desKey, desIV), CryptoStreamMode.Write);
Console.WriteLine("Encrypting
");//Read from the input file, then encrypt and write to the output file.
while(rdlen < totlen)
{
len = fin.Read(bin, 0, 100);
encStream.Write(bin, 0, len);
rdlen = rdlen + len;
Console.WriteLine("{0} bytes processed", rdlen);
}
encStream.Close();
fout.Close();
fin.Close();
}
代码 1 using System;
2 using System.Collections.Generic;
3 using System.Text;
4 using System.IO;
5 using System.Security.Cryptography;
6
7 namespace DevSDK.Security
8 {
9 /// <summary>
10 /// 软创加密类
11 /// </summary>
12 public static class Cryptography
13 {
14 /// <summary>
15 /// MD5 加密静态方法
16 /// </summary>
17 /// <param name="EncryptString">待加密的密文</param>
18 /// <returns>returns</returns>
19 public static string MD5Encrypt(string EncryptString)
20 {
21 if (string.IsNullOrEmpty(EncryptString)) { throw (new Exception("密文不得为空")); }
22
23 MD5 m_ClassMD5 = new MD5CryptoServiceProvider();
24
25 string m_strEncrypt = "";
26
27 try
28 {
29 m_strEncrypt = BitConverter.ToString(m_ClassMD5.ComputeHash(Encoding.Default.GetBytes(EncryptString))).Replace("-", "");
30 }
31 catch (ArgumentException ex) { throw ex; }
32 catch (CryptographicException ex) { throw ex; }
33 catch (Exception ex) { throw ex; }
34 finally { m_ClassMD5.Clear(); }
35
36 return m_strEncrypt;
37 }
38 /// <summary>
39 /// DES 加密(数据加密标准,速度较快,适用于加密大量数据的场合)
40 /// </summary>
41 /// <param name="EncryptString">待加密的密文</param>
42 /// <param name="EncryptKey">加密的密钥</param>
43 /// <returns>returns</returns>
44 public static string DESEncrypt(string EncryptString, string EncryptKey)
45 {
46 if (string.IsNullOrEmpty(EncryptString)) { throw (new Exception("密文不得为空")); }
47
48 if (string.IsNullOrEmpty(EncryptKey)) { throw (new Exception("密钥不得为空")); }
49
50 if (EncryptKey.Length != 8) { throw (new Exception("密钥必须为8位")); }
51
52 byte[] m_btIV = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
53
54 string m_strEncrypt = "";
55
56 DESCryptoServiceProvider m_DESProvider = new DESCryptoServiceProvider();
57
58 try
59 {
60 byte[] m_btEncryptString = Encoding.Default.GetBytes(EncryptString);
61
62 MemoryStream m_stream = new MemoryStream();
63
64 CryptoStream m_cstream = new CryptoStream(m_stream, m_DESProvider.CreateEncryptor(Encoding.Default.GetBytes(EncryptKey), m_btIV), CryptoStreamMode.Write);
65
66 m_cstream.Write(m_btEncryptString, 0, m_btEncryptString.Length);
67
68 m_cstream.FlushFinalBlock();
69
70 m_strEncrypt = Convert.ToBase64String(m_stream.ToArray());
71
72 m_stream.Close(); m_stream.Dispose();
73
74 m_cstream.Close(); m_cstream.Dispose();
75 }
76 catch (IOException ex) { throw ex; }
77 catch (CryptographicException ex) { throw ex; }
78 catch (ArgumentException ex) { throw ex; }
79 catch (Exception ex) { throw ex; }
80 finally { m_DESProvider.Clear(); }
81
82 return m_strEncrypt;
83 }
84 /// <summary>
85 /// DES 解密(数据加密标准,速度较快,适用于加密大量数据的场合)
86 /// </summary>
87 /// <param name="DecryptString">待解密的密文</param>
88 /// <param name="DecryptKey">解密的密钥</param>
89 /// <returns>returns</returns>
90 public static string DESDecrypt(string DecryptString, string DecryptKey)
91 {
92 if (string.IsNullOrEmpty(DecryptString)) { throw (new Exception("密文不得为空")); }
93
94 if (string.IsNullOrEmpty(DecryptKey)) { throw (new Exception("密钥不得为空")); }
95
96 if (DecryptKey.Length != 8) { throw (new Exception("密钥必须为8位")); }
97
98 byte[] m_btIV = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
99
100 string m_strDecrypt = "";
101
102 DESCryptoServiceProvider m_DESProvider = new DESCryptoServiceProvider();
103
104 try
105 {
106 byte[] m_btDecryptString = Convert.FromBase64String(DecryptString);
107
108 MemoryStream m_stream = new MemoryStream();
109
110 CryptoStream m_cstream = new CryptoStream(m_stream, m_DESProvider.CreateDecryptor(Encoding.Default.GetBytes(DecryptKey), m_btIV), CryptoStreamMode.Write);
111
112 m_cstream.Write(m_btDecryptString, 0, m_btDecryptString.Length);
113
114 m_cstream.FlushFinalBlock();
115
116 m_strDecrypt = Encoding.Default.GetString(m_stream.ToArray());
117
118 m_stream.Close(); m_stream.Dispose();
119
120 m_cstream.Close(); m_cstream.Dispose();
121 }
122 catch (IOException ex) { throw ex; }
123 catch (CryptographicException ex) { throw ex; }
124 catch (ArgumentException ex) { throw ex; }
125 catch (Exception ex) { throw ex; }
126 finally { m_DESProvider.Clear(); }
127
128 return m_strDecrypt;
129 }
130 /// <summary>
131 /// RC2 加密(用变长密钥对大量数据进行加密)
132 /// </summary>
133 /// <param name="EncryptString">待加密密文</param>
134 /// <param name="EncryptKey">加密密钥</param>
135 /// <returns>returns</returns>
136 public static string RC2Encrypt(string EncryptString, string EncryptKey)
137 {
138 if (string.IsNullOrEmpty(EncryptString)) { throw (new Exception("密文不得为空")); }
139
140 if (string.IsNullOrEmpty(EncryptKey)) { throw (new Exception("密钥不得为空")); }
141
142 if (EncryptKey.Length < 5 || EncryptKey.Length > 16) { throw (new Exception("密钥必须为5-16位")); }
143
144 string m_strEncrypt = "";
145
146 byte[] m_btIV = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
147
148 RC2CryptoServiceProvider m_RC2Provider = new RC2CryptoServiceProvider();
149
150 try
151 {
152 byte[] m_btEncryptString = Encoding.Default.GetBytes(EncryptString);
153
154 MemoryStream m_stream = new MemoryStream();
155
156 CryptoStream m_cstream = new CryptoStream(m_stream, m_RC2Provider.CreateEncryptor(Encoding.Default.GetBytes(EncryptKey), m_btIV), CryptoStreamMode.Write);
157
158 m_cstream.Write(m_btEncryptString, 0, m_btEncryptString.Length);
159
160 m_cstream.FlushFinalBlock();
161
162 m_strEncrypt = Convert.ToBase64String(m_stream.ToArray());
163
164 m_stream.Close(); m_stream.Dispose();
165
166 m_cstream.Close(); m_cstream.Dispose();
167 }
168 catch (IOException ex) { throw ex; }
169 catch (CryptographicException ex) { throw ex; }
170 catch (ArgumentException ex) { throw ex; }
171 catch (Exception ex) { throw ex; }
172 finally { m_RC2Provider.Clear(); }
173
174 return m_strEncrypt;
175 }
176 /// <summary>
177 /// RC2 解密(用变长密钥对大量数据进行加密)
178 /// </summary>
179 /// <param name="DecryptString">待解密密文</param>
180 /// <param name="DecryptKey">解密密钥</param>
181 /// <returns>returns</returns>
182 public static string RC2Decrypt(string DecryptString, string DecryptKey)
183 {
184 if (string.IsNullOrEmpty(DecryptString)) { throw (new Exception("密文不得为空")); }
185
186 if (string.IsNullOrEmpty(DecryptKey)) { throw (new Exception("密钥不得为空")); }
187
188 if (DecryptKey.Length < 5 || DecryptKey.Length > 16) { throw (new Exception("密钥必须为5-16位")); }
189
190 byte[] m_btIV = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
191
192 string m_strDecrypt = "";
193
194 RC2CryptoServiceProvider m_RC2Provider = new RC2CryptoServiceProvider();
195
196 try
197 {
198 byte[] m_btDecryptString = Convert.FromBase64String(DecryptString);
199
200 MemoryStream m_stream = new MemoryStream();
201
202 CryptoStream m_cstream = new CryptoStream(m_stream, m_RC2Provider.CreateDecryptor(Encoding.Default.GetBytes(DecryptKey), m_btIV), CryptoStreamMode.Write);
203
204 m_cstream.Write(m_btDecryptString, 0, m_btDecryptString.Length);
205
206 m_cstream.FlushFinalBlock();
207
208 m_strDecrypt = Encoding.Default.GetString(m_stream.ToArray());
209
210 m_stream.Close(); m_stream.Dispose();
211
212 m_cstream.Close(); m_cstream.Dispose();
213 }
214 catch (IOException ex) { throw ex; }
215 catch (CryptographicException ex) { throw ex; }
216 catch (ArgumentException ex) { throw ex; }
217 catch (Exception ex) { throw ex; }
218 finally { m_RC2Provider.Clear(); }
219
220 return m_strDecrypt;
221 }
222 /// <summary>
223 /// 3DES 加密(基于DES,对一块数据用三个不同的密钥进行三次加密,强度更高)
224 /// </summary>
225 /// <param name="EncryptString">待加密密文</param>
226 /// <param name="EncryptKey1">密钥一</param>
227 /// <param name="EncryptKey2">密钥二</param>
228 /// <param name="EncryptKey3">密钥三</param>
229 /// <returns>returns</returns>
230 public static string DES3Encrypt(string EncryptString, string EncryptKey1, string EncryptKey2, string EncryptKey3)
231 {
232 string m_strEncrypt = "";
233
234 try
235 {
236 m_strEncrypt = DESEncrypt(EncryptString, EncryptKey3);
237
238 m_strEncrypt = DESEncrypt(m_strEncrypt, EncryptKey2);
239
240 m_strEncrypt = DESEncrypt(m_strEncrypt, EncryptKey1);
241 }
242 catch (Exception ex) { throw ex; }
243
244 return m_strEncrypt;
245 }
246 /// <summary>
247 /// 3DES 解密(基于DES,对一块数据用三个不同的密钥进行三次加密,强度更高)
248 /// </summary>
249 /// <param name="DecryptString">待解密密文</param>
250 /// <param name="DecryptKey1">密钥一</param>
251 /// <param name="DecryptKey2">密钥二</param>
252 /// <param name="DecryptKey3">密钥三</param>
253 /// <returns>returns</returns>
254 public static string DES3Decrypt(string DecryptString, string DecryptKey1, string DecryptKey2, string DecryptKey3)
255 {
256 string m_strDecrypt = "";
257
258 try
259 {
260 m_strDecrypt = DESDecrypt(DecryptString, DecryptKey1);
261
262 m_strDecrypt = DESDecrypt(m_strDecrypt, DecryptKey2);
263
264 m_strDecrypt = DESDecrypt(m_strDecrypt, DecryptKey3);
265 }
266 catch (Exception ex) { throw ex; }
267
268 return m_strDecrypt;
269 }
270 /// <summary>
271 /// AES 加密(高级加密标准,是下一代的加密算法标准,速度快,安全级别高,目前 AES 标准的一个实现是 Rijndael 算法)
272 /// </summary>
273 /// <param name="EncryptString">待加密密文</param>
274 /// <param name="EncryptKey">加密密钥</param>
275 /// <returns></returns>
276 public static string AESEncrypt(string EncryptString, string EncryptKey)
277 {
278 if (string.IsNullOrEmpty(EncryptString)) { throw (new Exception("密文不得为空")); }
279
280 if (string.IsNullOrEmpty(EncryptKey)) { throw (new Exception("密钥不得为空")); }
281
282 string m_strEncrypt = "";
283
284 byte[] m_btIV = Convert.FromBase64String("Rkb4jvUy/ye7Cd7k89QQgQ==");
285
286 Rijndael m_AESProvider = Rijndael.Create();
287
288 try
289 {
290 byte[] m_btEncryptString = Encoding.Default.GetBytes(EncryptString);
291
292 MemoryStream m_stream = new MemoryStream();
293
294 CryptoStream m_csstream = new CryptoStream(m_stream, m_AESProvider.CreateEncryptor(Encoding.Default.GetBytes(EncryptKey), m_btIV), CryptoStreamMode.Write);
295
296 m_csstream.Write(m_btEncryptString, 0, m_btEncryptString.Length); m_csstream.FlushFinalBlock();
297
298 m_strEncrypt = Convert.ToBase64String(m_stream.ToArray());
299
300 m_stream.Close(); m_stream.Dispose();
301
302 m_csstream.Close(); m_csstream.Dispose();
303 }
304 catch (IOException ex) { throw ex; }
305 catch (CryptographicException ex) { throw ex; }
306 catch (ArgumentException ex) { throw ex; }
307 catch (Exception ex) { throw ex; }
308 finally { m_AESProvider.Clear(); }
309
310 return m_strEncrypt;
311 }
312 /// <summary>
313 /// AES 解密(高级加密标准,是下一代的加密算法标准,速度快,安全级别高,目前 AES 标准的一个实现是 Rijndael 算法)
314 /// </summary>
315 /// <param name="DecryptString">待解密密文</param>
316 /// <param name="DecryptKey">解密密钥</param>
317 /// <returns></returns>
318 public static string AESDecrypt(string DecryptString, string DecryptKey)
319 {
320 if (string.IsNullOrEmpty(DecryptString)) { throw (new Exception("密文不得为空")); }
321
322 if (string.IsNullOrEmpty(DecryptKey)) { throw (new Exception("密钥不得为空")); }
323
324 string m_strDecrypt = "";
325
326 byte[] m_btIV = Convert.FromBase64String("Rkb4jvUy/ye7Cd7k89QQgQ==");
327
328 Rijndael m_AESProvider = Rijndael.Create();
329
330 try
331 {
332 byte[] m_btDecryptString = Convert.FromBase64String(DecryptString);
333
334 MemoryStream m_stream = new MemoryStream();
335
336 CryptoStream m_csstream = new CryptoStream(m_stream, m_AESProvider.CreateDecryptor(Encoding.Default.GetBytes(DecryptKey), m_btIV), CryptoStreamMode.Write);
337
338 m_csstream.Write(m_btDecryptString, 0, m_btDecryptString.Length); m_csstream.FlushFinalBlock();
339
340 m_strDecrypt = Encoding.Default.GetString(m_stream.ToArray());
341
342 m_stream.Close(); m_stream.Dispose();
343
344 m_csstream.Close(); m_csstream.Dispose();
345 }
346 catch (IOException ex) { throw ex; }
347 catch (CryptographicException ex) { throw ex; }
348 catch (ArgumentException ex) { throw ex; }
349 catch (Exception ex) { throw ex; }
350 finally { m_AESProvider.Clear(); }
351
352 return m_strDecrypt;
353 }
354 }
355 }
2 using System.Collections.Generic;
3 using System.Text;
4 using System.IO;
5 using System.Security.Cryptography;
6
7 namespace DevSDK.Security
8 {
9 /// <summary>
10 /// 软创加密类
11 /// </summary>
12 public static class Cryptography
13 {
14 /// <summary>
15 /// MD5 加密静态方法
16 /// </summary>
17 /// <param name="EncryptString">待加密的密文</param>
18 /// <returns>returns</returns>
19 public static string MD5Encrypt(string EncryptString)
20 {
21 if (string.IsNullOrEmpty(EncryptString)) { throw (new Exception("密文不得为空")); }
22
23 MD5 m_ClassMD5 = new MD5CryptoServiceProvider();
24
25 string m_strEncrypt = "";
26
27 try
28 {
29 m_strEncrypt = BitConverter.ToString(m_ClassMD5.ComputeHash(Encoding.Default.GetBytes(EncryptString))).Replace("-", "");
30 }
31 catch (ArgumentException ex) { throw ex; }
32 catch (CryptographicException ex) { throw ex; }
33 catch (Exception ex) { throw ex; }
34 finally { m_ClassMD5.Clear(); }
35
36 return m_strEncrypt;
37 }
38 /// <summary>
39 /// DES 加密(数据加密标准,速度较快,适用于加密大量数据的场合)
40 /// </summary>
41 /// <param name="EncryptString">待加密的密文</param>
42 /// <param name="EncryptKey">加密的密钥</param>
43 /// <returns>returns</returns>
44 public static string DESEncrypt(string EncryptString, string EncryptKey)
45 {
46 if (string.IsNullOrEmpty(EncryptString)) { throw (new Exception("密文不得为空")); }
47
48 if (string.IsNullOrEmpty(EncryptKey)) { throw (new Exception("密钥不得为空")); }
49
50 if (EncryptKey.Length != 8) { throw (new Exception("密钥必须为8位")); }
51
52 byte[] m_btIV = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
53
54 string m_strEncrypt = "";
55
56 DESCryptoServiceProvider m_DESProvider = new DESCryptoServiceProvider();
57
58 try
59 {
60 byte[] m_btEncryptString = Encoding.Default.GetBytes(EncryptString);
61
62 MemoryStream m_stream = new MemoryStream();
63
64 CryptoStream m_cstream = new CryptoStream(m_stream, m_DESProvider.CreateEncryptor(Encoding.Default.GetBytes(EncryptKey), m_btIV), CryptoStreamMode.Write);
65
66 m_cstream.Write(m_btEncryptString, 0, m_btEncryptString.Length);
67
68 m_cstream.FlushFinalBlock();
69
70 m_strEncrypt = Convert.ToBase64String(m_stream.ToArray());
71
72 m_stream.Close(); m_stream.Dispose();
73
74 m_cstream.Close(); m_cstream.Dispose();
75 }
76 catch (IOException ex) { throw ex; }
77 catch (CryptographicException ex) { throw ex; }
78 catch (ArgumentException ex) { throw ex; }
79 catch (Exception ex) { throw ex; }
80 finally { m_DESProvider.Clear(); }
81
82 return m_strEncrypt;
83 }
84 /// <summary>
85 /// DES 解密(数据加密标准,速度较快,适用于加密大量数据的场合)
86 /// </summary>
87 /// <param name="DecryptString">待解密的密文</param>
88 /// <param name="DecryptKey">解密的密钥</param>
89 /// <returns>returns</returns>
90 public static string DESDecrypt(string DecryptString, string DecryptKey)
91 {
92 if (string.IsNullOrEmpty(DecryptString)) { throw (new Exception("密文不得为空")); }
93
94 if (string.IsNullOrEmpty(DecryptKey)) { throw (new Exception("密钥不得为空")); }
95
96 if (DecryptKey.Length != 8) { throw (new Exception("密钥必须为8位")); }
97
98 byte[] m_btIV = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
99
100 string m_strDecrypt = "";
101
102 DESCryptoServiceProvider m_DESProvider = new DESCryptoServiceProvider();
103
104 try
105 {
106 byte[] m_btDecryptString = Convert.FromBase64String(DecryptString);
107
108 MemoryStream m_stream = new MemoryStream();
109
110 CryptoStream m_cstream = new CryptoStream(m_stream, m_DESProvider.CreateDecryptor(Encoding.Default.GetBytes(DecryptKey), m_btIV), CryptoStreamMode.Write);
111
112 m_cstream.Write(m_btDecryptString, 0, m_btDecryptString.Length);
113
114 m_cstream.FlushFinalBlock();
115
116 m_strDecrypt = Encoding.Default.GetString(m_stream.ToArray());
117
118 m_stream.Close(); m_stream.Dispose();
119
120 m_cstream.Close(); m_cstream.Dispose();
121 }
122 catch (IOException ex) { throw ex; }
123 catch (CryptographicException ex) { throw ex; }
124 catch (ArgumentException ex) { throw ex; }
125 catch (Exception ex) { throw ex; }
126 finally { m_DESProvider.Clear(); }
127
128 return m_strDecrypt;
129 }
130 /// <summary>
131 /// RC2 加密(用变长密钥对大量数据进行加密)
132 /// </summary>
133 /// <param name="EncryptString">待加密密文</param>
134 /// <param name="EncryptKey">加密密钥</param>
135 /// <returns>returns</returns>
136 public static string RC2Encrypt(string EncryptString, string EncryptKey)
137 {
138 if (string.IsNullOrEmpty(EncryptString)) { throw (new Exception("密文不得为空")); }
139
140 if (string.IsNullOrEmpty(EncryptKey)) { throw (new Exception("密钥不得为空")); }
141
142 if (EncryptKey.Length < 5 || EncryptKey.Length > 16) { throw (new Exception("密钥必须为5-16位")); }
143
144 string m_strEncrypt = "";
145
146 byte[] m_btIV = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
147
148 RC2CryptoServiceProvider m_RC2Provider = new RC2CryptoServiceProvider();
149
150 try
151 {
152 byte[] m_btEncryptString = Encoding.Default.GetBytes(EncryptString);
153
154 MemoryStream m_stream = new MemoryStream();
155
156 CryptoStream m_cstream = new CryptoStream(m_stream, m_RC2Provider.CreateEncryptor(Encoding.Default.GetBytes(EncryptKey), m_btIV), CryptoStreamMode.Write);
157
158 m_cstream.Write(m_btEncryptString, 0, m_btEncryptString.Length);
159
160 m_cstream.FlushFinalBlock();
161
162 m_strEncrypt = Convert.ToBase64String(m_stream.ToArray());
163
164 m_stream.Close(); m_stream.Dispose();
165
166 m_cstream.Close(); m_cstream.Dispose();
167 }
168 catch (IOException ex) { throw ex; }
169 catch (CryptographicException ex) { throw ex; }
170 catch (ArgumentException ex) { throw ex; }
171 catch (Exception ex) { throw ex; }
172 finally { m_RC2Provider.Clear(); }
173
174 return m_strEncrypt;
175 }
176 /// <summary>
177 /// RC2 解密(用变长密钥对大量数据进行加密)
178 /// </summary>
179 /// <param name="DecryptString">待解密密文</param>
180 /// <param name="DecryptKey">解密密钥</param>
181 /// <returns>returns</returns>
182 public static string RC2Decrypt(string DecryptString, string DecryptKey)
183 {
184 if (string.IsNullOrEmpty(DecryptString)) { throw (new Exception("密文不得为空")); }
185
186 if (string.IsNullOrEmpty(DecryptKey)) { throw (new Exception("密钥不得为空")); }
187
188 if (DecryptKey.Length < 5 || DecryptKey.Length > 16) { throw (new Exception("密钥必须为5-16位")); }
189
190 byte[] m_btIV = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
191
192 string m_strDecrypt = "";
193
194 RC2CryptoServiceProvider m_RC2Provider = new RC2CryptoServiceProvider();
195
196 try
197 {
198 byte[] m_btDecryptString = Convert.FromBase64String(DecryptString);
199
200 MemoryStream m_stream = new MemoryStream();
201
202 CryptoStream m_cstream = new CryptoStream(m_stream, m_RC2Provider.CreateDecryptor(Encoding.Default.GetBytes(DecryptKey), m_btIV), CryptoStreamMode.Write);
203
204 m_cstream.Write(m_btDecryptString, 0, m_btDecryptString.Length);
205
206 m_cstream.FlushFinalBlock();
207
208 m_strDecrypt = Encoding.Default.GetString(m_stream.ToArray());
209
210 m_stream.Close(); m_stream.Dispose();
211
212 m_cstream.Close(); m_cstream.Dispose();
213 }
214 catch (IOException ex) { throw ex; }
215 catch (CryptographicException ex) { throw ex; }
216 catch (ArgumentException ex) { throw ex; }
217 catch (Exception ex) { throw ex; }
218 finally { m_RC2Provider.Clear(); }
219
220 return m_strDecrypt;
221 }
222 /// <summary>
223 /// 3DES 加密(基于DES,对一块数据用三个不同的密钥进行三次加密,强度更高)
224 /// </summary>
225 /// <param name="EncryptString">待加密密文</param>
226 /// <param name="EncryptKey1">密钥一</param>
227 /// <param name="EncryptKey2">密钥二</param>
228 /// <param name="EncryptKey3">密钥三</param>
229 /// <returns>returns</returns>
230 public static string DES3Encrypt(string EncryptString, string EncryptKey1, string EncryptKey2, string EncryptKey3)
231 {
232 string m_strEncrypt = "";
233
234 try
235 {
236 m_strEncrypt = DESEncrypt(EncryptString, EncryptKey3);
237
238 m_strEncrypt = DESEncrypt(m_strEncrypt, EncryptKey2);
239
240 m_strEncrypt = DESEncrypt(m_strEncrypt, EncryptKey1);
241 }
242 catch (Exception ex) { throw ex; }
243
244 return m_strEncrypt;
245 }
246 /// <summary>
247 /// 3DES 解密(基于DES,对一块数据用三个不同的密钥进行三次加密,强度更高)
248 /// </summary>
249 /// <param name="DecryptString">待解密密文</param>
250 /// <param name="DecryptKey1">密钥一</param>
251 /// <param name="DecryptKey2">密钥二</param>
252 /// <param name="DecryptKey3">密钥三</param>
253 /// <returns>returns</returns>
254 public static string DES3Decrypt(string DecryptString, string DecryptKey1, string DecryptKey2, string DecryptKey3)
255 {
256 string m_strDecrypt = "";
257
258 try
259 {
260 m_strDecrypt = DESDecrypt(DecryptString, DecryptKey1);
261
262 m_strDecrypt = DESDecrypt(m_strDecrypt, DecryptKey2);
263
264 m_strDecrypt = DESDecrypt(m_strDecrypt, DecryptKey3);
265 }
266 catch (Exception ex) { throw ex; }
267
268 return m_strDecrypt;
269 }
270 /// <summary>
271 /// AES 加密(高级加密标准,是下一代的加密算法标准,速度快,安全级别高,目前 AES 标准的一个实现是 Rijndael 算法)
272 /// </summary>
273 /// <param name="EncryptString">待加密密文</param>
274 /// <param name="EncryptKey">加密密钥</param>
275 /// <returns></returns>
276 public static string AESEncrypt(string EncryptString, string EncryptKey)
277 {
278 if (string.IsNullOrEmpty(EncryptString)) { throw (new Exception("密文不得为空")); }
279
280 if (string.IsNullOrEmpty(EncryptKey)) { throw (new Exception("密钥不得为空")); }
281
282 string m_strEncrypt = "";
283
284 byte[] m_btIV = Convert.FromBase64String("Rkb4jvUy/ye7Cd7k89QQgQ==");
285
286 Rijndael m_AESProvider = Rijndael.Create();
287
288 try
289 {
290 byte[] m_btEncryptString = Encoding.Default.GetBytes(EncryptString);
291
292 MemoryStream m_stream = new MemoryStream();
293
294 CryptoStream m_csstream = new CryptoStream(m_stream, m_AESProvider.CreateEncryptor(Encoding.Default.GetBytes(EncryptKey), m_btIV), CryptoStreamMode.Write);
295
296 m_csstream.Write(m_btEncryptString, 0, m_btEncryptString.Length); m_csstream.FlushFinalBlock();
297
298 m_strEncrypt = Convert.ToBase64String(m_stream.ToArray());
299
300 m_stream.Close(); m_stream.Dispose();
301
302 m_csstream.Close(); m_csstream.Dispose();
303 }
304 catch (IOException ex) { throw ex; }
305 catch (CryptographicException ex) { throw ex; }
306 catch (ArgumentException ex) { throw ex; }
307 catch (Exception ex) { throw ex; }
308 finally { m_AESProvider.Clear(); }
309
310 return m_strEncrypt;
311 }
312 /// <summary>
313 /// AES 解密(高级加密标准,是下一代的加密算法标准,速度快,安全级别高,目前 AES 标准的一个实现是 Rijndael 算法)
314 /// </summary>
315 /// <param name="DecryptString">待解密密文</param>
316 /// <param name="DecryptKey">解密密钥</param>
317 /// <returns></returns>
318 public static string AESDecrypt(string DecryptString, string DecryptKey)
319 {
320 if (string.IsNullOrEmpty(DecryptString)) { throw (new Exception("密文不得为空")); }
321
322 if (string.IsNullOrEmpty(DecryptKey)) { throw (new Exception("密钥不得为空")); }
323
324 string m_strDecrypt = "";
325
326 byte[] m_btIV = Convert.FromBase64String("Rkb4jvUy/ye7Cd7k89QQgQ==");
327
328 Rijndael m_AESProvider = Rijndael.Create();
329
330 try
331 {
332 byte[] m_btDecryptString = Convert.FromBase64String(DecryptString);
333
334 MemoryStream m_stream = new MemoryStream();
335
336 CryptoStream m_csstream = new CryptoStream(m_stream, m_AESProvider.CreateDecryptor(Encoding.Default.GetBytes(DecryptKey), m_btIV), CryptoStreamMode.Write);
337
338 m_csstream.Write(m_btDecryptString, 0, m_btDecryptString.Length); m_csstream.FlushFinalBlock();
339
340 m_strDecrypt = Encoding.Default.GetString(m_stream.ToArray());
341
342 m_stream.Close(); m_stream.Dispose();
343
344 m_csstream.Close(); m_csstream.Dispose();
345 }
346 catch (IOException ex) { throw ex; }
347 catch (CryptographicException ex) { throw ex; }
348 catch (ArgumentException ex) { throw ex; }
349 catch (Exception ex) { throw ex; }
350 finally { m_AESProvider.Clear(); }
351
352 return m_strDecrypt;
353 }
354 }
355 }

浙公网安备 33010602011771号