22using System . Text ;
33using SafeCrypt . src . Helpers ;
44using SafeCrypt . src . Encryption . AesEncryption . Models ;
5+ using SafeCrypt . AesEncryption ;
6+ using System . ComponentModel . DataAnnotations ;
57
6- namespace SafeCrypt . src . Encrypt . AesEncryption
8+ namespace SafeCrypt . Encrypt
79{
810 public class Encrypting : BaseAesEncryption
911 {
@@ -21,12 +23,18 @@ public class Encrypting : BaseAesEncryption
2123 /// <param name="secretKey">The secret key used for encryption.</param>
2224 /// <param name="iv">The initialization vector used for encryption.</param>
2325 /// <returns>The encrypted data as a byte array.</returns>
24- public byte [ ] Encrypt ( ByteEncryptionParameters param )
26+ public byte [ ] Encrypt ( EncryptionParameters param )
2527 {
2628 Validators . ValidateNotNull ( param ) ;
2729
2830 // Delegate the encryption to the underlying AES encryption method
29- return EncryptAES ( param ) ;
31+ var byteEncryptionParameters = new ByteEncryptionParameters
32+ {
33+ SecretKey = param . SecretKey . ConvertKeysToBytes ( ) ,
34+ IV = param . IV . ConvertKeysToBytes ( ) ,
35+ Data = param . DataToEncrypt . HexadecimalStringToByteArray ( )
36+ } ;
37+ return EncryptAES ( byteEncryptionParameters ) ;
3038 }
3139
3240
@@ -47,44 +55,51 @@ public byte[] Encrypt(StringEncryptionParameters param)
4755 /// <summary>
4856 /// Encrypts the provided string data using the Advanced Encryption Standard (AES) algorithm.
4957 /// </summary>
50- /// <param name="data ">The string data to be encrypted.</param>
51- /// <param name="secretKey ">The secret key used for encryption.</param>
52- /// <returns>An <see cref="AesEncrypted "/> object containing the encrypted data and initialization vector (IV).</returns>
58+ /// <param name="dataToBeEncrypted ">The string data to be encrypted.</param>
59+ /// <param name="base64secretKey ">The Base64-encoded secret key used for encryption.</param>
60+ /// <returns>An <see cref="EncryptionData "/> object containing the encrypted data and initialization vector (IV) as bas64 strings .</returns>
5361 /// <remarks>
54- /// This method encrypts the input string data using the specified secret key and a randomly generated initialization
55- /// vector (IV) for AES encryption in Cipher Block Chaining (CBC) mode. The resulting encrypted data and IV are encapsulated
56- /// in an <see cref="AesEncrypted"/> object, which is then returned.
62+ /// This method validates the Base64 encoding of the secret key using <see cref="Validators.IsBase64String"/>.
63+ /// If the key is not valid, the method returns null. Otherwise, it proceeds with the encryption process.
64+ /// The method generates a random 16-byte IV for AES in CBC mode, converts the input data to a byte array,
65+ /// and then encrypts the data using AES. The result is encapsulated in an <see cref="EncryptionData"/> object
66+ /// containing the encrypted data and IV, both Base64-encoded.
5767 /// </remarks>
58- /// <param name="data ">The string data to be encrypted.</param>
59- /// <param name="secretKey ">The secret key used for encryption.</param>
60- /// <returns>An <see cref="AesEncrypted "/> object containing the encrypted data and initialization vector (IV).</returns>
68+ /// <param name="dataToBeEncrypted ">The string data to be encrypted.</param>
69+ /// <param name="base64secretKey ">The Base64-encoded secret key used for encryption.</param>
70+ /// <returns>An <see cref="EncryptionData "/> object containing the encrypted data and initialization vector (IV).</returns>
6171 /// <exception cref="ArgumentNullException">
62- /// Thrown if the input data or secret key is null.
72+ /// Thrown if the input data or base64secretKey is null.
6373 /// </exception>
64- public EncryptionData Encrypt ( string data , string secretKey )
74+ /// <exception cref="FormatException">
75+ /// Thrown if the base64secretKey is not a valid Base64-encoded string.
76+ /// </exception>
77+ public EncryptionData EncryptToBase64String ( string dataToBeEncrypted , string base64secretKey )
6578 {
66- NullChecks ( data , secretKey ) ;
79+ // validate is base64
80+ if ( ! Validators . IsBase64String ( base64secretKey ) )
81+ {
82+ return null ;
83+ }
84+
85+ NullChecks ( data : dataToBeEncrypted , base64secretKey ) ;
6786
6887 // Generate a random 16-byte IV for AES in CBC mode
6988 var aesIv = GenerateRandomIVKeyAsBytes ( 16 ) ;
7089
71- //var aesKey = Encoding.UTF8.GetBytes(secretKey);
72- //var aesData = data.HexadecimalStringToByteArray();
73-
7490 var byteEncryptionParameters = new ByteEncryptionParameters
7591 {
76- SecretKey = Encoding . UTF8 . GetBytes ( secretKey ) ,
92+ SecretKey = Convert . FromBase64String ( base64secretKey ) ,
7793 IV = aesIv ,
78- Data = data . HexadecimalStringToByteArray ( )
94+ Data = dataToBeEncrypted . ConvertToHexString ( ) . HexadecimalStringToByteArray ( )
7995 } ;
8096
81- //var response = EncryptAES(aesData, aesKey, aesIv);
8297 var response = EncryptAES ( byteEncryptionParameters ) ;
8398
8499 var responseData = new EncryptionData
85100 {
86- Data = response ,
87- Iv = aesIv
101+ EncryptedData = Convert . ToBase64String ( response ) ,
102+ Iv = Convert . ToBase64String ( aesIv )
88103 } ;
89104
90105 return responseData ;
@@ -106,11 +121,18 @@ public EncryptionData Encrypt(string data, string secretKey)
106121 /// <exception cref="ArgumentNullException">
107122 /// Thrown if the input parameters or byte data is null.
108123 /// </exception>
109- public string EncryptByteToHexString ( ByteEncryptionParameters param )
124+ public string EncryptByteToHexString ( EncryptionParameters param )
110125 {
111126 Validators . ValidateNotNull ( param ) ;
112127
113- var cipherText = EncryptAES ( param ) ;
128+ var byteEncryptionParameters = new ByteEncryptionParameters
129+ {
130+ SecretKey = param . SecretKey . ConvertKeysToBytes ( ) ,
131+ IV = param . IV . ConvertKeysToBytes ( ) ,
132+ Data = param . DataToEncrypt . HexadecimalStringToByteArray ( )
133+ } ;
134+
135+ var cipherText = EncryptAES ( byteEncryptionParameters ) ;
114136
115137 // Convert the encrypted data to a hexadecimal string
116138 return cipherText . ByteArrayToHexString ( ) ;
@@ -132,11 +154,18 @@ public string EncryptByteToHexString(ByteEncryptionParameters param)
132154 /// <exception cref="ArgumentNullException">
133155 /// Thrown if the input parameters or byte data is null.
134156 /// </exception>
135- public string EncryptByteToBase64String ( ByteEncryptionParameters param )
157+ public string EncryptByteToBase64String ( EncryptionParameters param )
136158 {
137159 Validators . ValidateNotNull ( param ) ;
138160
139- var cipherText = EncryptAES ( param ) ;
161+ var byteEncryptionParameters = new ByteEncryptionParameters
162+ {
163+ SecretKey = param . SecretKey . ConvertKeysToBytes ( ) ,
164+ IV = param . IV . ConvertKeysToBytes ( ) ,
165+ Data = param . DataToEncrypt . HexadecimalStringToByteArray ( )
166+ } ;
167+
168+ var cipherText = EncryptAES ( byteEncryptionParameters ) ;
140169
141170 return Convert . ToBase64String ( cipherText ) ;
142171 }
@@ -157,11 +186,18 @@ public string EncryptByteToBase64String(ByteEncryptionParameters param)
157186 /// <exception cref="ArgumentNullException">
158187 /// Thrown if the input parameters or byte data is null.
159188 /// </exception>
160- public string EncryptByteToString ( ByteEncryptionParameters param )
189+ public string EncryptByteToString ( EncryptionParameters param )
161190 {
162191 Validators . ValidateNotNull ( param ) ;
163192
164- var cipherText = EncryptAES ( param ) ;
193+ var byteEncryptionParameters = new ByteEncryptionParameters
194+ {
195+ SecretKey = param . SecretKey . ConvertKeysToBytes ( ) ,
196+ IV = param . IV . ConvertKeysToBytes ( ) ,
197+ Data = param . DataToEncrypt . HexadecimalStringToByteArray ( )
198+ } ;
199+
200+ var cipherText = EncryptAES ( byteEncryptionParameters ) ;
165201
166202 return cipherText . BytesToString ( ) ;
167203 }
@@ -172,7 +208,7 @@ private void NullChecks(string data, string secretKey)
172208 if ( data == null || data . Length <= 0 )
173209 throw new ArgumentNullException ( nameof ( data ) ) ;
174210
175- if ( secretKey == null || secretKey . Length <= 0 )
211+ if ( secretKey == null )
176212 throw new ArgumentNullException ( nameof ( secretKey ) ) ;
177213 }
178214 }
0 commit comments