Docs never really mention why CryptoStream
should be used instead of TransformBlock
and TransformFinalBlock
other than that it calls whichever is needed automatically.
Thus, why would one use the code in this answer (https://stackoverflow.com/a/2006922/7343355) instead of this:
using (var encryptor = aes.CreateEncryptor())
{
result = encryptor.TransformFinalBlock(data, 0, data.Length); // Data length is greater than the blocksize
}
Even though TransformFinalBlock
should be used after TransformBlock
and only on the last block, somehow this code passed all the unit tests and always gives me the correct result. Are there any cases where this could fail? Does this have something to do with me using ECB
cipher mode to test this and would fail under other modes?