forked from tasos-py/AES-Encryption-Classes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AesEncryption.cs
606 lines (545 loc) · 22.3 KB
/
AesEncryption.cs
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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
using System;
using System.Security.Cryptography;
using System.Text;
using System.IO;
using System.Collections.Generic;
using System.Text.RegularExpressions;
/// <summary>
/// Encrypts data and files using AES CBC/CFB - 128/192/256 bits.
/// </summary>
/// <remarks>
/// The encryption and authentication keys
/// are derived from the supplied key/password using HKDF/PBKDF2.
/// The key can be set either with `SetMasterKey` or with `RandomKeyGen`.
/// Encrypted data format: salt[16] + iv[16] + ciphertext[n] + mac[32].
/// Ciphertext authenticity is verified with HMAC SHA256.
/// </remarks>
/// <note type="caution">CFB is not supported in .NET Core.</note>
class AesEncryption
{
private Dictionary<string, CipherMode> modes = new Dictionary<string, CipherMode>()
{
{ "CBC", CipherMode.CBC }, { "CFB", CipherMode.CFB }
};
private int[] sizes = new int[] { 128, 192, 256 };
private int saltLen = 16;
private int ivLen = 16;
private int macLen = 32;
private int macKeyLen = 32;
private string mode;
private int keyLen;
private byte[] masterkey;
/// The number of PBKDF2 iterations (applies to password based keys).
public int keyIterations = 20000;
/// Accepts ans returns base64 encoded data.
public bool base64 = true;
/// <param name="mode">Optional, the AES mode (CBC or CFB)</param>
/// <param name="size">Optional, the key size (128, 192, 256)</param>
/// <exception cref="ArgumentException">
/// Thrown when mode is not supported or size is invalid.
/// </exception>
public AesEncryption(string mode = "CBC", int size = 128)
{
this.mode = mode.ToUpper();
this.keyLen = size / 8;
if (!modes.ContainsKey(this.mode))
throw new ArgumentException(mode + " is not supported!");
if (Array.IndexOf(sizes, size) == -1)
throw new ArgumentException("Invalid key size!");
}
/// <summary>Encrypts data using a key or the supplied password.</summary>
/// <remarks>
/// The password is not required if a master key has been set
/// (either with `RandomKeyGgen` or with `SetMasterKey`).
/// If a password is supplied, it will be used to create a key with PBKDF2.
/// </remarks>
/// <param name="data">The plaintext.</param>
/// <param name="password">Optional, the password.</param>
/// <returns>Encrypted data (salt + iv + ciphertext + mac).</returns>
public byte[] Encrypt(byte[] data, string password = null)
{
byte[] iv = this.RandomBytes(ivLen);
byte[] salt = this.RandomBytes(saltLen);
try
{
byte[][] keys = this.Keys(salt, password);
byte[] aesKey = keys[0], macKey = keys[1];
byte[] ciphertext;
using (SymmetricAlgorithm cipher = this.Cipher(aesKey, iv))
using (ICryptoTransform ict = cipher.CreateEncryptor())
{
ciphertext = ict.TransformFinalBlock(data, 0, data.Length);
}
byte[] encrypted = new byte[saltLen + ivLen + ciphertext.Length + macLen];
Array.Copy(salt, 0, encrypted, 0, saltLen);
Array.Copy(iv, 0, encrypted, saltLen, ivLen);
Array.Copy(ciphertext, 0, encrypted, saltLen + ivLen, ciphertext.Length);
byte[] iv_ct = new byte[ivLen + ciphertext.Length];
Array.Copy(encrypted, saltLen, iv_ct, 0, iv_ct.Length);
byte[] mac = Sign(iv_ct, macKey);
Array.Copy(mac, 0, encrypted, encrypted.Length - macLen, macLen);
if (this.base64)
return Encoding.ASCII.GetBytes(Convert.ToBase64String(encrypted));
return encrypted;
}
catch (ArgumentException e)
{
this.ErrorHandler(e);
}
catch (CryptographicException e)
{
this.ErrorHandler(e);
}
return null;
}
/// <summary>Encrypts data using a key or the supplied password.</summary>
/// <param name="data">The plaintext.</param>
/// <param name="password">Optional, the password.</param>
/// <returns>Encrypted data (salt + iv + ciphertext + mac).</returns>
public byte[] Encrypt(string data, string password = null)
{
return Encrypt(Encoding.UTF8.GetBytes(data), password);
}
/// <summary>Decrypts data using a key or the supplied password.</summary>
/// <remarks>
/// The password is not required if a master key has been set
/// (either with `RandomKeyGgen` or with `SetMasterKey`).
/// If a password is supplied, it will be used to create a key with PBKDF2.
/// </remarks>
/// <param name="data">The ciphertext (raw of base46-encoded bytes).</param>
/// <param name="password">Optional, the pasword.</param>
/// <returns>Plaintext.</returns>
public byte[] Decrypt(byte[] data, string password = null)
{
try
{
if (this.base64)
data = Convert.FromBase64String((Encoding.ASCII.GetString(data)));
if (data.Length - saltLen - ivLen - macLen < 0)
throw new ArgumentException("Invalid data size!");
byte[] salt = new byte[saltLen];
byte[] iv = new byte[ivLen];
byte[] ciphertext = new byte[data.Length - saltLen - ivLen - macLen];
byte[] mac = new byte[macLen];
Array.Copy(data, 0, salt, 0, saltLen);
Array.Copy(data, saltLen, iv, 0, ivLen);
Array.Copy(data, saltLen + ivLen, ciphertext, 0, ciphertext.Length);
Array.Copy(data, saltLen + ivLen + ciphertext.Length, mac, 0, macLen);
byte[][] keys = this.Keys(salt, password);
byte[] aesKey = keys[0], macKey = keys[1];
byte[] iv_ct = new byte[ivLen + ciphertext.Length];
Array.Copy(data, saltLen, iv_ct, 0, iv_ct.Length);
this.Verify(iv_ct, mac, macKey);
byte[] plaintext;
using (SymmetricAlgorithm cipher = this.Cipher(aesKey, iv))
using (ICryptoTransform ict = cipher.CreateDecryptor())
{
plaintext = ict.TransformFinalBlock(ciphertext, 0, ciphertext.Length);
}
return plaintext;
}
catch (ArgumentException e)
{
this.ErrorHandler(e);
}
catch (CryptographicException e)
{
this.ErrorHandler(e);
}
catch (FormatException e)
{
this.ErrorHandler(e);
}
return null;
}
/// <summary>Decrypts data using a key or the supplied password.</summary>
/// <param name="data">The ciphertext (raw of base46-encoded bytes).</param>
/// <param name="password">Optional, the pasword.</param>
/// <returns>Plaintext.</returns>
public byte[] Decrypt(string data, string password = null)
{
return Decrypt(Encoding.ASCII.GetBytes(data), password);
}
/// <summary>Encrypts files using a key or the supplied password.</summary>
/// <remarks>
/// The original file is not modified; a new encrypted file is created.
/// The password is not required if a master key has been set
/// (either with `RandomKeyGgen` or with `SetMasterKey`).
/// If a password is supplied, it will be used to create a key with PBKDF2.
/// </remarks>
/// <param name="path">The file path.</param>
/// <param name="password">Optional, the pasword.</param>
/// <returns>The encrypted file path.</returns>
public string EncryptFile(string path, string password = null)
{
byte[] salt = RandomBytes(saltLen);
byte[] iv = RandomBytes(ivLen);
try
{
byte[][] keys = this.Keys(salt, password);
byte[] aesKey = keys[0], macKey = keys[1];
string newPath = path + ".enc";
using (FileStream fs = new FileStream(newPath, FileMode.Create, FileAccess.Write))
{
fs.Write(salt, 0, saltLen);
fs.Write(iv, 0, ivLen);
RijndaelManaged cipher = this.Cipher(aesKey, iv);
ICryptoTransform ict = cipher.CreateEncryptor();
HMACSHA256 hmac = new HMACSHA256(macKey);
hmac.TransformBlock(iv, 0, iv.Length, null, 0);
foreach (Object[] chunk in FileChunks(path))
{
byte[] data = (byte[])chunk[0];
byte[] ciphertext = new byte[data.Length];
if ((bool)chunk[1])
ciphertext = ict.TransformFinalBlock(data, 0, data.Length);
else
ict.TransformBlock(data, 0, data.Length, ciphertext, 0);
hmac.TransformBlock(ciphertext, 0, ciphertext.Length, null, 0);
fs.Write(ciphertext, 0, ciphertext.Length);
}
hmac.TransformFinalBlock(new byte[0], 0, 0);
byte[] mac = hmac.Hash;
fs.Write(mac, 0, mac.Length);
ict.Dispose();
cipher.Dispose();
hmac.Dispose();
}
return newPath;
}
catch (ArgumentException e)
{
this.ErrorHandler(e);
}
catch (CryptographicException e)
{
this.ErrorHandler(e);
}
catch (UnauthorizedAccessException e)
{
this.ErrorHandler(e);
}
catch (IOException e)
{
this.ErrorHandler(e);
}
return null;
}
/// <summary>Decrypts files using a key or the supplied password.</summary>
/// <remarks>
/// The original file is not modified; a new decrypted file is created.
/// The password is not required if a master key has been set
/// (either with `RandomKeyGgen` or with `SetMasterKey`).
/// If a password is supplied, it will be used to create a key with PBKDF2.
/// </remarks>
/// <param name="path">The file path.</param>
/// <param name="password">Optional, the pasword.</param>
/// <returns>Decrypted file path.</returns>
public string DecryptFile(string path, string password = null)
{
byte[] salt = new byte[saltLen];
byte[] iv = new byte[ivLen];
byte[] mac = new byte[macLen];
try
{
using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read))
{
fs.Read(salt, 0, saltLen);
fs.Read(iv, 0, ivLen);
fs.Seek(new FileInfo(path).Length - macLen, SeekOrigin.Begin);
fs.Read(mac, 0, macLen);
}
byte[][] keys = this.Keys(salt, password);
byte[] aesKey = keys[0], macKey = keys[1];
this.VerifyFile(path, mac, macKey);
string newPath = Regex.Replace(path, ".enc$", ".dec");
using (FileStream fs = new FileStream(newPath, FileMode.Create, FileAccess.Write))
{
RijndaelManaged cipher = this.Cipher(aesKey, iv);
ICryptoTransform ict = cipher.CreateDecryptor();
foreach (Object[] chunk in FileChunks(path, saltLen + ivLen, macLen))
{
byte[] data = (byte[])chunk[0];
byte[] plaintext = new byte[data.Length];
if ((bool)chunk[1])
{
plaintext = ict.TransformFinalBlock(data, 0, data.Length);
fs.Write(plaintext, 0, plaintext.Length);
}
else
{
int size = ict.TransformBlock(data, 0, data.Length, plaintext, 0);
fs.Write(plaintext, 0, size);
}
}
ict.Dispose();
cipher.Dispose();
}
return newPath;
}
catch (ArgumentException e)
{
this.ErrorHandler(e);
}
catch (CryptographicException e)
{
this.ErrorHandler(e);
}
catch (UnauthorizedAccessException e)
{
this.ErrorHandler(e);
}
catch (IOException e)
{
this.ErrorHandler(e);
}
return null;
}
/// <summary>Sets a new master key.</summary>
/// <remarks>
/// This key will be used to create the encryption and authentication keys.
/// </remarks>
/// <param name="key">The new master key.</param>
/// <param name="raw">Optional, expexts raw bytes (not base64-encoded).</param>
public void SetMasterKey(byte[] key, bool raw = false)
{
try
{
if (!raw)
key = Convert.FromBase64String((Encoding.ASCII.GetString(key)));
this.masterkey = key;
}
catch (FormatException e)
{
this.ErrorHandler(e);
}
}
/// <summary>Sets a new master key.</summary>
/// <remarks>
/// This key will be used to create the encryption and authentication keys.
/// </remarks>
/// <param name="key">The new master key.</param>
public void SetMasterKey(string key)
{
this.SetMasterKey(Encoding.ASCII.GetBytes(key), false);
}
/// <summary>
/// Returns the master key (or null if the key is not set).
/// </summary>
/// <param name="raw">Optional, returns raw bytes (not base64-encoded).</param>
/// <returns>The master key.</returns>
public byte[] GetMasterKey(bool raw = false)
{
if (masterkey == null)
this.ErrorHandler(new ArgumentException("The key is not set!"));
else if (!raw)
return Encoding.ASCII.GetBytes(Convert.ToBase64String(masterkey));
return masterkey;
}
/// <summary>Generates a new random key.</summary>
/// <remarks>
/// This key will be used to create the encryption and authentication keys.
/// </remarks>
/// <param name="keyLen">Optional, the key size.</param>
/// <param name="raw">Optional, returns raw bytes (not base64-encoded).</param>
/// <returns>The new master key.</returns>
public byte[] RandomKeyGen(int keyLen = 32, bool raw = false)
{
masterkey = this.RandomBytes(keyLen);
if (!raw)
{
return Encoding.ASCII.GetBytes(Convert.ToBase64String(masterkey));
}
return masterkey;
}
/// <summary>
/// Handles exceptions (prints the exception message by default).
/// </summary>
protected virtual void ErrorHandler(Exception exception)
{
Console.WriteLine(exception.Message);
}
/// <summary>
/// Derives encryption and authentication keys from a key or password.
/// If the password is not null, it will be used to create the keys.
/// </summary>
private byte[][] Keys(byte[] salt, string password = null)
{
byte[] dkey;
if (password != null)
dkey = Kdf.Pbkdf2Sha512(password, salt, keyLen + macKeyLen, keyIterations);
else if (this.masterkey != null)
dkey = Kdf.HkdfSha256(this.masterkey, salt, keyLen + macKeyLen);
else
throw new ArgumentException("No password or key specified!");
byte[][] keys = new byte[2][] { new byte[keyLen], new byte[macKeyLen] };
Array.Copy(dkey, 0, keys[0], 0, keyLen);
Array.Copy(dkey, keyLen, keys[1], 0, macKeyLen);
return keys;
}
/// <summary>
/// Creates random bytes; used for IV, salt and key generation.
/// </summary>
private byte[] RandomBytes(int size)
{
byte[] rb = new byte[size];
using (RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider())
{
rng.GetBytes(rb);
}
return rb;
}
/// <summary>
/// Creates an RijndaelManaged object; used for encryption / decryption.
/// </summary>
private RijndaelManaged Cipher(byte[] key, byte[] iv)
{
RijndaelManaged cipher = new RijndaelManaged
{
KeySize = this.keyLen * 8,
BlockSize = 128,
Mode = this.modes[this.mode],
Padding = (mode == "CFB") ? PaddingMode.None : PaddingMode.PKCS7,
FeedbackSize = (mode == "CFB") ? 8 : 128,
Key = key,
IV = iv
};
return cipher;
}
/// <summary>
/// Computes the MAC of ciphertext; used for authentication.
/// </summary>
private byte[] Sign(byte[] data, byte[] key)
{
using (HMACSHA256 hmac = new HMACSHA256(key))
{
return hmac.ComputeHash(data);
}
}
/// <summary>
/// Computes the MAC of ciphertext; used for authentication.
/// </summary>
private byte[] SignFile(string path, byte[] key, int beg = 0, int end = 0)
{
using (HMACSHA256 hmac = new HMACSHA256(key))
{
foreach (Object[] chunk in this.FileChunks(path, beg, end))
{
byte[] data = (byte[])chunk[0];
hmac.TransformBlock(data, 0, data.Length, null, 0);
}
hmac.TransformFinalBlock(new byte[0], 0, 0);
return hmac.Hash;
}
}
/// <summary>Verifies the authenticity of ciphertext.</summary>
/// <exception cref="ArgumentException">Thrown when MAC check fails.</exception>
private void Verify(byte[] data, byte[] mac, byte[] key)
{
byte[] dataMac = this.Sign(data, key);
if (!this.ConstantTimeComparison(mac, dataMac))
throw new ArgumentException("MAC verification failed!");
}
/// <summary>Verifies the authenticity of ciphertext.</summary>
/// <exception cref="ArgumentException">Thrown when MAC check fails.</exception>
private void VerifyFile(string path, byte[] mac, byte[] key)
{
byte[] fileMac = this.SignFile(path, key, saltLen, macLen);
if (!this.ConstantTimeComparison(mac, fileMac))
throw new ArgumentException("MAC verification failed!");
}
/// <summary>A generator that reads a file and yields chunks of data.</summary>
/// <remarks>The chunk size should be a multiple of the block size (16).</remarks>
/// <exception cref="FileNotFoundException">
/// Thrown when the file is not found.
/// </exception>
/// <exception cref="UnauthorizedAccessException">
/// Thrown when the file is not accesible.
/// </exception>
private IEnumerable<Object[]> FileChunks(string path, int beg = 0, int end = 0)
{
using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read))
{
int size = 1024;
end = (int)fs.Length - end;
int pos = fs.Read(new byte[beg], 0, beg);
while (pos < end)
{
size = (end - pos > size) ? size : end - pos;
byte[] data = new byte[size];
pos += fs.Read(data, 0, size);
yield return new Object[] { data, (pos == end) };
}
}
}
/// <summary>Safely compares two byte arrays, used for uthentication.</summary>
private bool ConstantTimeComparison(byte[] mac1, byte[] mac2)
{
int result = mac1.Length ^ mac2.Length;
for (int i = 0; i < mac1.Length && i < mac2.Length; i++)
{
result |= mac1[i] ^ mac2[i];
}
return result == 0;
}
private class Kdf
{
/// <summary>An PBKDF2 algorithm implementation, with HMAC-SHA512.</summary>
/// <param name="password">The password.</param>
/// <param name="salt">The salt.</param>
/// <param name="keyLen">The derived key size.</param>
/// <param name="iterations">The number of iterations.</param>
public static byte[] Pbkdf2Sha512(string password, byte[] salt, int keyLen, int iterations)
{
using (HMACSHA512 prf = new HMACSHA512(Encoding.UTF8.GetBytes(password)))
{
byte[] dkey = new byte[keyLen];
int hashLen = prf.HashSize / 8;
for (int i = 0; i < keyLen; i += hashLen)
{
byte[] b = BitConverter.GetBytes(i / hashLen + 1);
byte[] sb = new byte[salt.Length + 4];
Array.Reverse(b);
Array.Copy(salt, sb, salt.Length);
Array.Copy(b, 0, sb, salt.Length, 4);
byte[] u = prf.ComputeHash(sb);
byte[] f = u;
for (int j = 1; j < iterations; j++)
{
u = prf.ComputeHash(u);
for (int k = 0; k < f.Length; k++)
f[k] ^= u[k];
}
if (i + hashLen > keyLen)
hashLen = hashLen - (i + hashLen - keyLen);
Array.Copy(f, 0, dkey, i, hashLen);
}
return dkey;
}
}
/// <summary>A HKDF algorithm implementation, with HMAC-SHA256.</summary>
/// <param name="key">The key.</param>
/// <param name="salt">The salt.</param>
/// <param name="keyLen">The derived key size.</param>
public static byte[] HkdfSha256(byte[] key, byte[] salt, int keyLen)
{
byte[] dkey = new byte[keyLen];
byte[] mkey = new byte[0];
byte[] prk;
int hashLen = 32;
using (HMACSHA256 hmac = new HMACSHA256(salt))
prk = hmac.ComputeHash(key);
for (int i = 0; i < keyLen; i += hashLen)
{
Array.Resize(ref mkey, mkey.Length + 1);
mkey[mkey.Length - 1] = BitConverter.GetBytes(i / hashLen + 1)[0];
using (HMACSHA256 hmac = new HMACSHA256(prk))
mkey = hmac.ComputeHash(mkey);
if (i + hashLen > keyLen)
hashLen = hashLen - (i + hashLen - keyLen);
Array.Copy(mkey, 0, dkey, i, hashLen);
}
return dkey;
}
}
}