While developing some code that uses the .NET AesManaged algorithm, I made some mistakes but was surprised at the results.
My encryption was correct. I was generating a random IV block and writing it to the file (without encrypting the IV). Then I was encrypting the plaintext and writing it.
The decryption was wrong though... instead of reading the first block from the file an using it as an IV, it was just using a random IV and decrypting everything. The first block came out gibberish but the 2nd (and subsequent) ones were decrypted properly! Why would this be?
And even weirder... when I tried to fix it by encrypting the IV in the encryption code (instead of writing the IV unchanged) the decryption result was the same! (gibberish then correctly decrypted blocks).
Although I eventually fixed my code, how do these results make sense?