C# 计算 AES 的百分比 Encryption/Decryption

C# Calculating Percentage of AES Encryption/Decryption

我正在尝试 encrypt/decrypt 使用 AES 256 位的文件以及我从 here. The full code I am using is seen here 获得的代码。我想知道如何在写入文件时计算 while 循环中 encryption/decryption 的完成百分比。例如在加密中:

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?
}

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

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

如何显示由您决定。您可以更新表单控件(您可能需要使用 invoke if your cypto runs on a worker thread) or raise a custom event(例如 ProgressChanged)并在 UI 线程中使用它。

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

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

John Wu所述。对于解密,我使用了:

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

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