-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathAesStringEncryption.cs
More file actions
101 lines (87 loc) · 3.79 KB
/
Copy pathAesStringEncryption.cs
File metadata and controls
101 lines (87 loc) · 3.79 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
using System.IO;
using System.Security.Cryptography;
namespace System.Text.Json.Serialization.Encryption
{
internal class AesStringEncryption
{
/// <summary>
/// Encrypt a string
/// </summary>
/// <param name="plainTextString">the string to encrypt</param>
/// <param name="encryptionKey">the secret used to encrypt</param>
/// <returns>the encrypted string</returns>
public static string Encrypt(string plainTextString, string encryptionKey)
{
if (string.IsNullOrWhiteSpace(plainTextString))
{
throw new ArgumentException($"'{nameof(plainTextString)}' must contain a value.", nameof(plainTextString));
}
if (string.IsNullOrWhiteSpace(encryptionKey))
{
throw new ArgumentException($"'{nameof(encryptionKey)}' must contain a value.", nameof(encryptionKey));
}
var buffer = Encoding.UTF8.GetBytes(plainTextString);
using (var sha = new SHA256Managed())
using (var inputStream = new MemoryStream(buffer, false))
using (var outputStream = new MemoryStream())
using (var aes = new AesManaged())
{
aes.Key = sha.ComputeHash(Encoding.UTF8.GetBytes(encryptionKey));
var iv = aes.IV;
outputStream.Write(iv, 0, iv.Length);
outputStream.Flush();
var encryptor = aes.CreateEncryptor(aes.Key, iv);
using (var cryptoStream = new CryptoStream(outputStream, encryptor, CryptoStreamMode.Write))
{
inputStream.CopyTo(cryptoStream);
}
return Convert.ToBase64String(outputStream.ToArray());
}
}
/// <summary>
/// Decrypt a string
/// </summary>
/// <param name="encryptedString">the encrypted string</param>
/// <param name="encryptionKey">the secret used to encrypt</param>
/// <returns>decrypted string</returns>
public static string Decrypt(string encryptedString, string encryptionKey)
{
if (string.IsNullOrWhiteSpace(encryptedString))
{
throw new ArgumentException($"'{nameof(encryptedString)}' must contain a value.", nameof(encryptedString));
}
if (string.IsNullOrWhiteSpace(encryptionKey))
{
throw new ArgumentException($"'{nameof(encryptionKey)}' must contain a value.", nameof(encryptionKey));
}
try
{
var buffer = Convert.FromBase64String(encryptedString);
using (var sha = new SHA256Managed())
using (var inputStream = new MemoryStream(buffer, false))
using (var outputStream = new MemoryStream())
using (var aes = new AesManaged())
{
aes.Key = sha.ComputeHash(Encoding.UTF8.GetBytes(encryptionKey));
var iv = new byte[16];
var bytesRead = inputStream.Read(iv, 0, 16);
if (bytesRead < 16)
{
throw new CryptographicException("Invalid or missing IV.");
}
var decryptor = aes.CreateDecryptor(aes.Key, iv);
using (var cryptoStream = new CryptoStream(inputStream, decryptor, CryptoStreamMode.Read))
{
cryptoStream.CopyTo(outputStream);
}
var decryptedValue = Encoding.UTF8.GetString(outputStream.ToArray());
return decryptedValue;
}
}
catch
{
return string.Empty;
}
}
}
}