1- using System . IO ;
1+ using System ;
2+ using System . IO ;
23using System . Security . Cryptography ;
34
45namespace Microsoft . EntityFrameworkCore . DataEncryption . Providers ;
@@ -19,9 +20,9 @@ public class AesProvider : IEncryptionProvider
1920 public const int InitializationVectorSize = 16 ;
2021
2122 private readonly byte [ ] _key ;
23+ private readonly byte [ ] _iv ;
2224 private readonly CipherMode _mode ;
2325 private readonly PaddingMode _padding ;
24- private readonly byte [ ] _iv ;
2526
2627 /// <summary>
2728 /// Creates a new <see cref="AesProvider"/> instance used to perform symmetric encryption and decryption on strings.
@@ -32,8 +33,8 @@ public class AesProvider : IEncryptionProvider
3233 /// <param name="padding">Padding mode used in the symmetric encryption.</param>
3334 public AesProvider ( byte [ ] key , byte [ ] initializationVector , CipherMode mode = CipherMode . CBC , PaddingMode padding = PaddingMode . PKCS7 )
3435 {
35- _key = key ;
36- _iv = initializationVector ;
36+ _key = key ?? throw new ArgumentNullException ( nameof ( key ) , "" ) ;
37+ _iv = initializationVector ?? throw new ArgumentNullException ( nameof ( initializationVector ) , "" ) ;
3738 _mode = mode ;
3839 _padding = padding ;
3940 }
@@ -46,25 +47,16 @@ public byte[] Encrypt(byte[] input)
4647 return null ;
4748 }
4849
49- using var aes = CreateCryptographyProvider ( _key , _mode , _padding ) ;
50- using var memoryStream = new MemoryStream ( ) ;
51-
52- byte [ ] initializationVector = _iv ;
53- if ( initializationVector is null )
54- {
55- aes . GenerateIV ( ) ;
56- initializationVector = aes . IV ;
57- memoryStream . Write ( initializationVector , 0 , initializationVector . Length ) ;
58- }
50+ using Aes aes = CreateCryptographyProvider ( _key , _iv , _mode , _padding ) ;
51+ using ICryptoTransform transform = aes . CreateEncryptor ( aes . Key , aes . IV ) ;
52+ using MemoryStream memoryStream = new ( ) ;
53+ using CryptoStream cryptoStream = new ( memoryStream , transform , CryptoStreamMode . Write ) ;
5954
60- using var transform = aes . CreateEncryptor ( _key , initializationVector ) ;
61- using var crypto = new CryptoStream ( memoryStream , transform , CryptoStreamMode . Write ) ;
62- crypto . Write ( input , 0 , input . Length ) ;
63- crypto . FlushFinalBlock ( ) ;
55+ cryptoStream . Write ( input , 0 , input . Length ) ;
56+ cryptoStream . FlushFinalBlock ( ) ;
57+ memoryStream . Seek ( 0L , SeekOrigin . Begin ) ;
6458
65- memoryStream . Seek ( 0 , SeekOrigin . Begin ) ;
66-
67- return memoryStream . ToArray ( ) ;
59+ return StreamToBytes ( memoryStream ) ;
6860 }
6961
7062 /// <inheritdoc />
@@ -75,39 +67,46 @@ public byte[] Decrypt(byte[] input)
7567 return null ;
7668 }
7769
78- using var memoryStream = new MemoryStream ( input ) ;
70+ using Aes aes = CreateCryptographyProvider ( _key , _iv , _mode , _padding ) ;
71+ using ICryptoTransform transform = aes . CreateDecryptor ( aes . Key , aes . IV ) ;
72+ using MemoryStream memoryStream = new ( input ) ;
73+ using CryptoStream cryptoStream = new ( memoryStream , transform , CryptoStreamMode . Read ) ;
74+
75+ return StreamToBytes ( cryptoStream ) ;
76+ }
7977
80- byte [ ] initializationVector = _iv ;
81- if ( initializationVector is null )
78+ /// <summary>
79+ /// Converts a <see cref="Stream"/> into a byte array.
80+ /// </summary>
81+ /// <param name="stream">Stream.</param>
82+ /// <returns>The stream's content as a byte array.</returns>
83+ internal static byte [ ] StreamToBytes ( Stream stream )
84+ {
85+ if ( stream is MemoryStream ms )
8286 {
83- initializationVector = new byte [ InitializationVectorSize ] ;
84- memoryStream . Read ( initializationVector , 0 , initializationVector . Length ) ;
87+ return ms . ToArray ( ) ;
8588 }
8689
87- using var aes = CreateCryptographyProvider ( _key , _mode , _padding ) ;
88- using var transform = aes . CreateDecryptor ( _key , initializationVector ) ;
89-
90- using var outputStream = new MemoryStream ( ) ;
91- using var crypto = new CryptoStream ( memoryStream , transform , CryptoStreamMode . Read ) ;
92-
93- crypto . CopyTo ( outputStream ) ;
94-
95- return outputStream . ToArray ( ) ;
90+ using var output = new MemoryStream ( ) ;
91+ stream . CopyTo ( output ) ;
92+ return output . ToArray ( ) ;
9693 }
9794
9895 /// <summary>
9996 /// Generates an AES cryptography provider.
10097 /// </summary>
10198 /// <returns></returns>
102- private static Aes CreateCryptographyProvider ( byte [ ] key , CipherMode mode , PaddingMode padding )
99+ private static Aes CreateCryptographyProvider ( byte [ ] key , byte [ ] iv , CipherMode mode , PaddingMode padding )
103100 {
104101 var aes = Aes . Create ( ) ;
105102
106- aes . BlockSize = AesBlockSize ;
107103 aes . Mode = mode ;
104+ aes . KeySize = key . Length * 8 ;
105+ aes . BlockSize = AesBlockSize ;
106+ aes . FeedbackSize = AesBlockSize ;
108107 aes . Padding = padding ;
109108 aes . Key = key ;
110- aes . KeySize = key . Length * 8 ;
109+ aes . IV = iv ;
111110
112111 return aes ;
113112 }
0 commit comments