forked from aetperf/FastWrappers-TSQL
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKeyProvider.cs
More file actions
66 lines (59 loc) · 1.78 KB
/
KeyProvider.cs
File metadata and controls
66 lines (59 loc) · 1.78 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
using System;
using System.IO;
using System.Security.Cryptography;
namespace FastWrapper
{
public static class KeyProvider
{
// hard coded AES key and IV as sample (change it and compile your own)
private static readonly byte[] AesKey = {
0x01, 0x33, 0x58, 0xA7, 0x3B, 0x99, 0x2D, 0xFA,
0x62, 0x11, 0xD5, 0xE7, 0x8F, 0x2C, 0x99, 0x0A,
0xF2, 0x68, 0x44, 0xFA, 0x48, 0x92, 0xBE, 0x65,
0x10, 0x7A, 0xCA, 0xAC, 0x9E, 0xDE, 0x7F, 0x0C
};
private static readonly byte[] AesIV = {
0x11, 0x22, 0xAA, 0x77, 0x55, 0x99, 0x10, 0x01,
0x66, 0x33, 0x45, 0x0F, 0x3A, 0x2B, 0xCC, 0xEE
};
// Encrypt and decrypt methods
public static string AesEncrypt(string plainText)
{
if (plainText == null) return null;
using (Aes aes = Aes.Create())
{
aes.Key = AesKey;
aes.IV = AesIV;
aes.Mode = CipherMode.CBC;
aes.Padding = PaddingMode.PKCS7;
MemoryStream ms = new MemoryStream();
using (ICryptoTransform encryptor = aes.CreateEncryptor())
using (CryptoStream cs = new CryptoStream(ms, encryptor, CryptoStreamMode.Write))
using (StreamWriter sw = new StreamWriter(cs))
{
sw.Write(plainText);
}
return Convert.ToBase64String(ms.ToArray());
}
}
public static string AesDecrypt(string base64Cipher)
{
if (string.IsNullOrEmpty(base64Cipher)) return null;
byte[] cipherBytes = Convert.FromBase64String(base64Cipher);
using (Aes aes = Aes.Create())
{
aes.Key = AesKey;
aes.IV = AesIV;
aes.Mode = CipherMode.CBC;
aes.Padding = PaddingMode.PKCS7;
using (var ms = new MemoryStream(cipherBytes))
using (var decryptor = aes.CreateDecryptor())
using (var cs = new CryptoStream(ms, decryptor, CryptoStreamMode.Read))
using (var sr = new StreamReader(cs))
{
return sr.ReadToEnd();
}
}
}
}
}