11using System ;
2+ using System . Security . Cryptography ;
23using SafeCrypt . AesEncryption ;
34using SafeCrypt . Helpers ;
45using SafeCrypt . Models ;
@@ -21,24 +22,17 @@ public class AesEncryption : BaseAesEncryption
2122 /// <param name="secretKey">The secret key used for encryption.</param>
2223 /// <param name="iv">The initialization vector used for encryption.</param>
2324 /// <returns>The encrypted data as a byte array.</returns>
24- public EncryptionData EncryptToHexString ( EncryptionParameters param )
25+ public EncryptionData EncryptToHexString ( EncryptionParameters param , CipherMode mode = CipherMode . CBC )
2526 {
2627 var responseData = new EncryptionData ( ) ;
2728
28- Validators . ValidateNotNull ( param ) ;
29+ var parameterValidation = ValidateEncryptionParameters ( param ) ;
2930
30- // validate is base64
31- if ( ! Validators . IsBase64String ( param . SecretKey ) )
31+ if ( parameterValidation . HasError )
3232 {
33- AddError ( responseData , $ "SecretKey: { param . SecretKey } is not a base64 string") ;
34- return responseData ;
33+ return parameterValidation ;
3534 }
3635
37- if ( ! Validators . IsBase64String ( param . IV ) )
38- {
39- AddError ( responseData , $ "IV: { param . IV } is not a base64 string") ;
40- return responseData ;
41- }
4236 // Convert input string to bytes
4337 byte [ ] dataBytes = param . IV . ConvertKeysToBytes ( ) ;
4438
@@ -57,7 +51,7 @@ public EncryptionData EncryptToHexString(EncryptionParameters param)
5751 Data = param . DataToEncrypt . ConvertToHexString ( ) . HexadecimalStringToByteArray ( )
5852 } ;
5953
60- var response = EncryptAES ( byteEncryptionParameters ) ;
54+ var response = EncryptAES ( byteEncryptionParameters , mode ) ;
6155
6256 return new EncryptionData
6357 {
@@ -66,8 +60,7 @@ public EncryptionData EncryptToHexString(EncryptionParameters param)
6660 SecretKey = param . SecretKey
6761 } ;
6862 }
69-
70-
63+
7164 /// <summary>
7265 /// Encrypts the provided string data using the Advanced Encryption Standard (AES) algorithm.
7366 /// </summary>
@@ -90,7 +83,7 @@ public EncryptionData EncryptToHexString(EncryptionParameters param)
9083 /// <exception cref="FormatException">
9184 /// Thrown if the base64secretKey is not a valid Base64-encoded string.
9285 /// </exception>
93- public EncryptionData EncryptToBase64String ( string dataToBeEncrypted , string base64secretKey )
86+ public EncryptionData EncryptToBase64String ( string dataToBeEncrypted , string base64secretKey , CipherMode mode = CipherMode . CBC )
9487 {
9588 // validate is base64
9689 if ( ! Validators . IsBase64String ( base64secretKey ) )
@@ -110,7 +103,7 @@ public EncryptionData EncryptToBase64String(string dataToBeEncrypted, string bas
110103 Data = dataToBeEncrypted . ConvertToHexString ( ) . HexadecimalStringToByteArray ( )
111104 } ;
112105
113- var response = EncryptAES ( byteEncryptionParameters ) ;
106+ var response = EncryptAES ( byteEncryptionParameters , mode ) ;
114107
115108 return new EncryptionData
116109 {
@@ -120,6 +113,26 @@ public EncryptionData EncryptToBase64String(string dataToBeEncrypted, string bas
120113 } ;
121114 }
122115
116+ private EncryptionData ValidateEncryptionParameters ( EncryptionParameters param )
117+ {
118+ var responseData = new EncryptionData ( ) ;
119+
120+ Validators . ValidateNotNull ( param ) ;
121+
122+ // validate is base64
123+ if ( ! Validators . IsBase64String ( param . SecretKey ) )
124+ {
125+ AddError ( responseData , $ "SecretKey: { param . SecretKey } is not a base64 string") ;
126+ }
127+
128+ if ( ! Validators . IsBase64String ( param . IV ) )
129+ {
130+ AddError ( responseData , $ "IV: { param . IV } is not a base64 string") ;
131+ }
132+
133+ return responseData ;
134+ }
135+
123136 private void NullChecks ( string data , string secretKey )
124137 {
125138 if ( data == null || data . Length <= 0 )
0 commit comments