I'm using Rijndael to encrypt / decrypt some documents. I'm wondering if there is an implementation for C# that allows multithreaded usage of the algorithm, either manually or by using the Parallel
framework? I assume it is not possible because it is based on streams (CryptoStream
) but it's worth asking still.
Does anybody have sources to check?
Asked
Active
Viewed 1,172 times
0

Krumelur
- 32,180
- 27
- 124
- 263
1 Answers
1
I never heard about multithreaded CryptoStream in .NET, however, I think it depends on your encryption mode. If encryption mode is ECB, of course, you can easily make it multithreaded manually with Parallel.For or ForEach. With CBC or any other encryption mode with feedback, it is unlikely you can make it parallel, unless you'll use multiple initialization vectors. For ECB mode:
- Split your data in multiple bytes arrays (lets say you encrypt 1280 bytes with 10 threads, that split data to 10 byte arrays that contain 0..127 byte, 128..255 byte, etc, each array must contain integer number of blocks).
- Use Parallel.For or ForEach to cycle by all 10 byte arrays (for example, create List <byte[]> instance and supply it to Parallel.ForEach as argument).
- Create 10 Rijndael encryptor/decryptor instances with SymmetricAlgorithm.CreateEncryptor/SymmetricAlgorithm.CreateDecryptor
- Encrypt each part of data on separate thread.
- Combine data to 1000 bytes array again.
So, my idea is not to use CryptoStream, instead you have to call encryption API and operate plain text bytes directly.

Vitalii
- 4,434
- 4
- 35
- 77
-
That's using too much memory. It would be optimal to have two ore more streams, each streaming a part of the file. – Krumelur Dec 13 '11 at 11:38
-
@Krumelur Yes, you right, instead of byte arrays creation you can start multiple streams (for example, read file in shared mode starting from different file positions). For ECB mode it doesn't matter in what order do you encrypt file, that is why ECB mode is suitable for parallelization. So, creation is 10 ECB CryptoStream instances and giving them to Parallel.ForEach is also solution. However, you have to be attentive and disable automatic padding, or CryptoStream will pad each of 10 or more threads and write extra blocks in your file. Padding must be disabled. – Vitalii Dec 13 '11 at 11:41
-
Sounds very interesting. Have you tried it or is it al plain theory? (If you know it is working, I will give it a try rather earlier than later. if it is theory, I will have to postpone it for now) – Krumelur Dec 13 '11 at 11:43
-
It is a plain theory, I never had so large files that I had to encrypt in parallel. However, it must work, at least there are not any reason why not. – Vitalii Dec 13 '11 at 11:44
-
My main concern is ECB mode. It is not very safe. I will give it a try one day. – Krumelur Dec 13 '11 at 11:46
-
2ECB is not secure. CTR mode can be parallelised, with a little care, and is a lot more secure. – rossum Dec 13 '11 at 13:30
-
That's the point: what's the correct approach to parallize CTR? If you answer, you might score :-) – Krumelur Dec 16 '11 at 13:02
-
1CTR mode encryption uses a cipher stream, which you can XOR with the plain text (both for encoding as well as decoding). So you can have a buffer which is e.g. filled from multiple threads each holding a copy of the key. Then you simply XOR the data from the document with the cipher text. Probably IO will be the bottleneck regardsless, but that's beyond this question. – Maarten Bodewes Dec 18 '11 at 02:55