-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
367 lines (319 loc) · 15.8 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
using Org.BouncyCastle.Bcpg;
using Org.BouncyCastle.Bcpg.OpenPgp;
using Org.BouncyCastle.Crypto;
using Org.BouncyCastle.Crypto.Digests;
using Org.BouncyCastle.Crypto.Encodings;
using Org.BouncyCastle.Crypto.Engines;
using Org.BouncyCastle.OpenSsl;
using Org.BouncyCastle.Security;
using Org.BouncyCastle.Utilities.IO;
using System;
using System.IO;
using System.Runtime.InteropServices;
namespace BCCrypto
{
class Program
{
// Base64 encoded salt string, also known as encoding parameters
private static readonly string _saltBase64 = "cEAkJHcwcmQ=";
private static readonly string _password = "p@$$w0rd";
public enum Action { Encrypt, Decrypt }
static void Main(string[] args)
{
string currentDirectory = Environment.CurrentDirectory;
string prefix;
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux) || RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
{
prefix = string.Format("{0}/", currentDirectory);
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
prefix = Path.GetFullPath(string.Format("{0}/{1}", currentDirectory, "../../../"));
}
else
{
return;
}
// Encrypt RSA-OAEP with public key
CryptoAction(
string.Format("{0}{1}", prefix, "input/test.txt"),
string.Format("{0}{1}", prefix, "output/test (encrypted).rsa"), // included small text file for faster test run
//string.Format("{0}{1}", prefix, "input/RFC 8017 - PKCS #1_ RSA Cryptography Specifications Version 2.2.html"),
//string.Format("{0}{1}", prefix, "output/RFC 8017 - PKCS #1_ RSA Cryptography Specifications Version 2.2 (encrypted).rsa"),
string.Format("{0}{1}", prefix, "keys/public.pem"),
Standards.RsaOaep,
Action.Encrypt
);
// Decrypt RSA-OAEP with private key
CryptoAction(
string.Format("{0}{1}", prefix, "output/test (encrypted).rsa"),
string.Format("{0}{1}", prefix, "output/test (decrypted).txt"), // included small text file for faster test run
//string.Format("{0}{1}", prefix, "output/RFC 8017 - PKCS #1_ RSA Cryptography Specifications Version 2.2 (encrypted).rsa"),
//string.Format("{0}{1}", prefix, "output/RFC 8017 - PKCS #1_ RSA Cryptography Specifications Version 2.2 (decrypted).html"),
string.Format("{0}{1}", prefix, "keys/private.pem"),
Standards.RsaOaep,
Action.Decrypt
);
// Encrypt OpenPGP with public key
CryptoAction(
string.Format("{0}{1}", prefix, "input/RFC 4880 - OpenPGP Message Format.pdf"),
string.Format("{0}{1}", prefix, "output/RFC 4880 - OpenPGP Message Format (encrypted).gpg"),
string.Format("{0}{1}", prefix, "keys/public.gpg"),
Standards.OpenPgp,
Action.Encrypt
);
// Decrypt OpenPGP with private key
CryptoAction(
string.Format("{0}{1}", prefix, "output/RFC 4880 - OpenPGP Message Format (encrypted).gpg"),
string.Format("{0}{1}", prefix, "output/RFC 4880 - OpenPGP Message Format (decrypted).pdf"),
string.Format("{0}{1}", prefix, "keys/private.gpg"),
Standards.OpenPgp,
Action.Decrypt
);
Console.WriteLine("Enjoy cryptography!");
}
public static void CryptoAction(string inputFile, string outputFile, string inputKey, string standard, Action action)
{
using (Stream input = File.OpenRead(inputFile), output = File.Create(outputFile), key = File.OpenRead(inputKey))
{
try
{
switch (standard)
{
case "RSAES-OAEP":
if (action.Equals(Action.Encrypt))
{
EncryptRsa(input, output, key, Convert.FromBase64String(_saltBase64));
}
else if (action.Equals(Action.Decrypt))
{
DecryptRsa(input, output, key, Convert.FromBase64String(_saltBase64), _password.ToCharArray());
}
break;
case "OpenPGP":
if (action.Equals(Action.Encrypt))
{
EncryptPgp(input, output, key, true, true);
}
else if (action.Equals(Action.Decrypt))
{
DecryptPgp(input, output, key, _password.ToCharArray());
}
break;
default:
Console.WriteLine("Encryption standard is not supported.");
break;
};
}
catch (Exception ex)
{
Console.WriteLine(string.Format("{0}{1}{2}", ex.Message, Environment.NewLine, ex.InnerException));
}
}
}
#region RSA
private static void EncryptRsa(Stream input, Stream output, Stream key, byte[] salt)
{
using (StreamReader streamReader = new StreamReader(key))
{
AsymmetricKeyParameter publicKey = (AsymmetricKeyParameter)new PemReader(streamReader).ReadObject();
using (MemoryStream inputMemory = new MemoryStream(), outputMemory = new MemoryStream())
{
input.CopyTo(inputMemory);
byte[] inputBytes = inputMemory.ToArray();
IAsymmetricBlockCipher cipher = new OaepEncoding(new RsaEngine(), new Sha256Digest(), salt);
cipher.Init(true, publicKey);
int length = inputBytes.Length;
int blockSize = cipher.GetInputBlockSize();
for (int offset = 0; offset < length; offset += blockSize)
{
int chunkSize = Math.Min(blockSize, length - offset);
byte[] ciphered = cipher.ProcessBlock(inputBytes, offset, chunkSize);
outputMemory.Write(ciphered, 0, ciphered.Length);
}
outputMemory.WriteTo(output);
Console.WriteLine("RSA-OAEP encryption successfull.");
}
}
}
private static void DecryptRsa(Stream input, Stream output, Stream key, byte[] salt, char[] password)
{
using (StreamReader streamReader = new StreamReader(key))
{
AsymmetricCipherKeyPair cipherKeyPair = (AsymmetricCipherKeyPair)new PemReader(streamReader, new PasswordFinder(password)).ReadObject();
using (MemoryStream inputMemory = new MemoryStream(), outputMemory = new MemoryStream())
{
input.CopyTo(inputMemory);
byte[] inputBytes = inputMemory.ToArray();
IAsymmetricBlockCipher cipher = new OaepEncoding(new RsaEngine(), new Sha256Digest(), salt);
cipher.Init(false, cipherKeyPair.Private);
int length = inputBytes.Length;
int blockSize = cipher.GetInputBlockSize();
for (int offset = 0; offset < length; offset += blockSize)
{
int chunkSize = Math.Min(blockSize, length - offset);
byte[] deciphered = cipher.ProcessBlock(inputBytes, offset, chunkSize);
outputMemory.Write(deciphered, 0, deciphered.Length);
}
outputMemory.WriteTo(output);
Console.WriteLine("RSA-OAEP decryption successfull.");
}
}
}
#endregion
#region OpenPGP
private static void EncryptPgp(Stream input, Stream output, Stream key, bool armor, bool integrityCheck)
{
try
{
// Find public key for encryption
PgpPublicKey publicKey = null;
PgpPublicKeyRingBundle pgpPublicKeyRingBundle = new PgpPublicKeyRingBundle(PgpUtilities.GetDecoderStream(key));
foreach (PgpPublicKeyRing pkr in pgpPublicKeyRingBundle.GetKeyRings())
{
foreach (PgpPublicKey pKey in pkr.GetPublicKeys())
{
if (pKey.IsEncryptionKey)
{
publicKey = pKey;
break;
}
}
}
if (publicKey == null)
{
throw new ArgumentException("Public key for encryption not found.");
}
MemoryStream inputMemory = new MemoryStream();
input.CopyTo(inputMemory);
byte[] bytes = inputMemory.ToArray(); // clear data bytes
inputMemory.Close();
MemoryStream compressedLiteral = new MemoryStream();
PgpCompressedDataGenerator pgpCompressedDataGenerator = new PgpCompressedDataGenerator(CompressionAlgorithmTag.BZip2);
Stream compressed = pgpCompressedDataGenerator.Open(compressedLiteral);
PgpLiteralDataGenerator pgpLiteralDataGenerator = new PgpLiteralDataGenerator();
Stream literal = pgpLiteralDataGenerator.Open(compressed, PgpLiteralData.Binary, "STREAM", bytes.Length, DateTime.UtcNow);
literal.Write(bytes, 0, bytes.Length);
pgpLiteralDataGenerator.Close();
pgpCompressedDataGenerator.Close();
PgpEncryptedDataGenerator pgpEncryptedDataGenerator = new PgpEncryptedDataGenerator(SymmetricKeyAlgorithmTag.Aes256, integrityCheck, new SecureRandom());
pgpEncryptedDataGenerator.AddMethod(publicKey);
bytes = compressedLiteral.ToArray(); // compressed literal data bytes
MemoryStream encryptedMemory = new MemoryStream();
Stream os = encryptedMemory;
// optional armor ASCII encoding
if (armor)
{
os = new ArmoredOutputStream(os);
}
Stream encrypted = pgpEncryptedDataGenerator.Open(os, bytes.Length);
encrypted.Write(bytes, 0, bytes.Length);
encrypted.Close();
if (armor)
{
os.Close();
}
encryptedMemory.Seek(0, SeekOrigin.Begin);
Streams.PipeAll(encryptedMemory, output);
encryptedMemory.Close();
Console.WriteLine("OpenPGP encryption successfull.");
}
catch (PgpException ex)
{
Console.Error.WriteLine(ex);
Exception pgpInnerException = ex.InnerException;
if (pgpInnerException != null)
{
Console.Error.WriteLine(pgpInnerException.Message);
Console.Error.WriteLine(pgpInnerException.StackTrace);
}
}
}
private static void DecryptPgp(Stream input, Stream output, Stream key, char[] password)
{
try
{
PgpObjectFactory pgpObjectFactory = new PgpObjectFactory(PgpUtilities.GetDecoderStream(input));
PgpObject pgpObject = pgpObjectFactory.NextPgpObject();
// The first object might be a PGP marker packet
PgpEncryptedDataList pgpEncryptedDataList;
if (pgpObject is PgpEncryptedDataList)
{
pgpEncryptedDataList = (PgpEncryptedDataList)pgpObject;
}
else
{
pgpEncryptedDataList = (PgpEncryptedDataList)pgpObjectFactory.NextPgpObject();
}
// Find private key for decryption
PgpPrivateKey privateKey = null;
PgpSecretKeyRingBundle pgpSecretKeyRing = new PgpSecretKeyRingBundle(PgpUtilities.GetDecoderStream(key));
PgpPublicKeyEncryptedData pgpPublicKeyEncryptedData = null;
foreach (PgpPublicKeyEncryptedData pked in pgpEncryptedDataList.GetEncryptedDataObjects())
{
PgpSecretKey pgpDescretKey = pgpSecretKeyRing.GetSecretKey(pked.KeyId);
privateKey = pgpDescretKey.ExtractPrivateKey(password);
if (privateKey != null)
{
pgpPublicKeyEncryptedData = pked;
break;
}
}
if (privateKey == null)
{
throw new ArgumentException("Private key for decryption not found.");
}
Stream decrypted = pgpPublicKeyEncryptedData.GetDataStream(privateKey);
pgpObjectFactory = new PgpObjectFactory(decrypted);
pgpObject = pgpObjectFactory.NextPgpObject();
if (pgpObject is PgpCompressedData)
{
PgpCompressedData pgpCompressedData = (PgpCompressedData)pgpObject;
pgpObjectFactory = new PgpObjectFactory(pgpCompressedData.GetDataStream());
pgpObject = pgpObjectFactory.NextPgpObject();
}
if (pgpObject is PgpLiteralData)
{
PgpLiteralData pgpLiteralData = (PgpLiteralData)pgpObject;
Stream literal = pgpLiteralData.GetInputStream();
Streams.PipeAll(literal, output);
}
else if (pgpObject is PgpOnePassSignatureList)
{
throw new PgpException("Encrypted message contains a signed message, not a literal data.");
}
else
{
throw new PgpException("Message is not a simple encrypted file, type is unknown.");
}
if (pgpPublicKeyEncryptedData.IsIntegrityProtected())
{
if (!pgpPublicKeyEncryptedData.Verify())
{
Console.Error.WriteLine("Message failed integrity check.");
}
else
{
Console.Error.WriteLine("Message integrity check passed.");
}
}
else
{
Console.Error.WriteLine("No message integrity check.");
}
Console.WriteLine("OpenPGP decryption successfull.");
}
catch (PgpException ex)
{
Console.Error.WriteLine(ex);
Exception pgpInnerException = ex.InnerException;
if (pgpInnerException != null)
{
Console.Error.WriteLine(pgpInnerException.Message);
Console.Error.WriteLine(pgpInnerException.StackTrace);
}
}
}
#endregion
}
}