1
1
2
2
using System ;
3
- using System . Diagnostics ;
4
3
using System . IO ;
5
4
using System . Security . Cryptography ;
6
5
using System . Security . Cryptography . X509Certificates ;
@@ -48,12 +47,10 @@ public static void EncryptUsingPublicKey(this X509Certificate2 x509WithPublicKey
48
47
if ( null == outputStream ) throw new ArgumentNullException ( nameof ( outputStream ) ) ;
49
48
if ( null == dataEncryptionAlgorithmName ) throw new ArgumentNullException ( nameof ( dataEncryptionAlgorithmName ) ) ;
50
49
51
- MyTrace . Entering ( ) ;
52
-
53
50
try
54
51
{
55
- if ( null == x509WithPublicKey . PublicKey ) throw new ArgumentException ( "X509Certificate2.PublicKey was NULL." ) ;
56
- if ( null == x509WithPublicKey . PublicKey . Key ) throw new ArgumentException ( "X509Certificate2.PublicKey.Key was NULL." ) ;
52
+ if ( null == x509WithPublicKey . PublicKey ) throw new Exception ( $ "X509Certificate2.PublicKey was NULL. Cert: { x509WithPublicKey . Thumbprint } ") ;
53
+ if ( null == x509WithPublicKey . PublicKey . Key ) throw new Exception ( $ "X509Certificate2.PublicKey.Key was NULL. Cert: { x509WithPublicKey . Thumbprint } ") ;
57
54
58
55
// IMP: We didn't create the Cert. DO NOT DISPOSE.
59
56
// IMP: Disposing the AsymmetricAlgorithm will render the X509Certificate2 useless for subsequent use.
@@ -63,26 +60,28 @@ public static void EncryptUsingPublicKey(this X509Certificate2 x509WithPublicKey
63
60
{
64
61
if ( null == dataEncryptionAlgorithm ) throw new Exception ( $ "SymmetricAlgorithm.Create('{ dataEncryptionAlgorithmName } ') returned NULL.") ;
65
62
63
+ // Set desired key and block size.
64
+ // This may throw an excepion on invalid key/block sizes.
66
65
dataEncryptionAlgorithm . KeySize = keySize ;
67
66
dataEncryptionAlgorithm . BlockSize = blockSize ;
68
67
69
68
// The DataEncryptionKey and IV.
70
- byte [ ] dataEncryptionKey = dataEncryptionAlgorithm . Key ;
71
- byte [ ] dataEncryptionIV = dataEncryptionAlgorithm . IV ;
69
+ byte [ ] dataEncryptionKey = dataEncryptionAlgorithm . Key ?? throw new Exception ( "dataEncryptionAlgorithm.Key was NULL." ) ;
70
+ byte [ ] dataEncryptionIV = dataEncryptionAlgorithm . IV ?? throw new Exception ( "dataEncryptionAlgorithm.IV was NULL." ) ;
72
71
73
72
// Encrypt the DEK using the X509 public key (KEK).
74
73
var keyFormatter = new RSAPKCS1KeyExchangeFormatter ( keyEncryptionAlgorithm ) ;
75
74
byte [ ] encryptedDataEncryptionKey = keyFormatter . CreateKeyExchange ( dataEncryptionKey ) ;
76
75
77
- // Debug information (Set Trace to warning or above for PRD)
76
+ // Essential debug information...
78
77
MyTrace . Info ( ( ) => $ "KEK: { keyEncryptionAlgorithm . GetType ( ) . Name } / { keyEncryptionAlgorithm . KeySize } bits / { x509WithPublicKey . Thumbprint } ") ;
79
78
MyTrace . Info ( ( ) => $ "DEK: { dataEncryptionAlgorithm . GetType ( ) . Name } / { dataEncryptionAlgorithm . KeySize } bits. / BlockSize: { dataEncryptionAlgorithm . BlockSize } bits.") ;
80
79
81
- // Write the length & bytes of encrypted DEK and IV
80
+ // Write the EncryptedDEK and the IV (length & bytes)
82
81
outputStream . WriteLengthAndBytes ( encryptedDataEncryptionKey ) ;
83
82
outputStream . WriteLengthAndBytes ( dataEncryptionIV ) ;
84
83
85
- // Write Data
84
+ // Write encrypted data
86
85
using ( var transform = dataEncryptionAlgorithm . CreateEncryptor ( ) )
87
86
using ( var cryptoStream = new CryptoStream ( outputStream , transform , CryptoStreamMode . Write ) )
88
87
{
@@ -110,16 +109,9 @@ public static void DecryptUsingPrivateKey(this X509Certificate2 x509WithPrivateK
110
109
if ( null == x509WithPrivateKey ) throw new ArgumentNullException ( nameof ( x509WithPrivateKey ) ) ;
111
110
if ( null == dataEncryptionAlgorithmName ) throw new ArgumentNullException ( nameof ( dataEncryptionAlgorithmName ) ) ;
112
111
113
- MyTrace . Entering ( ) ;
114
-
115
- // Data Encryption key (DEK) is read from the stream.
116
- // DEK itself comes encrypted using the Key encryption key (KEK)
117
- // Use X509 cert private key to decrypt the DEK
118
- // Use the DEK to decrypt the data
119
-
120
112
try
121
113
{
122
- if ( null == x509WithPrivateKey . PrivateKey ) throw new ArgumentException ( "X509Certificate2.PrivateKey was NULL." ) ;
114
+ if ( null == x509WithPrivateKey . PrivateKey ) throw new Exception ( $ "X509Certificate2.PrivateKey was NULL. Cert: { x509WithPrivateKey . Thumbprint } ") ;
123
115
124
116
// IMP: We didn't create the Cert. DO NOT DISPOSE.
125
117
// IMP: Disposing the AsymmetricAlgorithm will render the X509Certificate2 useless for subsequent use.
@@ -234,18 +226,6 @@ static Int32 ReadInt32(this Stream inputStream)
234
226
235
227
return BitConverter . ToInt32 ( fourBytes , startIndex : 0 ) ;
236
228
}
237
-
238
- /// <summary>
239
- /// Because, .Net core doesn't honor TraceSwitches from the config files.
240
- /// Try TraceSwitch "Org.Security.Cryptography" in config files.
241
- /// If it doesn't work, update me.
242
- /// </summary>
243
- public static TraceLevel TraceLevel
244
- {
245
- set {
246
- MyTrace . MyTraceSwitch = new TraceSwitch ( "" , "" , value . ToString ( ) ) ;
247
- }
248
- }
249
229
}
250
230
}
251
231
0 commit comments