public byte[] DecryptAES(byte[] cipheredPayload, CryptoLevel keysize = CryptoLevel.AES256)
{
// if the key strenght requested is different from the instance
if (KeySize != KeySize)
{
KeySize = keysize;
// re-calculate the decryption hash using the decryption level requested
SetEncryptionLevel(KeySize);
}
// Check arguments.
if (cipheredPayload == null || cipheredPayload.Length <= 0)
{
throw new ArgumentNullException("cipheredPayload");
}
if (key_array == null || key_array.Length <= 0)
{
throw new ArgumentNullException("Key");
}
if (iv_array == null || iv_array.Length <= 0)
{
throw new ArgumentNullException("Key");
}
// declare the output object.
byte[] decrypted;
// Create an Aes object
// with the specified key and IV.
using (Aes aes = Aes.Create())
{
aes.Key = key_array;
aes.IV = iv_array;
aes.Mode = CipherMode.CBC;
aes.Padding = PaddingMode.PKCS7;
// Create a decrytor to perform the stream transform.
ICryptoTransform decryptor = aes.CreateDecryptor(aes.Key, aes.IV);
// Create the streams used for decryption.
using (MemoryStream msDecrypt = new MemoryStream(cipheredPayload))
{
using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
{
csDecrypt.Read(cipheredPayload, 0, cipheredPayload.Length);
csDecrypt.Flush();
decrypted = msDecrypt.ToArray();
}
msDecrypt.Close();
}
}
return decrypted;
}