forked from ServiceStack/ServiceStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCryptUtils.cs
More file actions
465 lines (407 loc) · 16.9 KB
/
CryptUtils.cs
File metadata and controls
465 lines (407 loc) · 16.9 KB
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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
// Copyright (c) Service Stack LLC. All Rights Reserved.
// License: https://raw.github.com/ServiceStack/ServiceStack/master/license.txt
#if !(PCL || SL5 || NETSTANDARD1_1) || __IOS__ || ANDROID
using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;
using ServiceStack.Text;
namespace ServiceStack
{
public enum RsaKeyLengths
{
Bit1024 = 1024,
Bit2048 = 2048,
Bit4096 = 4096
}
public class RsaKeyPair
{
public string PrivateKey { get; set; }
public string PublicKey { get; set; }
}
/// <summary>
/// Useful .NET Encryption Utils from:
/// https://msdn.microsoft.com/en-us/library/system.security.cryptography.rsacryptoserviceprovider(v=vs.110).aspx
/// </summary>
public static class RsaUtils
{
public static RsaKeyLengths KeyLength = RsaKeyLengths.Bit2048;
public static RsaKeyPair DefaultKeyPair;
public static bool DoOAEPPadding = true;
public static RsaKeyPair CreatePublicAndPrivateKeyPair(RsaKeyLengths rsaKeyLength = RsaKeyLengths.Bit2048)
{
using (var rsa = new RSACryptoServiceProvider((int)rsaKeyLength))
{
return new RsaKeyPair
{
PrivateKey = rsa.ToXmlString(true),
PublicKey = rsa.ToXmlString(false),
};
}
}
public static RSAParameters CreatePrivateKeyParams(RsaKeyLengths rsaKeyLength = RsaKeyLengths.Bit2048)
{
using (var rsa = new RSACryptoServiceProvider((int)rsaKeyLength))
{
return rsa.ExportParameters(includePrivateParameters: true);
}
}
public static string FromPrivateRSAParameters(this RSAParameters privateKey)
{
using (var rsa = new RSACryptoServiceProvider())
{
rsa.ImportParameters(privateKey);
return rsa.ToXmlString(includePrivateParameters: true);
}
}
public static string FromPublicRSAParameters(this RSAParameters publicKey)
{
using (var rsa = new RSACryptoServiceProvider())
{
rsa.ImportParameters(publicKey);
return rsa.ToXmlString(includePrivateParameters: false);
}
}
public static RSAParameters ToPrivateRSAParameters(this string privateKeyXml)
{
using (var rsa = new RSACryptoServiceProvider())
{
rsa.FromXmlString(privateKeyXml);
return rsa.ExportParameters(includePrivateParameters: true);
}
}
public static RSAParameters ToPublicRSAParameters(this string publicKeyXml)
{
using (var rsa = new RSACryptoServiceProvider())
{
rsa.FromXmlString(publicKeyXml);
return rsa.ExportParameters(includePrivateParameters: false);
}
}
public static string ToPublicKeyXml(this RSAParameters publicKey)
{
using (var rsa = new RSACryptoServiceProvider())
{
rsa.ImportParameters(publicKey);
return rsa.ToXmlString(includePrivateParameters: false);
}
}
public static RSAParameters ToPublicRsaParameters(this RSAParameters privateKey)
{
using (var rsa = new RSACryptoServiceProvider())
{
rsa.ImportParameters(privateKey);
return rsa.ExportParameters(includePrivateParameters: false);
}
}
public static string ToPrivateKeyXml(this RSAParameters privateKey)
{
using (var rsa = new RSACryptoServiceProvider())
{
rsa.ImportParameters(privateKey);
return rsa.ToXmlString(includePrivateParameters: true);
}
}
public static string Encrypt(this string text)
{
if (DefaultKeyPair != null)
return Encrypt(text, DefaultKeyPair.PublicKey, KeyLength);
throw new ArgumentNullException("DefaultKeyPair", "No KeyPair given for encryption in CryptUtils");
}
public static string Decrypt(this string text)
{
if (DefaultKeyPair != null)
return Decrypt(text, DefaultKeyPair.PrivateKey, KeyLength);
else
throw new ArgumentNullException("DefaultKeyPair", "No KeyPair given for encryption in CryptUtils");
}
public static string Encrypt(string text, string publicKeyXml, RsaKeyLengths rsaKeyLength = RsaKeyLengths.Bit2048)
{
var bytes = Encoding.UTF8.GetBytes(text);
var encryptedBytes = Encrypt(bytes, publicKeyXml, rsaKeyLength);
string encryptedData = Convert.ToBase64String(encryptedBytes);
return encryptedData;
}
public static string Encrypt(string text, RSAParameters publicKey, RsaKeyLengths rsaKeyLength = RsaKeyLengths.Bit2048)
{
var bytes = Encoding.UTF8.GetBytes(text);
var encryptedBytes = Encrypt(bytes, publicKey, rsaKeyLength);
string encryptedData = Convert.ToBase64String(encryptedBytes);
return encryptedData;
}
public static byte[] Encrypt(byte[] bytes, string publicKeyXml, RsaKeyLengths rsaKeyLength = RsaKeyLengths.Bit2048)
{
using (var rsa = new RSACryptoServiceProvider((int)rsaKeyLength))
{
rsa.FromXmlString(publicKeyXml);
return rsa.Encrypt(bytes, DoOAEPPadding);
}
}
public static byte[] Encrypt(byte[] bytes, RSAParameters publicKey, RsaKeyLengths rsaKeyLength = RsaKeyLengths.Bit2048)
{
using (var rsa = new RSACryptoServiceProvider((int)rsaKeyLength))
{
rsa.ImportParameters(publicKey);
return rsa.Encrypt(bytes, DoOAEPPadding);
}
}
public static string Decrypt(string encryptedText, string privateKeyXml, RsaKeyLengths rsaKeyLength = RsaKeyLengths.Bit2048)
{
var encryptedBytes = Convert.FromBase64String(encryptedText);
var bytes = Decrypt(encryptedBytes, privateKeyXml, rsaKeyLength);
var data = Encoding.UTF8.GetString(bytes);
return data;
}
public static string Decrypt(string encryptedText, RSAParameters privateKey, RsaKeyLengths rsaKeyLength = RsaKeyLengths.Bit2048)
{
var encryptedBytes = Convert.FromBase64String(encryptedText);
var bytes = Decrypt(encryptedBytes, privateKey, rsaKeyLength);
var data = Encoding.UTF8.GetString(bytes);
return data;
}
public static byte[] Decrypt(byte[] encryptedBytes, string privateKeyXml, RsaKeyLengths rsaKeyLength = RsaKeyLengths.Bit2048)
{
using (var rsa = new RSACryptoServiceProvider((int)rsaKeyLength))
{
rsa.FromXmlString(privateKeyXml);
byte[] bytes = rsa.Decrypt(encryptedBytes, DoOAEPPadding);
return bytes;
}
}
public static byte[] Decrypt(byte[] encryptedBytes, RSAParameters privateKey, RsaKeyLengths rsaKeyLength = RsaKeyLengths.Bit2048)
{
using (var rsa = new RSACryptoServiceProvider((int)rsaKeyLength))
{
rsa.ImportParameters(privateKey);
byte[] bytes = rsa.Decrypt(encryptedBytes, DoOAEPPadding);
return bytes;
}
}
public static byte[] Authenticate(byte[] dataToSign, RSAParameters privateKey, string hashAlgorithm = "SHA512", RsaKeyLengths rsaKeyLength = RsaKeyLengths.Bit2048)
{
using (var rsa = new RSACryptoServiceProvider((int)rsaKeyLength))
{
rsa.ImportParameters(privateKey);
//.NET 4.5 doesn't let you specify padding, defaults to PKCS#1 v1.5 padding
var signature = rsa.SignData(dataToSign, hashAlgorithm);
return signature;
}
}
public static bool Verify(byte[] dataToVerify, byte[] signature, RSAParameters publicKey, string hashAlgorithm = "SHA512", RsaKeyLengths rsaKeyLength = RsaKeyLengths.Bit2048)
{
using (var rsa = new RSACryptoServiceProvider((int)rsaKeyLength))
{
rsa.ImportParameters(publicKey);
var verified = rsa.VerifyData(dataToVerify, hashAlgorithm, signature);
return verified;
}
}
}
public static class HashUtils
{
public static HashAlgorithm GetHashAlgorithm(string hashAlgorithm)
{
switch (hashAlgorithm)
{
case "SHA1":
return new SHA1Managed();
case "SHA256":
return new SHA256Managed();
case "SHA512":
return new SHA512Managed();
default:
throw new NotSupportedException(hashAlgorithm);
}
}
}
public static class AesUtils
{
public const int KeySize = 256;
public const int KeySizeBytes = 256 / 8;
public const int BlockSize = 128;
public const int BlockSizeBytes = 128 / 8;
public static SymmetricAlgorithm CreateSymmetricAlgorithm()
{
return new AesCryptoServiceProvider
{
KeySize = KeySize,
BlockSize = BlockSize,
Mode = CipherMode.CBC,
Padding = PaddingMode.PKCS7
};
}
public static byte[] CreateKey()
{
using (var aes = CreateSymmetricAlgorithm())
{
return aes.Key;
}
}
public static byte[] CreateIv()
{
using (var aes = CreateSymmetricAlgorithm())
{
return aes.IV;
}
}
public static void CreateKeyAndIv(out byte[] cryptKey, out byte[] iv)
{
using (var aes = CreateSymmetricAlgorithm())
{
cryptKey = aes.Key;
iv = aes.IV;
}
}
public static void CreateCryptAuthKeysAndIv(out byte[] cryptKey, out byte[] authKey, out byte[] iv)
{
using (var aes = CreateSymmetricAlgorithm())
{
cryptKey = aes.Key;
iv = aes.IV;
}
using (var aes = CreateSymmetricAlgorithm())
{
authKey = aes.Key;
}
}
public static string Encrypt(string text, byte[] cryptKey, byte[] iv)
{
var encBytes = Encrypt(text.ToUtf8Bytes(), cryptKey, iv);
return Convert.ToBase64String(encBytes);
}
public static byte[] Encrypt(byte[] bytesToEncrypt, byte[] cryptKey, byte[] iv)
{
using (var aes = CreateSymmetricAlgorithm())
using (var encrypter = aes.CreateEncryptor(cryptKey, iv))
using (var cipherStream = MemoryStreamFactory.GetStream())
{
using (var cryptoStream = new CryptoStream(cipherStream, encrypter, CryptoStreamMode.Write))
using (var binaryWriter = new BinaryWriter(cryptoStream))
{
binaryWriter.Write(bytesToEncrypt);
}
return cipherStream.ToArray();
}
}
public static string Decrypt(string encryptedBase64, byte[] cryptKey, byte[] iv)
{
var bytes = Decrypt(Convert.FromBase64String(encryptedBase64), cryptKey, iv);
return bytes.FromUtf8Bytes();
}
public static byte[] Decrypt(byte[] encryptedBytes, byte[] cryptKey, byte[] iv)
{
using (var aes = CreateSymmetricAlgorithm())
using (var decryptor = aes.CreateDecryptor(cryptKey, iv))
using (var ms = MemoryStreamFactory.GetStream(encryptedBytes))
using (var cryptStream = new CryptoStream(ms, decryptor, CryptoStreamMode.Read))
{
return cryptStream.ReadFully();
}
}
}
/*
* Original Source:
* This work (Modern Encryption of a String C#, by James Tuley),
* identified by James Tuley, is free of known copyright restrictions.
* https://gist.github.com/4336842
* http://creativecommons.org/publicdomain/mark/1.0/
*/
public static class HmacUtils
{
public const int KeySize = 256;
public const int KeySizeBytes = 256 / 8;
public static HMAC CreateHashAlgorithm(byte[] authKey)
{
return new HMACSHA256(authKey);
}
public static byte[] Authenticate(byte[] encryptedBytes, byte[] authKey, byte[] iv)
{
using (var hmac = CreateHashAlgorithm(authKey))
using (var ms = MemoryStreamFactory.GetStream())
{
using (var writer = new BinaryWriter(ms))
{
//Prepend IV
writer.Write(iv);
//Write Ciphertext
writer.Write(encryptedBytes);
writer.Flush();
//Authenticate all data
var tag = hmac.ComputeHash(ms.ToArray());
//Postpend tag
writer.Write(tag);
}
return ms.ToArray();
}
}
public static bool Verify(byte[] authEncryptedBytes, byte[] authKey)
{
if (authKey == null || authKey.Length != KeySizeBytes)
throw new ArgumentException($"AuthKey needs to be {KeySize} bits", nameof(authKey));
if (authEncryptedBytes == null || authEncryptedBytes.Length == 0)
throw new ArgumentException("Encrypted Message Required!", nameof(authEncryptedBytes));
using (var hmac = CreateHashAlgorithm(authKey))
{
var sentTag = new byte[KeySizeBytes];
//Calculate Tag
var calcTag = hmac.ComputeHash(authEncryptedBytes, 0, authEncryptedBytes.Length - sentTag.Length);
const int ivLength = AesUtils.BlockSizeBytes;
//return false if message length is too small
if (authEncryptedBytes.Length < sentTag.Length + ivLength)
return false;
//Grab Sent Tag
Buffer.BlockCopy(authEncryptedBytes, authEncryptedBytes.Length - sentTag.Length, sentTag, 0, sentTag.Length);
//Compare Tag with constant time comparison
var compare = 0;
for (var i = 0; i < sentTag.Length; i++)
compare |= sentTag[i] ^ calcTag[i];
//return false if message doesn't authenticate
if (compare != 0)
return false;
}
return true; //Haz Success!
}
public static byte[] DecryptAuthenticated(byte[] authEncryptedBytes, byte[] cryptKey)
{
if (cryptKey == null || cryptKey.Length != KeySizeBytes)
throw new ArgumentException($"CryptKey needs to be {KeySize} bits", nameof(cryptKey));
//Grab IV from message
var iv = new byte[AesUtils.BlockSizeBytes];
Buffer.BlockCopy(authEncryptedBytes, 0, iv, 0, iv.Length);
using (var aes = AesUtils.CreateSymmetricAlgorithm())
{
using (var decrypter = aes.CreateDecryptor(cryptKey, iv))
using (var decryptedStream = MemoryStreamFactory.GetStream())
{
using (var decrypterStream = new CryptoStream(decryptedStream, decrypter, CryptoStreamMode.Write))
using (var writer = new BinaryWriter(decrypterStream))
{
//Decrypt Cipher Text from Message
writer.Write(
authEncryptedBytes,
iv.Length,
authEncryptedBytes.Length - iv.Length - KeySizeBytes);
}
return decryptedStream.ToArray();
}
}
}
}
[Obsolete("Use RsaUtils.* static class")]
public class CryptUtils
{
public static string Encrypt(string publicKeyXml, string data, RsaKeyLengths rsaKeyLength = RsaKeyLengths.Bit2048)
{
return RsaUtils.Encrypt(data, publicKeyXml, rsaKeyLength);
}
public static string Decrypt(string privateKeyXml, string encryptedData, RsaKeyLengths rsaKeyLength = RsaKeyLengths.Bit2048)
{
return RsaUtils.Encrypt(encryptedData, privateKeyXml, rsaKeyLength);
}
public static RsaKeyPair CreatePublicAndPrivateKeyPair(RsaKeyLengths rsaKeyLength = RsaKeyLengths.Bit2048)
{
return RsaUtils.CreatePublicAndPrivateKeyPair(rsaKeyLength);
}
}
}
#endif