Rijndael算法和CryptoStream:是否可以加密/解密多线程? [英] Rijndael algorithm and CryptoStream: is it possible to encrypt / decrypt multithreaded?

查看:138
本文介绍了Rijndael算法和CryptoStream:是否可以加密/解密多线程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Rijndael加密/解密一些文档。我想知道是否有一个C#的实现,允许手动或通过使用 Parallel 框架多线程使用该算法。我认为这是不可能的,因为它是基于流( CryptoStream ),但值得问一问。
有没有人有来源来检查?

解决方案

我从来没有听说过.NET中的多线程CryptoStream,这取决于你的加密模式。
如果加密模式是ECB,当然,您可以使用Parallel.For或ForEach轻松地手动进行多线程操作。
使用CBC或任何其他具有反馈的加密模式,除非您使用多个初始化向量,否则不太可能使其并行。
对于ECB模式:




  • 将数据拆分为多个字节数组(可以说您使用10个线程加密1280个字节,数据到包含0..127字节,128..255字节等的10字节数组,每个数组必须包含整数个块)

  • 使用Parallel.For或ForEach循环通过所有10个字节的数组(例如,创建List< byte []>实例并将其提供给Parallel.ForEach作为参数)。

  • 使用SymmetricAlgorithm创建10个Rijndael加密器/解密器实例.CreateEncryptor / SymmetricAlgorithm.CreateDecryptor

  • 在单独的线程上加密每个部分的数据。

  • 再次将数据合并到1000字节数组。



所以,我的想法是不要使用CryptoStream,而是必须调用加密API并直接操作纯文本字节。


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?

解决方案

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.

这篇关于Rijndael算法和CryptoStream:是否可以加密/解密多线程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆