-
Notifications
You must be signed in to change notification settings - Fork 0
/
AESClass.cs
244 lines (208 loc) · 13.7 KB
/
AESClass.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
/*
Copyright (C) 2020 Stefan Zehnpfennig
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Security.Cryptography;
using System.IO.Compression;
namespace speckmops
{
public static class AESClass
{
public static int PBKDF2_MinIterations = 10000;
public static int PBKDF2_MaxIterations = 20000;
public static int PBKDF2_SaltSize = 64;
public static Encoding DefaultEncoding = Encoding.Default;
public static CipherMode CipherMode = CipherMode.CBC;
public static byte[] Decrypt_Byte2Byte(byte[] data, byte[] Password, bool Compression = false) => Decrypt(data, Password, Compression);
public static byte[] Encrypt_Byte2Byte(byte[] data, byte[] Password, bool Compression = false) => Encrypt(data, Password, Compression);
public static string Decrypt_Byte2String(byte[] data, byte[] Password, bool Compression = false) => GetString(Decrypt(data, Password, Compression));
public static string Encrypt_Byte2String(byte[] data, byte[] Password, bool Compression = false) => GetString(Encrypt(data, Password, Compression));
public static byte[] Decrypt_String2Byte(string data, byte[] Password, bool Compression = false) => Decrypt(GetBytes(data), Password, Compression);
public static byte[] Encrypt_String2Byte(string data, byte[] Password, bool Compression = false) => Encrypt(GetBytes(data), Password, Compression);
public static string Decrypt_String2String(string data, byte[] Password, bool Compression = false) => GetString(Decrypt(GetBytes(data), Password, Compression));
public static string Encrypt_String2String(string data, byte[] Password, bool Compression = false) => GetString(Encrypt(GetBytes(data), Password, Compression));
public static byte[] Decrypt_Byte2Byte(byte[] data, string Password, bool Compression = false) => Decrypt(data, Password, Compression);
public static byte[] Encrypt_Byte2Byte(byte[] data, string Password, bool Compression = false) => Encrypt(data, Password, Compression);
public static string Decrypt_Byte2String(byte[] data, string Password, bool Compression = false) => GetString(Decrypt(data, Password, Compression));
public static string Encrypt_Byte2String(byte[] data, string Password, bool Compression = false) => GetString(Encrypt(data, Password, Compression));
public static byte[] Decrypt_String2Byte(string data, string Password, bool Compression = false) => Decrypt(GetBytes(data), Password, Compression);
public static byte[] Encrypt_String2Byte(string data, string Password, bool Compression = false) => Encrypt(GetBytes(data), Password, Compression);
public static string Decrypt_String2String(string data, string Password, bool Compression = false) => GetString(Decrypt(GetBytes(data), Password, Compression));
public static string Encrypt_String2String(string data, string Password, bool Compression = false) => GetString(Encrypt(GetBytes(data), Password, Compression));
public static string Decrypt_Base64String2String(string data, byte[] Password, bool Compression = false) => GetString(Decrypt(Convert.FromBase64String(data), Password, Compression));
public static string Encrypt_Base64String2String(string data, byte[] Password, bool Compression = false) => GetString(Encrypt(Convert.FromBase64String(data), Password, Compression));
public static string Decrypt_String2Base64String(string data, byte[] Password, bool Compression = false) => Convert.ToBase64String(Decrypt(GetBytes(data), Password, Compression));
public static string Encrypt_String2Base64String(string data, byte[] Password, bool Compression = false) => Convert.ToBase64String(Encrypt(GetBytes(data), Password, Compression));
public static byte[] Decrypt_Base64String2Byte(string data, byte[] Password, bool Compression = false) => Decrypt(Convert.FromBase64String(data), Password, Compression);
public static byte[] Encrypt_Base64String2Byte(string data, byte[] Password, bool Compression = false) => Encrypt(Convert.FromBase64String(data), Password, Compression);
public static string Decrypt_Byte2Base64String(byte[] data, byte[] Password, bool Compression = false) => Convert.ToBase64String(Decrypt(data, Password, Compression));
public static string Encrypt_Byte2Base64String(byte[] data, byte[] Password, bool Compression = false) => Convert.ToBase64String(Encrypt(data, Password, Compression));
public static string Decrypt_Base64String2Base64String(string data, byte[] Password, bool Compression = false) => Convert.ToBase64String(Decrypt(Convert.FromBase64String(data), Password, Compression));
public static string Encrypt_Base64StringBase64String(string data, byte[] Password, bool Compression = false) => Convert.ToBase64String(Encrypt(Convert.FromBase64String(data), Password, Compression));
public static string Decrypt_Base64String2String(string data, string Password, bool Compression = false) => GetString(Decrypt(Convert.FromBase64String(data), Password, Compression));
public static string Encrypt_Base64String2String(string data, string Password, bool Compression = false) => GetString(Encrypt(Convert.FromBase64String(data), Password, Compression));
public static string Decrypt_String2Base64String(string data, string Password, bool Compression = false) => Convert.ToBase64String(Decrypt(GetBytes(data), Password, Compression));
public static string Encrypt_String2Base64String(string data, string Password, bool Compression = false) => Convert.ToBase64String(Encrypt(GetBytes(data), Password, Compression));
public static byte[] Decrypt_Base64String2Byte(string data, string Password, bool Compression = false) => Decrypt(Convert.FromBase64String(data), Password, Compression);
public static byte[] Encrypt_Base64String2Byte(string data, string Password, bool Compression = false) => Encrypt(Convert.FromBase64String(data), Password, Compression);
public static string Decrypt_Byte2Base64String(byte[] data, string Password, bool Compression = false) => Convert.ToBase64String(Decrypt(data, Password, Compression));
public static string Encrypt_Byte2Base64String(byte[] data, string Password, bool Compression = false) => Convert.ToBase64String(Encrypt(data, Password, Compression));
public static string Decrypt_Base64String2Base64String(string data, string Password, bool Compression = false) => Convert.ToBase64String(Decrypt(Convert.FromBase64String(data), Password, Compression));
public static string Encrypt_Base64StringBase64String(string data, string Password, bool Compression = false) => Convert.ToBase64String(Encrypt(Convert.FromBase64String(data), Password, Compression));
private static byte[] Decrypt(byte[] data, string Password, bool Compression = false) => Decrypt(data, Encoding.Default.GetBytes(Password), Compression);
private static byte[] Decrypt(byte[] data, byte[] Password, bool Compression = false)
{
try
{
int Iterations = BitConverter.ToInt32(data, 0);
byte[] IV = new byte[16];
byte[] Salt = new byte[PBKDF2_SaltSize];
byte[] EncryptedData = new byte[data.Length - PBKDF2_SaltSize - 16 - 4];
Array.Copy(data, 4, IV, 0, 16);
Array.Copy(data, 4 16, Salt, 0, PBKDF2_SaltSize);
Array.Copy(data, 4 16 PBKDF2_SaltSize, EncryptedData, 0, EncryptedData.Length);
byte[] Result = null;
using (AesManaged AES = new AesManaged())
{
AES.KeySize = 256;
AES.BlockSize = 128;
AES.Mode = CipherMode;
Rfc2898DeriveBytes rfc2898 = new Rfc2898DeriveBytes(Password, Salt, Iterations);
AES.Key = rfc2898.GetBytes(AES.KeySize / 8);
AES.IV = IV;
ICryptoTransform aes_decryptor = AES.CreateDecryptor(AES.Key, AES.IV);
using (MemoryStream ms = new MemoryStream(EncryptedData))
{
using (CryptoStream cs = new CryptoStream(ms, aes_decryptor, CryptoStreamMode.Read))
{
byte[] buffer = new byte[1024];
int readed = 0;
do
{
readed = cs.Read(buffer, 0, buffer.Length);
if (readed > 0)
{
if (Result == null)
{
Result = new byte[readed];
Array.Copy(buffer, 0, Result, 0, readed);
}
else
{
int offset = Result.Length;
Array.Resize(ref Result, Result.Length readed);
Array.Copy(buffer, 0, Result, offset, readed);
}
}
} while (readed > 0);
}
if(Compression)
{
return Decompress(Result);
}
else
{
return Result;
}
}
}
}
catch { throw new Exception("Decryption failed"); }
}
private static byte[] Encrypt(byte[] data, string Password, bool Compression = false) => Encrypt(data, Encoding.Default.GetBytes(Password), Compression);
private static byte[] Encrypt(byte[] data, byte[] Password, bool Compression = false)
{
try
{
if(Compression)
{
data = Compress(data);
}
List<byte> result = new List<byte>();
using (AesManaged AES = new AesManaged())
{
AES.KeySize = 256;
AES.BlockSize = 128;
if (PBKDF2_MinIterations < 1 || PBKDF2_MinIterations > PBKDF2_MaxIterations)
{
throw new Exception("PBKDF2_MinIterations illegal value");
}
int Iterations = new Random().Next(PBKDF2_MinIterations, PBKDF2_MaxIterations);
Rfc2898DeriveBytes rfc2898 = new Rfc2898DeriveBytes(Encoding.Default.GetString(Password), PBKDF2_SaltSize, Iterations);
AES.Key = rfc2898.GetBytes(AES.KeySize / 8);
AES.IV = new byte[16];
AES.GenerateIV();
AES.Mode = CipherMode;
result.AddRange(BitConverter.GetBytes(Iterations));
result.AddRange(AES.IV);
result.AddRange(rfc2898.Salt);
ICryptoTransform aes_encryptor = AES.CreateEncryptor(AES.Key, AES.IV);
using (MemoryStream ms = new MemoryStream())
{
using (CryptoStream cs = new CryptoStream(ms, aes_encryptor, CryptoStreamMode.Write))
{
cs.Write(data, 0, data.Length);
}
result.AddRange(ms.ToArray());
}
}
return result.ToArray();
}
catch { throw new Exception("Encryption failed"); }
}
private static byte[] Compress(byte[] data)
{
try
{
using (MemoryStream ms = new MemoryStream())
{
using (DeflateStream ds = new DeflateStream(ms, CompressionMode.Compress))
{
ds.Write(data, 0, data.Length);
}
return ms.ToArray();
}
}
catch
{
throw new Exception("Compression failed");
}
}
private static byte[] Decompress(byte[] data)
{
try
{
using (MemoryStream decompressedMS = new MemoryStream())
{
using (MemoryStream compressMS = new MemoryStream(data))
{
using (DeflateStream ds = new DeflateStream(compressMS, CompressionMode.Decompress))
{
ds.CopyTo(decompressedMS);
}
}
return decompressedMS.ToArray();
}
}
catch
{
throw new Exception("Decompression failed");
}
}
private static string GetString(byte[] data) => DefaultEncoding.GetString(data);
private static byte[] GetBytes(string data) => DefaultEncoding.GetBytes(data);
}
}