-2

我正在使用高级加密标准算法创建一个文件加密应用程序,但我发现我的应用程序非常慢,尤其是当我加密大量数据时,例如。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);

    }
4

2 回答 2

2

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

这很简单,这样做:

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

于 2012-06-14T17:18:40.047 回答
0

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

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

于 2012-06-15T10:23:03.227 回答