@@ -6,23 +6,46 @@ namespace SafeCrypt.src.Encrypt.AesEncryption
66{
77 public class BaseAesEncryption
88 {
9- // Method to encrypt data using AES algorithm
9+ /// <summary>
10+ /// Encrypts the provided data using the Advanced Encryption Standard (AES) algorithm.
11+ /// </summary>
12+ /// <param name="data">The data to be encrypted.</param>
13+ /// <param name="key">The secret key used for encryption.</param>
14+ /// <param name="iv">The initialization vector used for encryption.</param>
15+ /// <returns>The encrypted data as a byte array.</returns>
16+ /// <remarks>
17+ /// The method uses the AES algorithm to encrypt the input data with the provided key and initialization vector.
18+ /// </remarks>
19+ /// <exception cref="ArgumentNullException">
20+ /// Thrown if the input data, key, or initialization vector is null.
21+ /// </exception>
22+ /// <exception cref="Exception">
23+ /// Thrown for general encryption-related exceptions.
24+ /// </exception>
1025 public virtual byte [ ] EncryptAES ( byte [ ] data , byte [ ] key , byte [ ] iv )
1126 {
1227 try
1328 {
29+ // Create an instance of the AES algorithm
1430 using ( Aes aes = Aes . Create ( ) )
1531 {
32+ // Set the key and initialization vector
1633 aes . Key = key ;
1734 aes . IV = iv ;
35+ // Create an encryptor using the key and initialization vector
1836 ICryptoTransform encryptor = aes . CreateEncryptor ( aes . Key , aes . IV ) ;
37+
38+ // Use a MemoryStream to store the encrypted data
1939 using ( MemoryStream memoryStream = new MemoryStream ( ) )
2040 {
41+ // Use a CryptoStream to perform the encryption
2142 using ( CryptoStream cryptoStream = new CryptoStream ( memoryStream , encryptor , CryptoStreamMode . Write ) )
2243 {
44+ // Write the data to be encrypted to the CryptoStream
2345 cryptoStream . Write ( data , 0 , data . Length ) ;
2446 cryptoStream . FlushFinalBlock ( ) ;
2547
48+ // Return the encrypted data as a byte array
2649 return memoryStream . ToArray ( ) ;
2750 }
2851 }
@@ -34,20 +57,41 @@ public virtual byte[] EncryptAES(byte[] data, byte[] key, byte[] iv)
3457 }
3558 }
3659
37- // Method to decrypt data using AES algorithm
60+ /// <summary>
61+ /// Decrypts the provided encrypted data using the Advanced Encryption Standard (AES) algorithm.
62+ /// </summary>
63+ /// <param name="encryptedData">The data to be decrypted.</param>
64+ /// <param name="key">The secret key used for decryption.</param>
65+ /// <param name="iv">The initialization vector used for decryption.</param>
66+ /// <returns>The decrypted data as a byte array.</returns>
67+ /// <remarks>
68+ /// The method uses the AES algorithm to decrypt the input encrypted data with the provided key and initialization vector.
69+ /// </remarks>
70+ /// <exception cref="ArgumentNullException">
71+ /// Thrown if the input encrypted data, key, or initialization vector is null.
72+ /// </exception>
3873 public static byte [ ] DecryptAES ( byte [ ] encryptedData , byte [ ] key , byte [ ] iv )
3974 {
75+ // Create an instance of the AES algorithm
4076 using ( Aes aes = Aes . Create ( ) )
4177 {
78+ // Set the key and initialization vector
4279 aes . Key = key ;
4380 aes . IV = iv ;
81+
82+ // Create a decryptor using the key and initialization vector
4483 ICryptoTransform decryptor = aes . CreateDecryptor ( aes . Key , aes . IV ) ;
84+
85+ // Use a MemoryStream to read the encrypted data
4586 using ( MemoryStream memoryStream = new MemoryStream ( encryptedData ) )
4687 {
88+ // Use a CryptoStream to perform the decryption
4789 using ( CryptoStream cryptoStream = new CryptoStream ( memoryStream , decryptor , CryptoStreamMode . Read ) )
4890 {
91+ // Use a MemoryStream to store the decrypted data
4992 using ( MemoryStream decryptedStream = new MemoryStream ( ) )
5093 {
94+ // Copy the decrypted data from the CryptoStream to the MemoryStream
5195 cryptoStream . CopyTo ( decryptedStream ) ;
5296 return decryptedStream . ToArray ( ) ;
5397 }
@@ -56,10 +100,22 @@ public static byte[] DecryptAES(byte[] encryptedData, byte[] key, byte[] iv)
56100 }
57101 }
58102
59- // Method to generate a random byte array of given length
60- // Used to get the IV
61- // Generate a random 16-byte IV for AES in CBC mode
62- public static byte [ ] GenerateRandomBytes ( int length )
103+
104+ /// <summary>
105+ /// Generates an array of random bytes using a cryptographically secure random number generator.
106+ /// </summary>
107+ /// <param name="length">The length of the byte array to generate.</param>
108+ /// <returns>An array of random bytes or the IV key</returns>
109+ /// <remarks>
110+ /// The method uses a cryptographically secure random number generator (RNGCryptoServiceProvider) to generate
111+ /// a byte array with the specified length, providing a high level of randomness suitable for cryptographic use.
112+ /// </remarks>
113+ /// <param name="length">The desired length of the generated byte array.</param>
114+ /// <returns>An array of random bytes with the specified length.</returns>
115+ /// <exception cref="ArgumentException">
116+ /// Thrown if the specified length is less than or equal to zero.
117+ /// </exception>
118+ public static byte [ ] GenerateRandomIVKeyAsBytes ( int length )
63119 {
64120 byte [ ] randomBytes = new byte [ length ] ;
65121 using ( RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider ( ) )
0 commit comments