/
AsymmetricCryptographyRSA.cs
359 lines (322 loc) · 16.2 KB
/
AsymmetricCryptographyRSA.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
/*
Copyright 2020 Raphael Beck
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
using System;
using System.IO;
using System.Text;
using System.Collections.Generic;
using Org.BouncyCastle.OpenSsl;
using Org.BouncyCastle.Security;
using Org.BouncyCastle.Crypto;
using Org.BouncyCastle.Crypto.Engines;
using Org.BouncyCastle.Crypto.Encodings;
using Org.BouncyCastle.Crypto.Parameters;
namespace GlitchedPolygons.Services.Cryptography.Asymmetric
{
/// <summary>
/// <see cref="IAsymmetricCryptographyRSA"/> implementation for asymmetric RSA encryption/decryption and signing/verifying.
/// </summary>
/// <seealso cref="IAsymmetricCryptographyRSA" />
public class AsymmetricCryptographyRSA : IAsymmetricCryptographyRSA
{
/// <summary>
/// The algorithm used for signing and verifying.
/// <seealso cref="SignerUtilities"/>
/// </summary>
private readonly string signatureAlgo;
/// <summary>
/// Instantiates a new <see cref="AsymmetricCryptographyRSA"/> instance
/// to use for encrypting/decrypting, signing and verifying data strings and bytes.
/// </summary>
/// <param name="signatureAlgo">The signature algorithm to use in the signing and verifying methods. Check out <see cref="SignerUtilities"/> for more information about what <c>string</c> values are valid here.</param>
public AsymmetricCryptographyRSA(string signatureAlgo = "SHA256withRSA")
{
this.signatureAlgo = signatureAlgo;
}
#region Encrypting and decrypting
/// <summary>
/// Encrypts the specified text using the provided RSA public key.
/// </summary>
/// <param name="text">The plain text to encrypt.</param>
/// <param name="publicKeyPem">The public RSA key for encryption. Needs to be a PEM-formatted <c>string</c>.</param>
/// <returns>The encrypted <c>string</c>; <c>string.Empty</c> if the passed key or plain text argument was <c>null</c> or empty; <c>null</c> if encryption failed.</returns>
public string Encrypt(string text, string publicKeyPem)
{
if (string.IsNullOrEmpty(text) || string.IsNullOrEmpty(publicKeyPem))
{
return string.Empty;
}
try
{
return Convert.ToBase64String(Encrypt(Encoding.UTF8.GetBytes(text), publicKeyPem));
}
catch
{
return null;
}
}
/// <summary>
/// Decrypts the specified text using the provided RSA private key.
/// </summary>
/// <param name="encryptedText">The encrypted text to decrypt.</param>
/// <param name="privateKeyPem">The private RSA key needed for decryption (PEM-formatted <c>string</c>).</param>
/// <returns>Decrypted <c>string</c>; <c>null</c> if the passed key or encrypted text argument was <c>null</c> or empty; <c>null</c> if decryption failed.</returns>
public string Decrypt(string encryptedText, string privateKeyPem)
{
if (string.IsNullOrEmpty(encryptedText) || string.IsNullOrEmpty(privateKeyPem))
{
return string.Empty;
}
try
{
return Encoding.UTF8.GetString(Decrypt(Convert.FromBase64String(encryptedText), privateKeyPem));
}
catch
{
return null;
}
}
/// <summary>
/// Encrypts the specified bytes using the provided RSA public key,
/// which needs to be a PEM-formatted <c>string</c>.
/// </summary>
/// <param name="data">The data (<c>byte[]</c> array) to encrypt.</param>
/// <param name="publicKeyPem">The public key (PEM-formatted <c>string</c>) to use for encryption.</param>
/// <returns>The encrypted bytes (<c>System.Byte[]</c>) if successful; <c>Array.Empty<byte>()</c> if the passed data or key argument was <c>null</c> or empty; <c>null</c> if encryption failed.</returns>
public byte[] Encrypt(byte[] data, string publicKeyPem)
{
if (data is null || data.Length == 0 || string.IsNullOrEmpty(publicKeyPem))
{
return Array.Empty<byte>();
}
try
{
AsymmetricCipherKeyPair keyPair = PemStringToKeyPair(publicKeyPem);
ICipherParameters key = keyPair?.Public ?? PemStringToKeyParameters(publicKeyPem);
return ProcessData(data, key, true);
}
catch
{
return null;
}
}
/// <summary>
/// Decrypts the specified bytes using the provided private RSA key
/// (the key needs to be a PEM-formatted <c>string</c>).
/// </summary>
/// <param name="encryptedData">The encrypted data bytes (<c>byte[]</c>).</param>
/// <param name="privateKeyPem">The private RSA key to use for decryption (PEM-formatted <c>string</c>).</param>
/// <returns>Decrypted bytes (System.Byte[]) if successful; an empty <c>byte[]</c> array if the passed data or key argument was <c>null</c> or empty; <c>null</c> if decryption failed.</returns>
public byte[] Decrypt(byte[] encryptedData, string privateKeyPem)
{
if (encryptedData is null || encryptedData.Length == 0 || string.IsNullOrEmpty(privateKeyPem))
{
return Array.Empty<byte>();
}
try
{
return ProcessData(encryptedData, PemStringToKeyPair(privateKeyPem).Private, false);
}
catch
{
return null;
}
}
#endregion
#region Signing and verifying
/// <summary>
/// Signs the specified <c>string</c> using the provided private RSA key (which needs to be a PEM-formatted <c>string</c>).<para> </para>
/// Signature algo is the value of <see cref="signatureAlgo"/>; see <see cref="SignerUtilities"/> for more information about what algorithms are supported and what <c>string</c>s to use here.<para> </para>
/// If the procedure succeeds, the calculated signature <c>string</c> is returned (which is base-64 encoded).<para> </para>
/// Otherwise, an empty <c>string</c> is returned if the provided <paramref name="data"/> and/or <paramref name="privateKeyPem"/> parameters
/// were <c>null</c> or empty. If the procedure fails entirely, <c>null</c> is returned.
/// </summary>
/// <param name="data">The data to sign.</param>
/// <param name="privateKeyPem">The private RSA key to use for generating the signature (PEM-formatted <c>string</c>)</param>
/// <returns>The signature (base-64 encoded <c>string</c>). <c>string.Empty</c> is returned if the provided <paramref name="data"/> and/or <paramref name="privateKeyPem"/> parameters were <c>null</c> or empty. Returns <c>null</c> if signing failed entirely.</returns>
/// <seealso cref="SignerUtilities"/>
public string Sign(string data, string privateKeyPem)
{
if (string.IsNullOrEmpty(data) || string.IsNullOrEmpty(privateKeyPem))
{
return string.Empty;
}
try
{
byte[] signature = Sign(Encoding.UTF8.GetBytes(data), privateKeyPem);
return Convert.ToBase64String(signature);
}
catch
{
return null;
}
}
/// <summary>
/// Verifies a signature that was obtained using <see cref="Sign(string,string)"/> with a public RSA key (which needs to be a PEM-formatted <c>string</c>).<para> </para>
/// </summary>
/// <param name="data">The data whose signature you want to verify.</param>
/// <param name="signature">The passed <paramref name="data"/>'s signature (return value of <see cref="Sign(string,string)"/>).</param>
/// <param name="publicKeyPem">The public RSA key (PEM-formatted <c>string</c>) to use for signature verification.</param>
/// <returns>Whether the data's signature verification succeeded or not.</returns>
public bool Verify(string data, string signature, string publicKeyPem)
{
if (string.IsNullOrEmpty(data) || string.IsNullOrEmpty(signature) || string.IsNullOrEmpty(publicKeyPem))
{
return false;
}
try
{
byte[] signatureBytes = Convert.FromBase64String(signature);
return Verify(Encoding.UTF8.GetBytes(data), signatureBytes, publicKeyPem);
}
catch
{
return false;
}
}
/// <summary>
/// Signs the specified data <c>byte[]</c> array using the provided private RSA key (which needs to be a PEM-formatted <c>string</c>).<para> </para>
/// If the procedure succeeds, the calculated signature <c>byte[]</c> array is returned. Otherwise,
/// an empty <c>byte[]</c> array is returned if the provided <paramref name="data"/> and/or <paramref name="privateKeyPem"/> parameters
/// were <c>null</c> or empty. If the procedure fails entirely, <c>null</c> is returned.
/// </summary>
/// <param name="data">The data to sign.</param>
/// <param name="privateKeyPem">The private RSA key to use for generating the signature (PEM-formatted <c>string</c>)</param>
/// <returns>The signature (<c>byte[]</c>), <c>string.Empty</c> if the provided <paramref name="data"/> and/or <paramref name="privateKeyPem"/> parameters were <c>null</c> or empty. Returns <c>null</c> if signing failed entirely.</returns>
/// <seealso cref="SignerUtilities"/>
public byte[] Sign(byte[] data, string privateKeyPem)
{
if (data is null || data.Length == 0 || string.IsNullOrEmpty(privateKeyPem))
{
return Array.Empty<byte>();
}
try
{
ISigner sig = SignerUtilities.GetSigner(signatureAlgo);
sig.Init(true, PemStringToKeyPair(privateKeyPem).Private);
sig.BlockUpdate(data, 0, data.Length);
return sig.GenerateSignature();
}
catch
{
return null;
}
}
/// <summary>
/// Verifies a signature that was obtained using <see cref="Sign(byte[],string)"/> with a public RSA key (which needs to be a PEM-formatted <c>string</c>).<para> </para>
/// </summary>
/// <param name="data">The data whose signature you want to verify.</param>
/// <param name="signature">The passed <paramref name="data"/>'s signature (return value of <see cref="Sign(byte[],string)"/>).</param>
/// <param name="publicKeyPem">The public RSA key (PEM-formatted <c>string</c>) to use for signature verification.</param>
/// <returns>Whether the data's signature verification succeeded or not.</returns>
public bool Verify(byte[] data, byte[] signature, string publicKeyPem)
{
if (data is null || data.Length == 0 || string.IsNullOrEmpty(publicKeyPem))
{
return false;
}
try
{
ISigner signer = SignerUtilities.GetSigner(signatureAlgo);
signer.Init(false, PemStringToKeyParameters(publicKeyPem));
signer.BlockUpdate(data, 0, data.Length);
return signer.VerifySignature(signature);
}
catch
{
return false;
}
}
#endregion
#region Conversions
/// <summary>
/// Tries to convert a PEM-formatted <c>string</c> => <see cref="AsymmetricCipherKeyPair"/>.<para> </para>
/// Only possible if the provided key is the private key (public keys are typically read with the <see cref="PemReader"/> as <see cref="RsaKeyParameters"/>).
/// </summary>
/// <param name="rsaKeyPem">The PEM-formatted key <c>string</c> to convert.</param>
/// <returns>The converted <see cref="AsymmetricCipherKeyPair"/>; <c>null</c> if the provided key <c>string</c> was <c>null</c>, empty or the public key.</returns>
private static AsymmetricCipherKeyPair PemStringToKeyPair(string rsaKeyPem)
{
if (string.IsNullOrEmpty(rsaKeyPem))
{
return null;
}
var stringReader = new StringReader(rsaKeyPem);
try
{
var pemReader = new PemReader(stringReader);
return pemReader.ReadObject() as AsymmetricCipherKeyPair;
}
catch
{
return null;
}
finally
{
stringReader.Dispose();
}
}
/// <summary>
/// Tries to convert a PEM-formatted <c>string</c> => <see cref="RsaKeyParameters"/>.<para> </para>
/// </summary>
/// <param name="rsaKeyPem">The PEM-formatted key <c>string</c> to convert.</param>
/// <returns>The converted <see cref="RsaKeyParameters"/>; <c>null</c> if the provided key <c>string</c> was <c>null</c> or empty.</returns>
private static RsaKeyParameters PemStringToKeyParameters(string rsaKeyPem)
{
if (string.IsNullOrEmpty(rsaKeyPem))
{
return null;
}
var stringReader = new StringReader(rsaKeyPem);
try
{
var pemReader = new PemReader(stringReader);
return pemReader.ReadObject() as RsaKeyParameters;
}
catch
{
return null;
}
finally
{
stringReader.Dispose();
}
}
/// <summary>
/// Encrypts or decrypts the input <paramref name="data"/> parameter
/// according to the <paramref name="encrypt"/> <c>bool</c> parameter, using the provided RSA <paramref name="key"/>.<para> </para>
/// If <paramref name="encrypt"/> is set to <c>false</c>, the method will try to decrypt instead.<para> </para>
/// This method can throw exceptions! E.g. don't pass any <c>null</c> or invalid arguments.
/// Trying to decrypt with a <c>null</c> or public <paramref name="key"/> will throw exceptions! Make sure to wrap the call to this method in a try/catch block.
/// </summary>
/// <param name="data">The data to encrypt or decrypt</param>
/// <param name="key">The RSA key to use for encryption/decryption.</param>
/// <param name="encrypt">Should the method encrypt the passed input <paramref name="data"/> or attempt to decrypt it?</param>
/// <returns>The processed data <c>byte[]</c> array; exceptions are thrown in case of a failure.</returns>
private static byte[] ProcessData(byte[] data, ICipherParameters key, bool encrypt)
{
// PKCS1 OAEP paddings
OaepEncoding eng = new OaepEncoding(new RsaEngine());
eng.Init(encrypt, key);
int length = data.Length;
int blockSize = eng.GetInputBlockSize();
List<byte> processedBytes = new List<byte>(length);
for (int chunkPosition = 0; chunkPosition < length; chunkPosition += blockSize)
{
int chunkSize = Math.Min(blockSize, length - chunkPosition);
processedBytes.AddRange(eng.ProcessBlock(data, chunkPosition, chunkSize));
}
return processedBytes.ToArray();
}
#endregion
}
}