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