-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSimpleAESEncryption.cs
More file actions
96 lines (82 loc) · 3.14 KB
/
SimpleAESEncryption.cs
File metadata and controls
96 lines (82 loc) · 3.14 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
using System;
using System.Security.Cryptography;
using System.Text;
// ReSharper disable MemberCanBePrivate.Global
namespace DUCK.Crypto
{
public static class SimpleAESEncryption
{
/// <summary>
/// A class containing AES-encrypted text, plus the IV value required to decrypt it (with the correct password)
/// </summary>
public struct AESEncryptedText
{
public string IV;
public string EncryptedText;
}
/// <summary>
/// Encrypts a given text string with a password
/// </summary>
/// <param name="plainText">The text to encrypt</param>
/// <param name="password">The password which will be required to decrypt it</param>
/// <returns>An AESEncryptedText object containing the encrypted string and the IV value required to decrypt it.</returns>
public static AESEncryptedText Encrypt(string plainText, string password)
{
using (var aes = Aes.Create())
{
aes.GenerateIV();
aes.Key = ConvertToKeyBytes(aes, password);
var textBytes = Encoding.UTF8.GetBytes(plainText);
var aesEncryptor = aes.CreateEncryptor();
var encryptedBytes = aesEncryptor.TransformFinalBlock(textBytes, 0, textBytes.Length);
return new AESEncryptedText
{
IV = Convert.ToBase64String(aes.IV),
EncryptedText = Convert.ToBase64String(encryptedBytes)
};
}
}
/// <summary>
/// Decrypts an AESEncryptedText with a password
/// </summary>
/// <param name="encryptedText">The AESEncryptedText object to decrypt</param>
/// <param name="password">The password to use when decrypting</param>
/// <returns>The original plainText string.</returns>
public static string Decrypt(AESEncryptedText encryptedText, string password)
{
return Decrypt(encryptedText.EncryptedText, encryptedText.IV, password);
}
/// <summary>
/// Decrypts an encrypted string with an IV value password
/// </summary>
/// <param name="encryptedText">The encrypted string to be decrypted</param>
/// <param name="iv">The IV value which was generated when the text was encrypted</param>
/// <param name="password">The password to use when decrypting</param>
/// <returns>The original plainText string.</returns>
public static string Decrypt(string encryptedText, string iv, string password)
{
using (Aes aes = Aes.Create())
{
var ivBytes = Convert.FromBase64String(iv);
var encryptedTextBytes = Convert.FromBase64String(encryptedText);
var decryptor = aes.CreateDecryptor(ConvertToKeyBytes(aes, password), ivBytes);
var decryptedBytes = decryptor.TransformFinalBlock(encryptedTextBytes, 0, encryptedTextBytes.Length);
return Encoding.UTF8.GetString(decryptedBytes);
}
}
// Ensure the AES key byte-array is the right size - AES will reject it otherwise
private static byte[] ConvertToKeyBytes(SymmetricAlgorithm algorithm, string password)
{
algorithm.GenerateKey();
var keyBytes = Encoding.UTF8.GetBytes(password);
var validKeySize = algorithm.Key.Length;
if (keyBytes.Length != validKeySize)
{
var newKeyBytes = new byte[validKeySize];
Array.Copy(keyBytes, newKeyBytes, Math.Min(keyBytes.Length, newKeyBytes.Length));
keyBytes = newKeyBytes;
}
return keyBytes;
}
}
}