-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathAesUtil.cs
94 lines (76 loc) · 3.59 KB
/
AesUtil.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
using System;
using System.Collections.Generic;
using System.IO;
using System.Security.Cryptography;
using System.Text;
namespace Meadow.Cli
{
static class AesUtil
{
const int ITERATIONS = 50000;
static readonly HashAlgorithmName HASH_ALGO = HashAlgorithmName.SHA512;
public static string EncryptString(string text, string password)
{
var passwordBytes = new UTF8Encoding(false, false).GetBytes(password);
using (Aes aesAlg = Aes.Create())
{
aesAlg.GenerateIV();
byte[] iv = aesAlg.IV;
// The Salt will be the first 8 bytes of the IV.
byte[] theSalt = new byte[8];
Array.Copy(iv, theSalt, 8);
// A key for AES is generated by expanding the password using the following method.
Rfc2898DeriveBytes keyGen = new Rfc2898DeriveBytes(passwordBytes, theSalt, ITERATIONS, HASH_ALGO);
byte[] aesKey = keyGen.GetBytes(aesAlg.KeySize / 8);
aesAlg.Key = aesKey;
aesAlg.Mode = CipherMode.CBC;
aesAlg.Padding = PaddingMode.PKCS7;
// Create a decrytor to perform the stream transform.
using (ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, iv))
using (MemoryStream msEncrypt = new MemoryStream())
{
// You can write the IV here and not need to do it later.
msEncrypt.Write(iv, 0, iv.Length);
using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
{
// Write all data to the stream.
swEncrypt.Write(text);
}
return Convert.ToBase64String(msEncrypt.ToArray());
}
}
}
public static string DecryptString(string cipherText, string password)
{
var passwordBytes = new UTF8Encoding(false, false).GetBytes(password);
var fullCipher = Convert.FromBase64String(cipherText);
using (Aes aesAlg = Aes.Create())
{
aesAlg.Mode = CipherMode.CBC;
aesAlg.Padding = PaddingMode.PKCS7;
// Grab IV from ciphertext
byte[] iv = new byte[16];
Array.Copy(fullCipher, 0, iv, 0, 16);
// Use the IV for the Salt
byte[] theSalt = new byte[8];
Array.Copy(iv, theSalt, 8);
Rfc2898DeriveBytes keyGen = new Rfc2898DeriveBytes(passwordBytes, theSalt, ITERATIONS, HASH_ALGO);
byte[] aesKey = keyGen.GetBytes(aesAlg.KeySize / 8);
aesAlg.Key = aesKey;
// Create a decrytor to perform the stream transform.
using (ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, iv))
using (MemoryStream msDecrypt = new MemoryStream(fullCipher))
{
msDecrypt.Position += 16;
using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read)) //Notice this is Read mode not Write mode.
using (StreamReader srDecrypt = new StreamReader(csDecrypt))
{
//Decrypt the ciphertext
return srDecrypt.ReadToEnd();
}
}
}
}
}
}