11using System ;
22using System . Text ;
3- using System . Security . Cryptography ;
43using SafeCrypt . src . Helpers ;
54using SafeCrypt . src . Encryption . AesEncryption . Models ;
65
76namespace SafeCrypt . src . Encrypt . AesEncryption
87{
98 public class Encrypting : BaseAesEncryption
109 {
11- public byte [ ] Encrypt ( byte [ ] data , byte [ ] secretKey , byte [ ] iv )
12- => EncryptAES ( data , secretKey , iv ) ;
10+ /// <summary>
11+ /// Encrypts the provided data using the specified secret key and initialization vector (IV).
12+ /// </summary>
13+ /// <param name="parameters">The encryption parameters.</param>
14+ /// <returns>The encrypted data as a byte array.</returns>
15+ /// <remarks>
16+ /// This method serves as a wrapper around the underlying AES encryption logic provided by the
17+ /// <see cref="EncryptAES"/> method. It simplifies the encryption process by exposing a more
18+ /// user-friendly interface, accepting data, secret key, and initialization vector as parameters.
19+ /// </remarks>
20+ /// <param name="data">The data to be encrypted.</param>
21+ /// <param name="secretKey">The secret key used for encryption.</param>
22+ /// <param name="iv">The initialization vector used for encryption.</param>
23+ /// <returns>The encrypted data as a byte array.</returns>
24+ public byte [ ] Encrypt ( ByteEncryptionParameters param )
25+ {
26+ Validators . ValidateNotNull ( param ) ;
27+
28+ // Delegate the encryption to the underlying AES encryption method
29+ return EncryptAES ( param ) ;
30+ }
1331
1432
15- public byte [ ] Encrypt ( string data , string secretKey , string iv )
33+ public byte [ ] Encrypt ( StringEncryptionParameters param )
1634 {
17- NullChecks ( data , secretKey , iv ) ;
35+ Validators . ValidateNotNull ( param ) ;
1836
19- var aesKey = secretKey . ConvertKeysToBytes ( ) ;
20- var aesIv = iv . ConvertKeysToBytes ( ) ;
37+ var byteEncryptionParameters = new ByteEncryptionParameters
38+ {
39+ SecretKey = param . SecretKey . ConvertKeysToBytes ( ) ,
40+ IV = param . IV . ConvertKeysToBytes ( ) ,
41+ Data = param . Data . HexadecimalStringToByteArray ( )
42+ } ;
2143
22- var aesData = data . HexadecimalStringToByteArray ( ) ;
23- return EncryptAES ( aesData , aesKey , aesIv ) ;
44+ return EncryptAES ( byteEncryptionParameters ) ;
2445 }
2546
26-
27-
28- public AesEncryptionData Encrypt ( string data , string secretKey )
47+ /// <summary>
48+ /// Encrypts the provided string data using the Advanced Encryption Standard (AES) algorithm.
49+ /// </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>
53+ /// <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.
57+ /// </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>
61+ /// <exception cref="ArgumentNullException">
62+ /// Thrown if the input data or secret key is null.
63+ /// </exception>
64+ public EncryptionData Encrypt ( string data , string secretKey )
2965 {
3066 NullChecks ( data , secretKey ) ;
3167
32- var aesIv = GenerateRandomBytes ( 16 ) ;
68+ // Generate a random 16-byte IV for AES in CBC mode
69+ var aesIv = GenerateRandomIVKeyAsBytes ( 16 ) ;
3370
34- var aesKey = Encoding . UTF8 . GetBytes ( secretKey ) ;
35- var aesData = data . HexadecimalStringToByteArray ( ) ;
71+ // var aesKey = Encoding.UTF8.GetBytes(secretKey);
72+ // var aesData = data.HexadecimalStringToByteArray();
3673
37- var response = EncryptAES ( aesData , aesKey , aesIv ) ;
74+ var byteEncryptionParameters = new ByteEncryptionParameters
75+ {
76+ SecretKey = Encoding . UTF8 . GetBytes ( secretKey ) ,
77+ IV = aesIv ,
78+ Data = data . HexadecimalStringToByteArray ( )
79+ } ;
3880
39- var responseData = new AesEncryptionData
81+ //var response = EncryptAES(aesData, aesKey, aesIv);
82+ var response = EncryptAES ( byteEncryptionParameters ) ;
83+
84+ var responseData = new EncryptionData
4085 {
4186 Data = response ,
4287 Iv = aesIv
@@ -45,43 +90,90 @@ public AesEncryptionData Encrypt(string data, string secretKey)
4590 return responseData ;
4691 }
4792
48- public string EncryptByteToHexString ( byte [ ] data , byte [ ] secretKey , byte [ ] iv )
93+ /// <summary>
94+ /// Encrypts the provided byte data using the Advanced Encryption Standard (AES) algorithm
95+ /// and returns the encrypted data as a hexadecimal string.
96+ /// </summary>
97+ /// <param name="param">The parameters containing the byte data, secret key, and initialization vector (IV).</param>
98+ /// <returns>The encrypted data represented as a hexadecimal string.</returns>
99+ /// <remarks>
100+ /// This method encrypts the input byte data using the specified secret key and initialization vector (IV)
101+ /// using the AES algorithm. The resulting encrypted data is then converted to a hexadecimal string before being
102+ /// returned. The encryption parameters are encapsulated in a <see cref="ByteEncryptionParameters"/> object.
103+ /// </remarks>
104+ /// <param name="param">The parameters containing the byte data, secret key, and initialization vector (IV).</param>
105+ /// <returns>The encrypted data represented as a hexadecimal string.</returns>
106+ /// <exception cref="ArgumentNullException">
107+ /// Thrown if the input parameters or byte data is null.
108+ /// </exception>
109+ public string EncryptByteToHexString ( ByteEncryptionParameters param )
49110 {
50- var cipherText = EncryptAES ( data , secretKey , iv ) ;
111+ Validators . ValidateNotNull ( param ) ;
112+
113+ var cipherText = EncryptAES ( param ) ;
51114
115+ // Convert the encrypted data to a hexadecimal string
52116 return cipherText . ByteArrayToHexString ( ) ;
53117 }
54118
55- public string EncryptByteToBase64String ( byte [ ] data , byte [ ] secretKey , byte [ ] iv )
119+ /// <summary>
120+ /// Encrypts the provided byte data using the Advanced Encryption Standard (AES) algorithm
121+ /// and returns the encrypted data as a Base64-encoded string.
122+ /// </summary>
123+ /// <param name="param">The parameters containing the byte data, secret key, and initialization vector (IV).</param>
124+ /// <returns>The encrypted data represented as a Base64-encoded string.</returns>
125+ /// <remarks>
126+ /// This method encrypts the input byte data using the specified secret key and initialization vector (IV)
127+ /// using the AES algorithm. The resulting encrypted data is then converted to a Base64-encoded string before
128+ /// being returned. The encryption parameters are encapsulated in a <see cref="ByteEncryptionParameters"/> object.
129+ /// </remarks>
130+ /// <param name="param">The parameters containing the byte data, secret key, and initialization vector (IV).</param>
131+ /// <returns>The encrypted data represented as a Base64-encoded string.</returns>
132+ /// <exception cref="ArgumentNullException">
133+ /// Thrown if the input parameters or byte data is null.
134+ /// </exception>
135+ public string EncryptByteToBase64String ( ByteEncryptionParameters param )
56136 {
57- var cipherText = EncryptAES ( data , secretKey , iv ) ;
137+ Validators . ValidateNotNull ( param ) ;
138+
139+ var cipherText = EncryptAES ( param ) ;
58140
59141 return Convert . ToBase64String ( cipherText ) ;
60142 }
61143
62- public string EncryptByteToString ( byte [ ] data , byte [ ] secretKey , byte [ ] iv )
144+ /// <summary>
145+ /// Encrypts the provided byte data using the Advanced Encryption Standard (AES) algorithm
146+ /// and returns the encrypted data as a string using UTF-8 encoding.
147+ /// </summary>
148+ /// <param name="param">The parameters containing the byte data, secret key, and initialization vector (IV).</param>
149+ /// <returns>The encrypted data represented as a string using UTF-8 encoding.</returns>
150+ /// <remarks>
151+ /// This method encrypts the input byte data using the specified secret key and initialization vector (IV)
152+ /// using the AES algorithm. The resulting encrypted data is then converted to a string using UTF-8
153+ /// encoding before being returned. The encryption parameters are encapsulated in a <see cref="ByteEncryptionParameters"/> object.
154+ /// </remarks>
155+ /// <param name="param">The parameters containing the byte data, secret key, and initialization vector (IV).</param>
156+ /// <returns>The encrypted data represented as a string using UTF-8 encoding.</returns>
157+ /// <exception cref="ArgumentNullException">
158+ /// Thrown if the input parameters or byte data is null.
159+ /// </exception>
160+ public string EncryptByteToString ( ByteEncryptionParameters param )
63161 {
64- var cipherText = EncryptAES ( data , secretKey , iv ) ;
162+ Validators . ValidateNotNull ( param ) ;
163+
164+ var cipherText = EncryptAES ( param ) ;
65165
66166 return cipherText . BytesToString ( ) ;
67167 }
168+
68169
69- // needs methods that would accept aes mode
70- //public byte[] AesEncrypt(byte[] data, byte[] key, byte[] iv, ReturnType returnType)
71- //{
72-
73- //}
74-
75- private void NullChecks ( string data , string secretKey , string iv = "" )
170+ private void NullChecks ( string data , string secretKey )
76171 {
77172 if ( data == null || data . Length <= 0 )
78173 throw new ArgumentNullException ( nameof ( data ) ) ;
79174
80175 if ( secretKey == null || secretKey . Length <= 0 )
81176 throw new ArgumentNullException ( nameof ( secretKey ) ) ;
82-
83- if ( iv == null || iv . Length <= 0 )
84- throw new ArgumentNullException ( nameof ( iv ) ) ;
85177 }
86178 }
87179
0 commit comments