Rijndael algorithm and CryptoStream: is it possible to encrypt / decrypt multithreaded?

前端 未结 1 1124
温柔的废话
温柔的废话 2021-01-27 07:58

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

相关标签:
1条回答
  • 2021-01-27 08:34

    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.

    0 讨论(0)
提交回复
热议问题