@@ -51,7 +51,7 @@ public AesEncryptionService(
5151 {
5252 encryptionKeyIdentifier , key
5353 }
54- } , new List < byte [ ] > ( 0 ) )
54+ } , [ ] )
5555 {
5656 }
5757
@@ -62,7 +62,7 @@ public AesEncryptionService(
6262 /// <param name="keys">A dictionary of available encryption keys and their identifiers for encryption and decryption.</param>
6363 public AesEncryptionService (
6464 string encryptionKeyIdentifier ,
65- IDictionary < string , byte [ ] > keys ) : this ( encryptionKeyIdentifier , keys , new List < byte [ ] > ( 0 ) )
65+ IDictionary < string , byte [ ] > keys ) : this ( encryptionKeyIdentifier , keys , [ ] )
6666 {
6767 }
6868
@@ -77,9 +77,9 @@ public AesEncryptionService(
7777 IDictionary < string , byte [ ] > keys ,
7878 IList < byte [ ] > decryptionKeys )
7979 {
80- Guard . AgainstNullAndEmpty ( nameof ( encryptionKeyIdentifier ) , encryptionKeyIdentifier ) ;
81- Guard . AgainstNull ( nameof ( keys ) , keys ) ;
82- Guard . AgainstNull ( nameof ( decryptionKeys ) , decryptionKeys ) ;
80+ ArgumentException . ThrowIfNullOrWhiteSpace ( encryptionKeyIdentifier ) ;
81+ ArgumentNullException . ThrowIfNull ( keys ) ;
82+ ArgumentNullException . ThrowIfNull ( decryptionKeys ) ;
8383
8484 this . encryptionKeyIdentifier = encryptionKeyIdentifier ;
8585 this . decryptionKeys = decryptionKeys ;
@@ -127,28 +127,26 @@ public EncryptedValue Encrypt(string value, IOutgoingLogicalMessageContext conte
127127
128128 AddKeyIdentifierHeader ( context ) ;
129129
130- using ( var aes = Aes . Create ( ) )
130+ using var aes = Aes . Create ( ) ;
131+
132+ aes . Key = encryptionKey ;
133+ aes . Mode = CipherMode . CBC ;
134+ ConfigureIV ( aes ) ;
135+
136+ using var encryptor = aes . CreateEncryptor ( ) ;
137+ using var memoryStream = new MemoryStream ( ) ;
138+ using var cryptoStream = new CryptoStream ( memoryStream , encryptor , CryptoStreamMode . Write ) ;
139+ using var writer = new StreamWriter ( cryptoStream ) ;
140+
141+ writer . Write ( value ) ;
142+ writer . Flush ( ) ;
143+ cryptoStream . Flush ( ) ;
144+ cryptoStream . FlushFinalBlock ( ) ;
145+ return new EncryptedValue
131146 {
132- aes . Key = encryptionKey ;
133- aes . Mode = CipherMode . CBC ;
134- ConfigureIV ( aes ) ;
135-
136- using ( var encryptor = aes . CreateEncryptor ( ) )
137- using ( var memoryStream = new MemoryStream ( ) )
138- using ( var cryptoStream = new CryptoStream ( memoryStream , encryptor , CryptoStreamMode . Write ) )
139- using ( var writer = new StreamWriter ( cryptoStream ) )
140- {
141- writer . Write ( value ) ;
142- writer . Flush ( ) ;
143- cryptoStream . Flush ( ) ;
144- cryptoStream . FlushFinalBlock ( ) ;
145- return new EncryptedValue
146- {
147- EncryptedBase64Value = Convert . ToBase64String ( memoryStream . ToArray ( ) ) ,
148- Base64Iv = Convert . ToBase64String ( aes . IV )
149- } ;
150- }
151- }
147+ EncryptedBase64Value = Convert . ToBase64String ( memoryStream . ToArray ( ) ) ,
148+ Base64Iv = Convert . ToBase64String ( aes . IV )
149+ } ;
152150 }
153151
154152 string DecryptUsingKeyIdentifier ( EncryptedValue encryptedValue , string keyIdentifier )
@@ -191,20 +189,21 @@ string DecryptUsingAllKeys(EncryptedValue encryptedValue)
191189 static string Decrypt ( EncryptedValue encryptedValue , byte [ ] key )
192190 {
193191 var iv = Convert . FromBase64String ( encryptedValue . Base64Iv ) ;
194- using ( var aes = Aes . Create ( ) )
195- {
196- var encrypted = Convert . FromBase64String ( encryptedValue . EncryptedBase64Value ) ;
197- aes . IV = iv ;
198- aes . Mode = CipherMode . CBC ;
199- aes . Key = key ;
200- using ( var decryptor = aes . CreateDecryptor ( ) )
201- using ( var memoryStream = new MemoryStream ( encrypted ) )
202- using ( var cryptoStream = new CryptoStream ( memoryStream , decryptor , CryptoStreamMode . Read ) )
203- using ( var reader = new StreamReader ( cryptoStream ) )
204- {
205- return reader . ReadToEnd ( ) ;
206- }
207- }
192+
193+ var encrypted = Convert . FromBase64String ( encryptedValue . EncryptedBase64Value ) ;
194+
195+ using var aes = Aes . Create ( ) ;
196+
197+ aes . IV = iv ;
198+ aes . Mode = CipherMode . CBC ;
199+ aes . Key = key ;
200+
201+ using var decryptor = aes . CreateDecryptor ( ) ;
202+ using var memoryStream = new MemoryStream ( encrypted ) ;
203+ using var cryptoStream = new CryptoStream ( memoryStream , decryptor , CryptoStreamMode . Read ) ;
204+ using var reader = new StreamReader ( cryptoStream ) ;
205+
206+ return reader . ReadToEnd ( ) ;
208207 }
209208
210209 static void VerifyExpiredKeys ( IList < byte [ ] > keys )
@@ -233,48 +232,39 @@ static void VerifyEncryptionKey(byte[] key)
233232
234233 static bool IsValidKey ( byte [ ] key )
235234 {
236- using ( var aes = Aes . Create ( ) )
237- {
238- var bitLength = key . Length * 8 ;
235+ using var aes = Aes . Create ( ) ;
239236
240- var maxValidKeyBitLength = aes . LegalKeySizes . Max ( keyLength => keyLength . MaxSize ) ;
241- if ( bitLength < maxValidKeyBitLength )
242- {
243- Log . WarnFormat ( "Encryption key is {0} bits which is less than the maximum allowed {1} bits. Consider using a {2}-bit encryption key to obtain the maximum cipher strength" , bitLength , maxValidKeyBitLength , maxValidKeyBitLength ) ;
244- }
237+ var bitLength = key . Length * 8 ;
245238
246- return aes . ValidKeySize ( bitLength ) ;
239+ var maxValidKeyBitLength = aes . LegalKeySizes . Max ( keyLength => keyLength . MaxSize ) ;
240+ if ( bitLength < maxValidKeyBitLength )
241+ {
242+ Log . WarnFormat ( "Encryption key is {0} bits which is less than the maximum allowed {1} bits. Consider using a {2}-bit encryption key to obtain the maximum cipher strength" , bitLength , maxValidKeyBitLength , maxValidKeyBitLength ) ;
247243 }
244+
245+ return aes . ValidKeySize ( bitLength ) ;
248246 }
249247
250248 /// <summary>
251249 /// Adds the key identifier of the currently used encryption key to the outgoing message's headers.
252250 /// </summary>
253- protected internal virtual void AddKeyIdentifierHeader ( IOutgoingLogicalMessageContext context )
254- {
255- context . Headers [ EncryptionHeaders . EncryptionKeyIdentifier ] = encryptionKeyIdentifier ;
256- }
251+ protected internal virtual void AddKeyIdentifierHeader ( IOutgoingLogicalMessageContext context ) => context . Headers [ EncryptionHeaders . EncryptionKeyIdentifier ] = encryptionKeyIdentifier ;
257252
258253 /// <summary>
259254 /// Tries to locate an encryption key identifier from an incoming message.
260255 /// </summary>
261- protected internal virtual bool TryGetKeyIdentifierHeader ( out string keyIdentifier , IIncomingLogicalMessageContext context )
262- {
263- return context . Headers . TryGetValue ( EncryptionHeaders . EncryptionKeyIdentifier , out keyIdentifier ) ;
264- }
256+ protected internal virtual bool TryGetKeyIdentifierHeader ( out string keyIdentifier , IIncomingLogicalMessageContext context ) => context . Headers . TryGetValue ( EncryptionHeaders . EncryptionKeyIdentifier , out keyIdentifier ) ;
265257
266258 /// <summary>
267259 /// Configures the initialization vector.
268260 /// </summary>
269- protected internal virtual void ConfigureIV ( Aes aes )
270- {
271- aes . GenerateIV ( ) ;
272- }
261+ protected internal virtual void ConfigureIV ( Aes aes ) => aes . GenerateIV ( ) ;
273262
274263 readonly string encryptionKeyIdentifier ;
275- IList < byte [ ] > decryptionKeys ; // Required, as we decrypt in the configured order.
276- byte [ ] encryptionKey ;
277- IDictionary < string , byte [ ] > keys ;
264+ readonly IList < byte [ ] > decryptionKeys ; // Required, as we decrypt in the configured order.
265+ readonly byte [ ] encryptionKey ;
266+ readonly IDictionary < string , byte [ ] > keys ;
267+
278268 static readonly ILog Log = LogManager . GetLogger < AesEncryptionService > ( ) ;
279269 }
280270}
0 commit comments