|
1 | 1 | using SafeCrypt.src.Encryption.AesEncryption.Models; |
2 | 2 | using System; |
3 | 3 | using System.IO; |
| 4 | +using System.Reflection; |
4 | 5 | using System.Security.Cryptography; |
5 | 6 | using System.Text; |
6 | 7 |
|
@@ -72,34 +73,42 @@ public virtual byte[] EncryptAES(ByteEncryptionParameters param) |
72 | 73 | /// <exception cref="ArgumentNullException"> |
73 | 74 | /// Thrown if the input encrypted data, key, or initialization vector is null. |
74 | 75 | /// </exception> |
75 | | - public static byte[] DecryptAES(byte[] encryptedData, byte[] key, byte[] iv) |
| 76 | + public static byte[] DecryptAES(ByteDecryptionParameters param) |
76 | 77 | { |
77 | | - // Create an instance of the AES algorithm |
78 | | - using (Aes aes = Aes.Create()) |
| 78 | + try |
79 | 79 | { |
80 | | - // Set the key and initialization vector |
81 | | - aes.Key = key; |
82 | | - aes.IV = iv; |
| 80 | + // Create an instance of the AES algorithm |
| 81 | + using (Aes aes = Aes.Create()) |
| 82 | + { |
| 83 | + // Set the key and initialization vector |
| 84 | + aes.Key = param.SecretKey; |
| 85 | + aes.IV = param.IV; |
83 | 86 |
|
84 | | - // Create a decryptor using the key and initialization vector |
85 | | - ICryptoTransform decryptor = aes.CreateDecryptor(aes.Key, aes.IV); |
| 87 | + // Create a decryptor using the key and initialization vector |
| 88 | + ICryptoTransform decryptor = aes.CreateDecryptor(aes.Key, aes.IV); |
86 | 89 |
|
87 | | - // Use a MemoryStream to read the encrypted data |
88 | | - using (MemoryStream memoryStream = new MemoryStream(encryptedData)) |
89 | | - { |
90 | | - // Use a CryptoStream to perform the decryption |
91 | | - using (CryptoStream cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read)) |
| 90 | + // Use a MemoryStream to read the encrypted data |
| 91 | + using (MemoryStream memoryStream = new MemoryStream(param.Data)) |
92 | 92 | { |
93 | | - // Use a MemoryStream to store the decrypted data |
94 | | - using (MemoryStream decryptedStream = new MemoryStream()) |
| 93 | + // Use a CryptoStream to perform the decryption |
| 94 | + using (CryptoStream cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read)) |
95 | 95 | { |
96 | | - // Copy the decrypted data from the CryptoStream to the MemoryStream |
97 | | - cryptoStream.CopyTo(decryptedStream); |
98 | | - return decryptedStream.ToArray(); |
| 96 | + // Use a MemoryStream to store the decrypted data |
| 97 | + using (MemoryStream decryptedStream = new MemoryStream()) |
| 98 | + { |
| 99 | + // Copy the decrypted data from the CryptoStream to the MemoryStream |
| 100 | + cryptoStream.CopyTo(decryptedStream); |
| 101 | + return decryptedStream.ToArray(); |
| 102 | + } |
99 | 103 | } |
100 | 104 | } |
101 | 105 | } |
102 | 106 | } |
| 107 | + catch (Exception ex) |
| 108 | + { |
| 109 | + |
| 110 | + throw; |
| 111 | + } |
103 | 112 | } |
104 | 113 | } |
105 | 114 | } |
0 commit comments