C#中的多线程加密 [英] Multithreading encryption in C#

查看:80
本文介绍了C#中的多线程加密的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不熟悉加密,正在使用以下方法加密文件:

I am new to encryption and am encrypting files using the following method:

private static void encryptFile(string filePath, byte[] password, byte[] salt)
{
    Rfc2898DeriveBytes rdb = new Rfc2898DeriveBytes(password, salt, 1000);
    AesManaged algorithm = new AesManaged();

    byte[] rgbKey = rdb.GetBytes(algorithm.KeySize / 8);
    byte[] rgbIV = rdb.GetBytes(algorithm.BlockSize / 8);
    GCHandle keyHandle = GCHandle.Alloc(rgbKey, GCHandleType.Pinned);
    GCHandle IVHandle = GCHandle.Alloc(rgbIV, GCHandleType.Pinned);

    ICryptoTransform cryptoAlgorithm = algorithm.CreateEncryptor(rgbKey, rgbIV);

    using (FileStream readStream = File.Open(filePath, FileMode.Open))
    {
        using (FileStream writeStream = new FileStream(filePath + ".enc", FileMode.Create, FileAccess.Write))
        {
            using (CryptoStream cryptoStream = new CryptoStream(writeStream, cryptoAlgorithm, CryptoStreamMode.Write))
            {
                while (readStream.Position < readStream.Length)
                {
                    byte[] buffer = new byte[4096];
                    int amountRead = readStream.Read(buffer, 0, buffer.Length);
                    cryptoStream.Write(buffer, 0, amountRead);
                }
                cryptoStream.Flush();
            }
        }
    }

    UtilityMethods.destroyBytes(rgbKey);
    UtilityMethods.destroyBytes(rgbIV);
    keyHandle.Free();
    IVHandle.Free();
}

我想做的是多线程处理,以加快加密速度.使用单个线程,这花费了5分钟以上的时间才能加密约3GB的文件.我希望能够在1分钟内完成加密(30秒以下的时间很棒,但我想我可能会延长).

What I want to do is multithread the process for faster encryption. Using a single thread, this has taken over 5 min to encrypt a ~3GB file. I'm looking to be able to do that encryption in under 1 min if possible (under 30 seconds would be fantastic, but I think I might be stretching).

我相信答案是创建多个流(尽管我不确定),为每个流分配一部分文件进行加密,但是我不确定如何拆分文件"来分配一块到每个流,或者在每个部分经过分配给它的流之后将文件放回一起".有人可以指出我正确的方向吗?

I believe the answer is to create multiple streams (although I'm not sure), assigning each stream a chunk of the file to encrypt, but I'm not sure how to "break the file apart" to assign a chunk to each stream, or "put the file back together" after each part has gone through the stream that it was assigned to. Could someone point me in the right direction?

非常感谢!

P.S.我已经看过这个( Rijndael算法和CryptoStream :是否可以加密/解密多线程?),但我不明白答案(ECB,CBC?).如果我的问题的答案在那里,您能否提供一些示例代码来使我朝正确的方向前进?

P.S. I've viewed this (Rijndael algorithm and CryptoStream: is it possible to encrypt / decrypt multithreaded?), but I don't understand the answer (ECB, CBC?). If the answer to my question lies there, could you possibly provide some sample code to get me going in the right direction?

再次感谢!

推荐答案

CBC(密码块链接)是AesManaged的默认块模式.

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

如果使用CBC,则不能与AES并行加密不同的块,因为例如,加密块1的副作用是为块2设置了新的IV.这称为"IV反馈".如果并行运行,则将无法正常工作.您将需要选择其他

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.

您可以使用ECB模式(没有IV反馈),但是如果这样做,以明文形式重复的块也将以密文形式重复,这使您容易遭受某些类型的攻击.

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.

最好是使用CTR,IV是基于计数器的,但是.NET似乎不支持它.

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

我建议您将单个文件分成几个虚拟"文件,每个文件都有自己的密码流.使用CBC,并使用

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.

另请参见此问题.

我建议您在准备就绪后将解决方案发布到 security.stackexchange.com 上进行审核.

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

这篇关于C#中的多线程加密的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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