Multithreading the AES encryption in .NET

huangapple go评论64阅读模式
英文:

Multithreading the AES encryption in .NET

问题

I have written a program for encrypting files with AES. The encryption process is done on file streams.

The encryption method:

public static async Task EncryptUsingAESAsync(FileStream fileReadStream, FileStream fileWriteStream, byte[] key, byte[] iv)
{
    using(Aes aesAlg = Aes.Create())
    {
        aesAlg.Key = key;
        aesAlg.IV = iv;

        ICryptoTransform encryptor = aesAlg.CreateEncryptor();

        using(CryptoStream cryptoStream = new CryptoStream(fileWriteStream, encryptor, CryptoStreamMode.Write))
        {
            await fileReadStream.CopyToAsync(cryptoStream);
        }
    }
}

And this method will be called on,

public static async Task EncryptFileStreamAsync(string absolutePath, byte[] key, byte[] iv)
{
    using(FileStream fReadStream = new FileStream(absolutePath, FileMode.Open, FileAccess.Read))
    {
        using(FileStream fWriteStream = new FileStream($"{absolutePath}.EWW", FileMode.CreateNew, FileAccess.Write))
        {
            await fWriteStream.WriteAsync(iv, 0, iv.Length);
            await CryptoProvider.EncryptUsingAESAsync(fReadStream, fWriteStream, key, iv);
        }
    }

    File.Delete(absolutePath);
}

I have read that AES implementation in .NET actually uses Windows cryptographic APIs. So can I await these methods (especially EncryptUsingAESAsync method) like asynchronous IO or should I explicitly call it in TaskFactoy.StartNew() to get it done on a separate thread? or is there any other way to get the benefit of multithreading?

英文:

I have written a program for encrypting files with AES. The encryption process is done on file streams.

The encryption method:

public static async Task EncryptUsingAESAsync(FileStream fileReadStream, FileStream fileWriteStream, byte[] key, byte[] iv)
{
    using(Aes aesAlg = Aes.Create())
    {
        aesAlg.Key = key;
        aesAlg.IV = iv;

        ICryptoTransform encryptor = aesAlg.CreateEncryptor();

        using(CryptoStream cryptoStream = new CryptoStream(fileWriteStream, encryptor, CryptoStreamMode.Write))
        {
            await fileReadStream.CopyToAsync(cryptoStream);
        }
    }
}

And this method will be called on,

public static async Task EncryptFileStreamAsync(string absolutePath, byte[] key, byte[] iv)
{
    using(FileStream fReadStream = new FileStream(absolutePath, FileMode.Open, FileAccess.Read))
    {
        using(FileStream fWriteStream = new FileStream($"{absolutePath}.EWW", FileMode.CreateNew, FileAccess.Write))
        {
            await fWriteStream.WriteAsync(iv, 0, iv.Length);
            await CryptoProvider.EncryptUsingAESAsync(fReadStream, fWriteStream, key, iv);
        }
    }

    File.Delete(absolutePath);
}

I have read that AES implementation in .NET actually uses Windows cryptographic APIs. So can I await these methods (especially EncryptUsingAESAsync method) like asynchronous IO or should I explicitly call it in TaskFactoy.StartNew() to get it done on a seperate thread? or is there any other way to get the benefit of multithreading?

答案1

得分: 2

这取决于您的目标和环境问题。如果受到磁盘IO的限制,您将无法从并行运行这些活动中获得任何好处。

个人而言,我会在类似的环境中获取一些典型的文件集,并使用几种不同的实现进行基准测试,但我也会将 List<Task>.WhenAny(或 .WhenAll)添加到您要检查的潜在实现列表中。

如果您需要快速解决方案,我可能会选择使用异步方法,因为这将由运行时来管理(而且您已经构建了任务),如果环境具备管理它的环境开销,它将并行运行多个任务。我不确定 CryptoProvider.EncryptUsingAESAsync 是否在内部多线程运行,这可能需要进一步考虑。

英文:

It depends what your goal is, and your environment concerns. If you are limited by disk IO, you won't see any benefit from running these activities in parallel.

Personally, I'd get a few typical sets of files, in a similar environment, and benchmark this with a few implementations, but I'd also add List&lt;Task&gt;.WhenAny (or .WhenAll) to your list of potential implementations to check.

I'd probably go the Async route if you need a solution quickly, as this will then be managed by the runtime (and you've already built the tasks), it'll run multiple tasks in parallel if it has the environmental overhead to manage it. I'm not sure if the CryptoProvider.EncryptUsingAESAsync is internally multithreaded though, this could add further considerations.

huangapple
  • 本文由 发表于 2023年6月1日 12:30:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/76378673.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定