-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathEncryptHelper.cs
More file actions
388 lines (360 loc) · 14.7 KB
/
Copy pathEncryptHelper.cs
File metadata and controls
388 lines (360 loc) · 14.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
/*
* 作用:字符串加密/解密,MD5,AES,DES,BASE64。
* */
using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;
namespace Helper.Core.Library
{
public class EncryptHelper
{
#region 对外公开方法
#region MD5
/// <summary>
/// MD5 加密
/// </summary>
/// <param name="data">需要加密的数据</param>
/// <returns></returns>
public static string MD5(string data)
{
byte[] buffer = System.Text.Encoding.Default.GetBytes(data);
MD5CryptoServiceProvider cryptoServiceProvider = new MD5CryptoServiceProvider();
byte[] hashValue = cryptoServiceProvider.ComputeHash(buffer);
StringBuilder stringBuilder = new StringBuilder();
foreach (byte byteData in hashValue)
{
if (byteData < 16)
{
stringBuilder.Append("0");
}
stringBuilder.Append(byteData.ToString("X"));
}
return stringBuilder.ToString().ToLower();
}
/// <summary>
/// MD5 加密
/// </summary>
/// <param name="data">需要加密的数据</param>
/// <param name="confuseData">混淆字符</param>
/// <param name="encryptTimes">加密次数</param>
/// <returns></returns>
public static string MD5(string data, string confuseData, int encryptTimes = 3)
{
string encryptData = data;
string encryptConfuseData = confuseData;
do
{
encryptTimes--;
encryptData = MD5(encryptData);
encryptConfuseData = MD5(encryptConfuseData);
} while (encryptTimes > 0);
encryptData = encryptData.Insert(0, encryptConfuseData.Substring(24));
encryptData = encryptData.Insert(32, encryptConfuseData.Substring(0, 8));
encryptData = encryptData.Insert(16, encryptConfuseData.Substring(8, 8));
encryptData = encryptData.Insert(32, encryptConfuseData.Substring(16, 8));
return encryptData;
}
#endregion
#region AES 加密/解密
/// <summary>
/// AES 加密
/// </summary>
/// <param name="data">需要加密的数据</param>
/// <param name="key">密钥</param>
/// <returns></returns>
public static string AESEncrypt(string data, string key)
{
MemoryStream memoryStream = null;
CryptoStream cryptoStream = null;
RijndaelManaged rijndaelManaged = null;
try
{
byte[] plainBytes = Encoding.UTF8.GetBytes(data);
Byte[] byteKeys = new Byte[32];
Array.Copy(Encoding.UTF8.GetBytes(key.PadRight(byteKeys.Length)), byteKeys, byteKeys.Length);
string result = null;
using (memoryStream = new MemoryStream())
{
rijndaelManaged = new RijndaelManaged();
rijndaelManaged.Mode = CipherMode.ECB;
rijndaelManaged.Padding = PaddingMode.PKCS7;
rijndaelManaged.KeySize = 128;
rijndaelManaged.Key = byteKeys;
using (cryptoStream = new CryptoStream(memoryStream, rijndaelManaged.CreateEncryptor(), CryptoStreamMode.Write))
{
cryptoStream.Write(plainBytes, 0, plainBytes.Length);
cryptoStream.FlushFinalBlock();
}
result = Convert.ToBase64String(memoryStream.ToArray());
}
return result;
}
catch
{
throw;
}
finally
{
if (cryptoStream != null) cryptoStream.Close();
if (memoryStream != null) memoryStream.Close();
if (rijndaelManaged != null) rijndaelManaged.Clear();
}
}
/// <summary>
/// AES 加密
/// </summary>
/// <param name="data">需要加密的数据</param>
/// <param name="key">密钥</param>
/// <param name="vector">向量</param>
/// <returns></returns>
public static string AESEncrypt(string data, string key, string vector)
{
MemoryStream memoryStream = null;
CryptoStream cryptoStream = null;
Rijndael rijndael = null;
try
{
Byte[] plainBytes = Encoding.UTF8.GetBytes(data);
Byte[] byteKeys = new Byte[32];
Array.Copy(Encoding.UTF8.GetBytes(key.PadRight(byteKeys.Length)), byteKeys, byteKeys.Length);
Byte[] byteVectors = new Byte[16];
Array.Copy(Encoding.UTF8.GetBytes(vector.PadRight(byteVectors.Length)), byteVectors, byteVectors.Length);
Byte[] cryptograph = null;
rijndael = Rijndael.Create();
using (memoryStream = new MemoryStream())
{
using (cryptoStream = new CryptoStream(memoryStream, rijndael.CreateEncryptor(byteKeys, byteVectors), CryptoStreamMode.Write))
{
cryptoStream.Write(plainBytes, 0, plainBytes.Length);
cryptoStream.FlushFinalBlock();
cryptograph = memoryStream.ToArray();
}
}
return Convert.ToBase64String(cryptograph);
}
catch
{
throw;
}
finally
{
if (cryptoStream != null) cryptoStream.Close();
if (memoryStream != null) memoryStream.Close();
if (rijndael != null) rijndael.Clear();
}
}
/// <summary>
/// AES 解密
/// </summary>
/// <param name="data">需要解密的数据</param>
/// <param name="key">密钥</param>
/// <returns></returns>
public static string AESDecrypt(string data, string key)
{
MemoryStream memoryStream = null;
CryptoStream cryptoStream = null;
RijndaelManaged rijndaelManaged = null;
try
{
Byte[] encryptedBytes = Convert.FromBase64String(data);
Byte[] byteKeys = new Byte[32];
Array.Copy(Encoding.UTF8.GetBytes(key.PadRight(byteKeys.Length)), byteKeys, byteKeys.Length);
string result = null;
using (memoryStream = new MemoryStream(encryptedBytes))
{
rijndaelManaged = new RijndaelManaged();
rijndaelManaged.Mode = CipherMode.ECB;
rijndaelManaged.Padding = PaddingMode.PKCS7;
rijndaelManaged.KeySize = 128;
rijndaelManaged.Key = byteKeys;
using (cryptoStream = new CryptoStream(memoryStream, rijndaelManaged.CreateDecryptor(), CryptoStreamMode.Read))
{
byte[] tmp = new byte[encryptedBytes.Length + 32];
int len = cryptoStream.Read(tmp, 0, encryptedBytes.Length + 32);
byte[] ret = new byte[len];
Array.Copy(tmp, 0, ret, 0, len);
result = Encoding.UTF8.GetString(ret);
}
}
return result;
}
catch
{
throw;
}
finally
{
if (cryptoStream != null) cryptoStream.Close();
if (memoryStream != null) memoryStream.Close();
if (rijndaelManaged != null) rijndaelManaged.Clear();
}
}
/// <summary>
/// AES 解密
/// </summary>
/// <param name="data">需要解密的数据</param>
/// <param name="key">密钥</param>
/// <param name="vector">向量</param>
/// <returns></returns>
public static string AESDecrypt(string data, string key, string vector)
{
MemoryStream memoryStream = null;
MemoryStream originalMemory = null;
CryptoStream cryptoStream = null;
Rijndael rijndael = null;
try
{
Byte[] encryptedBytes = Convert.FromBase64String(data);
Byte[] byteKeys = new Byte[32];
Array.Copy(Encoding.UTF8.GetBytes(key.PadRight(byteKeys.Length)), byteKeys, byteKeys.Length);
Byte[] byteVectors = new Byte[16];
Array.Copy(Encoding.UTF8.GetBytes(vector.PadRight(byteVectors.Length)), byteVectors, byteVectors.Length);
Byte[] original = null;
rijndael = Rijndael.Create();
using (memoryStream = new MemoryStream(encryptedBytes))
{
using (cryptoStream = new CryptoStream(memoryStream, rijndael.CreateDecryptor(byteKeys, byteVectors), CryptoStreamMode.Read))
{
using (originalMemory = new MemoryStream())
{
Byte[] Buffer = new Byte[1024];
Int32 readBytes = 0;
while ((readBytes = cryptoStream.Read(Buffer, 0, Buffer.Length)) > 0)
{
originalMemory.Write(Buffer, 0, readBytes);
}
original = originalMemory.ToArray();
}
}
}
return Encoding.UTF8.GetString(original);
}
catch
{
throw;
}
finally
{
if (originalMemory != null) originalMemory.Close();
if (cryptoStream != null) cryptoStream.Close();
if (memoryStream != null) memoryStream.Close();
if (rijndael != null) rijndael.Clear();
}
}
#endregion
#region DES 加密/解密
/// <summary>
/// DES 加密
/// </summary>
/// <param name="data">需要加密的数据</param>
/// <param name="key">密钥(必须 8 位)</param>
/// <param name="vector">向量(必须 8 位)</param>
/// <returns></returns>
public static string DESEncrypt(string data, string key, string vector = null)
{
DESCryptoServiceProvider desCryptoService = null;
MemoryStream memoryStream = null;
CryptoStream cryptoStream = null;
try
{
string result = null;
using(desCryptoService = new DESCryptoServiceProvider())
{
byte[] byteDataList = Encoding.UTF8.GetBytes(data);
desCryptoService.Key = ASCIIEncoding.ASCII.GetBytes(key);
if (string.IsNullOrEmpty(vector)) vector = key;
desCryptoService.IV = ASCIIEncoding.ASCII.GetBytes(vector);
using(memoryStream = new MemoryStream())
{
using(cryptoStream = new CryptoStream(memoryStream, desCryptoService.CreateEncryptor(), CryptoStreamMode.Write))
{
cryptoStream.Write(byteDataList, 0, byteDataList.Length);
cryptoStream.FlushFinalBlock();
}
result = Convert.ToBase64String(memoryStream.ToArray());
}
}
return result;
}
catch
{
throw;
}
finally
{
if (cryptoStream != null) cryptoStream.Close();
if (memoryStream != null) memoryStream.Close();
if (desCryptoService != null) desCryptoService.Clear();
}
}
/// <summary>
/// DES 解密
/// </summary>
/// <param name="data">需要解密的数据</param>
/// <param name="key">密钥(必须 8 位)</param>
/// <param name="vector">向量(必须 8 位)</param>
/// <returns></returns>
public static string DESDecrypt(string data, string key, string vector = null)
{
DESCryptoServiceProvider desCryptoService = null;
MemoryStream memoryStream = null;
CryptoStream cryptoStream = null;
try
{
string result = "";
byte[] byteDataList = Convert.FromBase64String(data);
using (desCryptoService = new DESCryptoServiceProvider())
{
desCryptoService.Key = ASCIIEncoding.ASCII.GetBytes(key);
if (string.IsNullOrEmpty(vector)) vector = key;
desCryptoService.IV = ASCIIEncoding.ASCII.GetBytes(vector);
using (memoryStream = new MemoryStream())
{
using (cryptoStream = new CryptoStream(memoryStream, desCryptoService.CreateDecryptor(), CryptoStreamMode.Write))
{
cryptoStream.Write(byteDataList, 0, byteDataList.Length);
cryptoStream.FlushFinalBlock();
}
result = Encoding.UTF8.GetString(memoryStream.ToArray());
}
}
return result;
}
catch
{
throw;
}
finally
{
if (cryptoStream != null) cryptoStream.Close();
if (memoryStream != null) memoryStream.Close();
if (desCryptoService != null) desCryptoService.Clear();
}
}
#endregion
#region Base64 加密/解密
/// <summary>
/// Base64 加密
/// </summary>
/// <param name="data">要加密的数据</param>
/// <returns></returns>
public static string Base64Encrypt(string data)
{
byte[] byteDataList = Encoding.UTF8.GetBytes(data);
return Convert.ToBase64String(byteDataList, 0, byteDataList.Length);
}
/// <summary>
/// Base64 解密
/// </summary>
/// <param name="data">要解密的数据</param>
/// <returns></returns>
public static string Base64Decrypt(string data)
{
byte[] byteDataList = Convert.FromBase64String(data);
return Encoding.UTF8.GetString(byteDataList);
}
#endregion
#endregion
}
}