44using System ;
55using System . Collections . Generic ;
66using System . Linq ;
7+ using System . Security ;
78using System . Security . Cryptography ;
89using System . Text ;
910using Microsoft . EntityFrameworkCore . DataEncryption . Internal ;
@@ -13,13 +14,32 @@ namespace Microsoft.EntityFrameworkCore.DataEncryption.Test.Providers
1314{
1415 public class AesProviderTest
1516 {
17+ [ Theory ]
18+ [ InlineData ( AesKeySize . AES128Bits ) ]
19+ [ InlineData ( AesKeySize . AES192Bits ) ]
20+ [ InlineData ( AesKeySize . AES256Bits ) ]
21+ public void EncryptDecryptByteArrayTest ( AesKeySize keySize )
22+ {
23+ byte [ ] input = DataHelper . RandomBytes ( 20 ) ;
24+ AesKeyInfo encryptionKeyInfo = AesProvider . GenerateKey ( keySize ) ;
25+ var provider = new AesProvider ( encryptionKeyInfo . Key ) ;
26+
27+ byte [ ] encryptedData = provider . Encrypt ( input , b => b , StandardConverters . StreamToBytes ) ;
28+ Assert . NotNull ( encryptedData ) ;
29+
30+ byte [ ] decryptedData = provider . Decrypt ( encryptedData , b => b , StandardConverters . StreamToBytes ) ;
31+ Assert . NotNull ( decryptedData ) ;
32+
33+ Assert . Equal ( input , decryptedData ) ;
34+ }
35+
1636 [ Theory ]
1737 [ InlineData ( AesKeySize . AES128Bits ) ]
1838 [ InlineData ( AesKeySize . AES192Bits ) ]
1939 [ InlineData ( AesKeySize . AES256Bits ) ]
2040 public void EncryptDecryptStringTest ( AesKeySize keySize )
2141 {
22- string input = StringHelper . RandomString ( 20 ) ;
42+ string input = DataHelper . RandomString ( 20 ) ;
2343 AesKeyInfo encryptionKeyInfo = AesProvider . GenerateKey ( keySize ) ;
2444 var provider = new AesProvider ( encryptionKeyInfo . Key ) ;
2545
@@ -32,6 +52,28 @@ public void EncryptDecryptStringTest(AesKeySize keySize)
3252 Assert . Equal ( input , decryptedData ) ;
3353 }
3454
55+ [ Theory ]
56+ [ InlineData ( AesKeySize . AES128Bits ) ]
57+ [ InlineData ( AesKeySize . AES192Bits ) ]
58+ [ InlineData ( AesKeySize . AES256Bits ) ]
59+ public void EncryptDecryptSecureStringTest ( AesKeySize keySize )
60+ {
61+ SecureString input = DataHelper . RandomSecureString ( 20 ) ;
62+ AesKeyInfo encryptionKeyInfo = AesProvider . GenerateKey ( keySize ) ;
63+ var provider = new AesProvider ( encryptionKeyInfo . Key ) ;
64+
65+ string encryptedData = provider . Encrypt ( input , Encoding . UTF8 . GetBytes , StandardConverters . StreamToBase64String ) ;
66+ Assert . NotNull ( encryptedData ) ;
67+
68+ SecureString decryptedData = provider . Decrypt ( encryptedData , Convert . FromBase64String , StandardConverters . StreamToSecureString ) ;
69+ Assert . NotNull ( decryptedData ) ;
70+
71+ byte [ ] inputBytes = Encoding . UTF8 . GetBytes ( input ) ;
72+ byte [ ] decryptedBytes = Encoding . UTF8 . GetBytes ( decryptedData ) ;
73+
74+ Assert . Equal ( inputBytes , decryptedBytes ) ;
75+ }
76+
3577 [ Theory ]
3678 [ InlineData ( AesKeySize . AES128Bits ) ]
3779 [ InlineData ( AesKeySize . AES192Bits ) ]
@@ -98,7 +140,7 @@ private static void ExecuteAesEncryptionTest<TContext>(AesKeySize aesKeyType) wh
98140 var provider = new AesProvider ( encryptionKeyInfo . Key , CipherMode . CBC , PaddingMode . Zeros ) ;
99141 var author = new AuthorEntity ( "John" , "Doe" , 42 )
100142 {
101- Password = StringHelper . RandomSecureString ( 10 ) ,
143+ Password = DataHelper . RandomSecureString ( 10 ) ,
102144 Books = new List < BookEntity >
103145 {
104146 new ( "Lorem Ipsum" , 300 ) ,
0 commit comments