【发布时间】: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