In the AesManaged class there's the IV and Key properties. In the example at https://learn.microsoft.com/en-us/dotnet/api/system.security.cryptography.aesmanaged?view=net-5.0#examples these properties are set and then passed to the CreateDecryptor
method. But, in my testing, one can decrypt ciphertext's successfully even without setting the IV / Key methods:
using System;
using System.Security.Cryptography;
using System.Text;
using System.IO;
class Test
{
// from https://stackoverflow.com/a/311179/569976
public static byte[] StringToByteArray(String hex)
{
int NumberChars = hex.Length;
byte[] bytes = new byte[NumberChars / 2];
for (int i = 0; i < NumberChars; i += 2)
bytes[i / 2] = Convert.ToByte(hex.Substring(i, 2), 16);
return bytes;
}
static void Main()
{
var ciphertext = StringToByteArray("10f42fd95857ed2775cfbc4b471bc213");
var aes = new AesManaged();
var key = Encoding.ASCII.GetBytes("aaaaaaaaaaaaaaaa");
var iv = Encoding.ASCII.GetBytes("bbbbbbbbbbbbbbbb");
ICryptoTransform decryptor = aes.CreateDecryptor(key, iv);
MemoryStream msDecrypt = new MemoryStream(ciphertext);
CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read);
StreamReader srDecrypt = new StreamReader(csDecrypt);
String plaintext = srDecrypt.ReadToEnd();
Console.WriteLine(plaintext);
}
}
So why set them with the properties at all?
I guess you'd still need to set the mode (CBC, ECB, CTR, etc) via the properties but setting the IV / Key via the properties seems redundant and unnecessary to me?