【问题标题】:C# Calculating Percentage of AES Encryption/DecryptionC# 计算 AES 加密/解密的百分比
【发布时间】:2016-08-19 18:52:17
【问题描述】:

我正在尝试使用从here 获得的代码使用 AES 256 位加密/解密文件。我使用的完整代码见here。我想知道如何计算在 while 循环中写入文件时加密/解密完成的百分比。以加密为例:

while ((read = fsIn.Read(buffer, 0, buffer.Length)) > 0)
{
    cs.Write(buffer, 0, read);
    //int percentage = Calculate percentage done here?
}

在解密中:

while ((read = cs.Read(buffer, 0, buffer.Length)) > 0)
{
    fsOut.Write(buffer, 0, read);
    //int percentage = Calculate percentage done here?
}

【问题讨论】:

  • 对不起,我的错。感谢您修复它。

标签: c# file-io filestream percentage


【解决方案1】:

您可以按如下方式计算完成百分比:

var percentComplete = (float)fsIn.Position * 100 / fsIn.Length;

如何显示它取决于您。您可以更新表单控件(如果您的 cypto 在工作线程上运行,您可能需要使用invoke)或引发custom event(例如ProgressChanged)并在您的UI线程中使用它。

【讨论】:

  • 谢谢,我在我的最终答案中使用了这个,我还包括如何显示文件解密的百分比:)
【解决方案2】:

我能够让它同时使用加密和解密。对于加密,我使用了:

var percentComplete = (float)fsIn.Position * 100 / fsIn.Length;

正如John Wu 所说。对于解密,我使用了:

var percentComplete = (float)fsOut.Position * 100 / fsCrypt.Length;

这是可行的,因为它将 Position(x100) 除以 加密文件的总长度,而不是 fsOut.Length;它只返回已写入新解密文件的数据。

【讨论】:

    猜你喜欢
    • 2013-04-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-24
    • 2016-09-24
    • 2015-05-01
    相关资源
    最近更新 更多