-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
422 lines (327 loc) · 15.5 KB
/
Program.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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
using System;
using System.IO;
using System.Text;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Collections.Generic;
using System.Security.Cryptography;
namespace JPL_Internship
{
// ! GLOBAL
public class EncryptedJson
{
public string jType { get; set; }
public string SHash { get; set; }
public byte[] AHash { get; set; }
public byte[] ASignature { get; set; }
public string APublicKey { get; set; }
public string jData { get; set; }
public string FileToString (string path)
{
StreamReader r = new StreamReader(path);
string jsonString = r.ReadToEnd();
return jsonString;
}
public EncryptedJson LoadJson(string path)
{
string file = FileToString(path);
EncryptedJson json = Newtonsoft.Json.JsonConvert.DeserializeObject<EncryptedJson>(file);
return json;
}
public void AEncodedJson(string type, byte[] hash, byte[] signature, string publicKey, string data, string encrFileName)
{
EncryptedJson json = new EncryptedJson();
json.jType = type;
json.AHash = hash;
json.ASignature = signature;
json.APublicKey = publicKey;
json.jData = data;
string jsonReady = JsonSerializer.Serialize(json);
File.WriteAllText(@$"{encrFileName}", jsonReady);
}
public void SEncodedJson(string type, string hash, string data, string encrFileName)
{
EncryptedJson json = new EncryptedJson();
json.jType = type;
json.SHash = hash;
json.jData = data;
string jsonReady = JsonSerializer.Serialize(json);
File.WriteAllText(@$"{encrFileName}", jsonReady);
}
}
public interface IEncryption{
void Encrypt(string data, string encrFileName);
void Decrypt(string encrFile, string key);
}
public class AsymmetricManager : IEncryption{
private readonly string _type;
private RSACryptoServiceProvider rsa;
private (byte[],string) GenerateHash(string data)
{
StringBuilder stringBdr = new StringBuilder(); // repeated modifications to a string
byte[] textBytes = Encoding.ASCII.GetBytes(data); // converting string to into a bytes array
using (SHA1 encr = SHA1.Create())
{
byte[] generateHash = encr.ComputeHash(textBytes);
for (int i = 0; i<generateHash.Length; i++)
{
stringBdr.Append(generateHash[i].ToString("x2")); // hexadecimal string conversion
}
return (generateHash,stringBdr.ToString());
}
}
private byte[] SignData(byte[] hashOfDataToSign, RSAParameters privateKey)
{
using (var rsa = new RSACryptoServiceProvider(2048))
{
rsa.PersistKeyInCsp = false;
rsa.ImportParameters(privateKey);
var rsaFormatter = new RSAPKCS1SignatureFormatter(rsa);
rsaFormatter.SetHashAlgorithm("SHA1");
return rsaFormatter.CreateSignature(hashOfDataToSign);
}
}
private bool VerifySignature(byte[] hashOfDataToSign, byte[] signature, string publicKey)
{
using (var rsa = new RSACryptoServiceProvider(2048))
{
rsa.FromXmlString(publicKey);
var rsaDeformatter = new RSAPKCS1SignatureDeformatter(rsa);
rsaDeformatter.SetHashAlgorithm("SHA1");
return rsaDeformatter.VerifySignature(hashOfDataToSign, signature);
}
}
private (RSAParameters, RSAParameters) GenerateAssymetricKeys(){
rsa = new RSACryptoServiceProvider(2048);
RSAParameters publicKey = rsa.ExportParameters(false); // sending this to a friend
RSAParameters privateKey = rsa.ExportParameters(true); // private key
return (publicKey, privateKey);
}
private string AEncrypt(string publicKey, string data)
{
using (var rsa = new RSACryptoServiceProvider())
{
rsa.FromXmlString(publicKey);
var byteData = Encoding.UTF8.GetBytes(data);
var encryptData = rsa.Encrypt(byteData, false);
return Convert.ToBase64String(encryptData);
}
}
private string ADecrypt(string cipherText, string privateKey)
{
using (var rsa = new RSACryptoServiceProvider())
{
var cipherByteData = Convert.FromBase64String(cipherText);
rsa.FromXmlString(privateKey);
var encryptData = rsa.Decrypt(cipherByteData, false);
return Encoding.UTF8.GetString(encryptData);
}
}
public AsymmetricManager()
{
_type = "1";
}
public void Encrypt(string data, string encrFileName)
{
EncryptedJson json = new EncryptedJson();
var asymKeys = GenerateAssymetricKeys();
string rawData = json.FileToString(data);
// Digital Signature
var hashedDocument = GenerateHash(rawData);
byte[] ByteHashedData = hashedDocument.Item1;
string StrHashedData = hashedDocument.Item2;
var signature = SignData(ByteHashedData, asymKeys.Item2);
Console.WriteLine();
Console.WriteLine("Digital Signature = " + Convert.ToBase64String(signature));
string publicKey = rsa.ToXmlString(false);
// byte[] publicKey2 = rsa.ExportRSAPublicKey();
// string b64PublicKey = Convert.ToBase64String(publicKey2);
string privateKey = rsa.ToXmlString(true);
Console.WriteLine();
Console.WriteLine("Public key: ");
Console.WriteLine(publicKey);
// Console.WriteLine(b64PublicKey);
Console.WriteLine("Private key: ");
Console.WriteLine(privateKey);
Console.WriteLine();
var dataToEncrypt = AEncrypt(publicKey, rawData);
json.AEncodedJson(_type, ByteHashedData, signature, publicKey, dataToEncrypt, encrFileName);
Console.WriteLine($"Data ecnrypted successfully in {encrFileName} -- Type: {_type}");
}
public void Decrypt(string encrFile, string privateKey)
{
EncryptedJson json = new EncryptedJson();
EncryptedJson encrInfo = json.LoadJson(encrFile);
byte[] hashedDocument = encrInfo.AHash;
byte[] signature = encrInfo.ASignature;
string publicKey = encrInfo.APublicKey;
string e_decr = encrInfo.jData;
var verified = VerifySignature(hashedDocument, signature, publicKey);
if (verified){
Console.WriteLine("--- VERIFIED ---");
Console.WriteLine();
var decrMessage = ADecrypt(e_decr, privateKey);
Console.WriteLine();
Console.WriteLine("Decrypted message: " + decrMessage);
Console.WriteLine($"Data decrypted successfully from {encrFile} -- Type: {_type}");
}
else{
Console.WriteLine("--- NOT VERIFIED ---");
}
}
}
public class SymmetricManager : IEncryption{
private readonly string _type;
private Aes GenerateCryptoKey(){
Aes aes = Aes.Create();
aes.GenerateIV();
aes.GenerateKey();
return aes;
}
private string ConvertCryptoKeyToStr(Aes aes){
string sKey = Convert.ToBase64String(aes.IV);
return sKey;
}
private byte[] ConvertCryptoKeyToByte(string key){
byte[] byteKey= Convert.FromBase64String(key);
return byteKey;
}
private string GenerateHash(string data)
{
StringBuilder stringBdr = new StringBuilder(); // repeated modifications to a string
byte[] textBytes = Encoding.ASCII.GetBytes(data); // converting string to into a bytes array
using (SHA1 encr = SHA1.Create())
{
byte[] generateHash = encr.ComputeHash(textBytes);
for (int i = 0; i<generateHash.Length; i++)
{
stringBdr.Append(generateHash[i].ToString("x2")); // hexadecimal string conversion
}
return stringBdr.ToString();
}
}
private string SEncrypt(string key, string data)
{
byte[] iv = new byte[16];
byte[] array;
using (Aes aes = Aes.Create())
{
aes.Key = Encoding.UTF8.GetBytes(key);
aes.IV = iv;
ICryptoTransform encryptor = aes.CreateEncryptor(aes.Key, aes.IV);
using (MemoryStream memoryStream = new MemoryStream())
{
using (CryptoStream cryptoStream = new CryptoStream((Stream)memoryStream, encryptor, CryptoStreamMode.Write))
{
using (StreamWriter streamWriter = new StreamWriter((Stream)cryptoStream))
{
streamWriter.Write(data);
}
array = memoryStream.ToArray();
}
}
}
return Convert.ToBase64String(array);
}
private string SDecrypt(string key, string cipherText)
{
byte[] iv = new byte[16];
byte[] buffer = Convert.FromBase64String(cipherText);
using (Aes aes = Aes.Create())
{
aes.Key = Encoding.UTF8.GetBytes(key);
aes.IV = iv;
ICryptoTransform decryptor = aes.CreateDecryptor(aes.Key, aes.IV);
using (MemoryStream memoryStream = new MemoryStream(buffer))
{
using (CryptoStream cryptoStream = new CryptoStream((Stream)memoryStream, decryptor, CryptoStreamMode.Read))
{
using (StreamReader streamReader = new StreamReader((Stream)cryptoStream))
{
return streamReader.ReadToEnd();
}
}
}
}
}
public SymmetricManager()
{
_type = "2";
}
public void Encrypt(string data, string encrFileName)
{
EncryptedJson json = new EncryptedJson();
string rawData = json.FileToString(data);
var key = ConvertCryptoKeyToStr(GenerateCryptoKey());
Console.WriteLine();
Console.WriteLine("Symmetric Key: ");
Console.WriteLine(key);
Console.WriteLine();
var dataToEncrypt = SEncrypt(key, rawData);
var hash = GenerateHash(rawData);
json.SEncodedJson(_type, hash, dataToEncrypt, encrFileName);
Console.WriteLine($"Data ecnrypted successfully in {encrFileName} -- Type: {_type}");
}
public void Decrypt(string encrFile, string key)
{
EncryptedJson json = new EncryptedJson();
EncryptedJson encrInfo = json.LoadJson(encrFile);
string h_decr = encrInfo.SHash;
string e_decr = encrInfo.jData;
string decrypted_e = SDecrypt(key, e_decr);
string h1 = GenerateHash(decrypted_e);
if (string.Equals(h_decr, h1))
{
Console.WriteLine();
Console.WriteLine("Decrypted Message: " + decrypted_e);
}
Console.WriteLine($"Data decrypted successfully from {encrFile} -- Type: {_type}");
}
}
public abstract class EncryptionFactory
{
public abstract IEncryption Create();
}
public class AssymetricFactory:EncryptionFactory
{
public override IEncryption Create()
{
return new AsymmetricManager();
}
}
public class SymmetricFactory:EncryptionFactory
{
public override IEncryption Create()
{
return new SymmetricManager();
}
}
public enum EncryptionTypes
{
Asymmetric,
Symmetric
}
public class Factory
{
private readonly Dictionary<EncryptionTypes, EncryptionFactory> _factories;
public Factory()
{
_factories = new Dictionary<EncryptionTypes, EncryptionFactory>
{
{ EncryptionTypes.Asymmetric, new AssymetricFactory() },
{ EncryptionTypes.Symmetric, new SymmetricFactory() },
};
}
public IEncryption NewEncryption(EncryptionTypes action) =>_factories[action].Create();
}
class Program
{
static void Main(string[] args)
{
var factory = new Factory().NewEncryption(EncryptionTypes.Asymmetric);
factory.Decrypt("asym2.json",
"<RSAKeyValue><Modulus>xF1F4+IwVhuo4y/60i+6JnJjw5wbrpCcjv0Mhj3EyEDDdP3k8k0gAxeUbLqQTpcBYyyV+sQzHt0qlbC+lxCGf9aIl9BcxkC3z312y7mBLi0tAwxQnwn5t4tBP5TIaTD/PjlEIAliItrOnrY+RPLWckxdQKOrny8MSFJmz4TVf9Qsm9f49+8GrwxdhzUbwZ1IripleNtKptOIJH1gJEIks8F6alP5eGjNPjFE8tKyMrYfSxWZ/Z0LIyqf6uhbeyLscKXrrMzq7H2xjUevmPRSkoGrqPCBQO6jBTcRbczfjl4F8IJuCUm/YjyqfYaL7bhhxzA5rlTSsTPRy4ISwiZYZQ==</Modulus><Exponent>AQAB</Exponent><P>6OUlEGZhULgEGibXjg3py69vMoJPWqLoCfc1t/qN4qR+AywLozHGkUTCTA/TpWoDlTU6m+FXEJBvEi551/zTtVcIUTHx1RUF21pKvFxKWOdvQAQ3juSzUXWnE+uzcPDIYOMo9veZWMkMHCNMncmkQFb2iJ7bn2nR4wy/38wKEs0=</P><Q>19hamRib5No+n3EmwCZUrIvr+BX5pDCvsClOgs2F/68Q9fhjY1cu0V543JvkD5wVy3d9pt7cri8nNBGJvJ2wfb/LzLv0auKuRscArLtgzk+VHrx9zXugRIcWNR8WTkesLYSSug3Ko+h1zSN3IWfRWBMbZ8fpw7NEyZV16yw4S/k=</Q><DP>lNZUgI2dZbOkU349RbiYkKZ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