Multithreading encryption in C# Multithreading encryption in C# multithreading multithreading

Multithreading encryption in C#


CBC (Cipher Block Chaining) is the default block mode for AesManaged.

You cannot encrypt different blocks in parallel with AES if you are using CBC because the side effect of encrypting block 1, for example, is to set a new IV for block 2. This is called "IV feedback." If you are running in parallel, this won't work. You will need to pick a different cipher block mode.

You could use ECB mode (which doesn't have IV feedback), but if you do, blocks that repeat themselves in plaintext will also repeat themselves in ciphertext, which opens you up to certain types of attacks.

Best would be to use CTR, where IV is based on a counter, but alas .NET does not seem to support it.

I would suggest you break the single file into several "virtual" files, each with its own cipherstream. Use CBC and populate the IV with bytes from the RNGCryptoServiceProvider. Seed the RNGCryptoServiceProvider with a value derived from the index of the virtual file within the physical file. That way you have an IV that varies from block to block and repeated plaintext won't show up in the ciphertext. Repeat the process when decrypting.

See also this question.

I would suggest you post your solution on security.stackexchange.com for review when you are ready.


You need to use Stream.Seek for your read stream. Have a look at the question at: StreamReader and seekingto help you avoid common pitfalls.

Keep in mind that you will have to remember the start & stop position of each stream precisely (particularly the encrypted stream length) otherwise you'll be unable to decrypt your file slices.

Good luck!