【问题标题】:Speedup my application加速我的应用程序
【发布时间】:2012-06-14 16:44:19
【问题描述】:

我正在使用高级加密标准算法创建一个文件加密应用程序,但我发现我的应用程序非常慢,尤其是当我加密大量数据时, 前任。 80MB 文件大小,30 分钟。已经过去了,但我的应用程序尚未完成对 80mb 大小的文件的加密。

我在我的加密算法中使用 ECB(电子密码本)模式

如何加快应用程序加密大量数据的速度?我做了一些研究,发现http://en.wikipedia.org/wiki/Speedup,但我不确定这是否是我问题的答案......或者如果我使用BackgroundWorker它是否有效?顺便说一句,我在项目开发中使用的是 Visual Studio 2008。

这是我加密文件的代码..

private void cmdEncrypt_Click(object sender, EventArgs e)
{
        AESECB aes = new AESECB();
        FileInfo fInfo = new FileInfo(txtFileSource.Text);

        if (txtFileSource.Text == "")
        {
            MessageBox.Show("Please Select a File", "Message", MessageBoxButtons.OK, MessageBoxIcon.Warning);

            return;
        }

        if (txtSecretKey.Text == "")
        {
            MessageBox.Show("Please Enter the password", "Message", MessageBoxButtons.OK, MessageBoxIcon.Warning);

            return;
        }

        if (fInfo.Exists == false)
        {
            MessageBox.Show("File Not Found!", "Message", MessageBoxButtons.OK, MessageBoxIcon.Warning);

            return;
        }

        byte[] bytePadding = aes.filePadding(txtFileSource.Text);
        byte[] fByte = aes.getFileByte(txtFileSource.Text);
        int farrLength = (bytePadding.Length + fByte.Length);
        byte[] newFbyte = new byte[farrLength];
        byte[] encryptedFByte = new byte[farrLength];
        int counterBytePadding =0;
        byte firstByte = 0;

        for (int i = 0; i < farrLength; i++)
        {
            if (i < fByte.Length)
            {
                newFbyte[i] = fByte[i];
            }
            else
            {
                newFbyte[i] = bytePadding[counterBytePadding];
                counterBytePadding++;
            }
        }

        int plainFileBlock = newFbyte.Length / 16;

        progressBar1.Maximum = plainFileBlock-1;

        progressBar1.Visible = true;

        int counter = 0;
        int counter2 = 0;

        for (int j = 0; j < plainFileBlock; j++)
        {
            byte[] encfbyte = aes.fileEncrypt(txtSecretKey.Text, newFbyte, counter);

            for (int k = 0; k < 16; k++)
            {
                encryptedFByte[counter2] = encfbyte[k];
                counter2++;
            }

            progressBar1.Value = j;
            counter = counter + 16;

        }

        progressBar1.Visible = false;

        int bytesToRead = encryptedFByte.Length;

        string newPath = txtFileSource.Text + ".aesenc";

        using (FileStream newFile = new FileStream(newPath, FileMode.Create, FileAccess.Write))
        {
            newFile.Write(encryptedFByte, 0, bytesToRead);

        }


        MessageBox.Show("Encryption Done!", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);

    }

【问题讨论】:

  • 我们无法准确回答。首先分析并找出究竟是什么是慢的。
  • 我已经说过,加密大量数据很慢,尤其是在加密80mb的数据时..
  • 如何加密?哪一行代码比较慢?
  • 我对每128bit的数据进行加密,不知道哪几行代码慢,因为我对自己的代码很有信心..

标签: c# encryption aes


【解决方案1】:

“对自己的代码充满信心”很好,但所有优秀的程序员都会衡量。

这很简单,这样做:

using System.Diagnostics;

var startTime = DateTime.Now;
//some code here
Debug.WriteLine("This took {0}", DateTime.Now.Subtract(startTime));

然后在VS中查看你的输出窗口(View->Output)。

通过用这两行包裹方法的不同部分,您将识别出慢位。

我怀疑你逐字节复制 80MB 的循环。在此处尝试Array.Resize

【讨论】:

  • 如何识别什么方法慢?因为所有的结果都是 0:00:00
  • 从宽开始,第一行在所有代码的顶部,最后一行在底部。那不应该显示为零。然后缩小范围。您可以添加多条第二行来执行此操作,为每一行提供不同的文本,只需记住显示的时间是从起始行到该点的总时间,但您可以使用以下行重置时钟:startTime = DateTime.Now; at随时。
【解决方案2】:

按照韦斯顿的建议,请测量您的代码,以确定慢的原因。也就是说,可能是文件加密 for loop 让你放慢了速度。如果是这样,您绝对可以通过使用超过 1 个 cpu 来加速该过程。在 C# 中查找并行结构:“Parallel.For”。

这是一个简单的例子: http://www.dotnetcurry.com/ShowArticle.aspx?ID=608

【讨论】:

    猜你喜欢
    • 2013-02-18
    • 2011-12-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-20
    • 2022-07-12
    相关资源
    最近更新 更多