Skip to content

Commit 8506ad0

Browse files
committed
feat: add try catch to decrypt
1 parent 31f41f1 commit 8506ad0

1 file changed

Lines changed: 27 additions & 18 deletions

File tree

src/Encryption/AesEncryption/BaseAesEncryption.cs

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using SafeCrypt.src.Encryption.AesEncryption.Models;
22
using System;
33
using System.IO;
4+
using System.Reflection;
45
using System.Security.Cryptography;
56
using System.Text;
67

@@ -72,34 +73,42 @@ public virtual byte[] EncryptAES(ByteEncryptionParameters param)
7273
/// <exception cref="ArgumentNullException">
7374
/// Thrown if the input encrypted data, key, or initialization vector is null.
7475
/// </exception>
75-
public static byte[] DecryptAES(byte[] encryptedData, byte[] key, byte[] iv)
76+
public static byte[] DecryptAES(ByteDecryptionParameters param)
7677
{
77-
// Create an instance of the AES algorithm
78-
using (Aes aes = Aes.Create())
78+
try
7979
{
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;
8386

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);
8689

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))
9292
{
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))
9595
{
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+
}
99103
}
100104
}
101105
}
102106
}
107+
catch (Exception ex)
108+
{
109+
110+
throw;
111+
}
103112
}
104113
}
105114
}

0 commit comments

Comments
 (0)