public byte[] Decrypt(byte[] cipherBytes, byte[] key, byte[] iv) { if (cipherBytes == null || cipherBytes.Length <= 0) { throw new ArgumentNullException(nameof(cipherBytes)); } if (key == null || key.Length <= 0) { throw new ArgumentNullException(nameof(key)); } if (iv == null || iv.Length <= 0) { throw new ArgumentNullException(nameof(iv)); } using (var aesAlg = Aes.Create()) { if (aesAlg == null) { throw new CryptographicException("Could not create AES algorithm"); } aesAlg.Key = key; aesAlg.IV = iv; using (var decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV)) using (var msDecrypt = new MemoryStream(cipherBytes)) using (var csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read)) { return(csDecrypt.GetBytes()); } } }