C# C语言中AES-256的多线程处理#
所以我做了一个加密文件的方法。但是,大型文件需要相当长的时间,我知道使用更多线程可以大大减少时间。我在多线程方面的经验很少。我正在Windows窗体应用程序中使用AES和.NET 4.7.2。 任何帮助都将不胜感激, 谢谢 编辑:我忘了添加方法,我道歉C# C语言中AES-256的多线程处理#,c#,.net,multithreading,winforms,encryption,C#,.net,Multithreading,Winforms,Encryption,所以我做了一个加密文件的方法。但是,大型文件需要相当长的时间,我知道使用更多线程可以大大减少时间。我在多线程方面的经验很少。我正在Windows窗体应用程序中使用AES和.NET 4.7.2。 任何帮助都将不胜感激, 谢谢 编辑:我忘了添加方法,我道歉 private void AES_Encrypt(string inputFile, string password) { try { byte[] salt = GenerateRandomSalt();
private void AES_Encrypt(string inputFile, string password)
{
try
{
byte[] salt = GenerateRandomSalt();
if (!deleteAfter.Checked)
{
FileStream fsCrypt = new FileStream(inputFile + ".encrypted", FileMode.Create);
byte[] passwordBytes = System.Text.Encoding.UTF8.GetBytes(password);
AesManaged AES = new AesManaged();
AES.KeySize = 256;
AES.BlockSize = 128;
var key = new Rfc2898DeriveBytes(passwordBytes, salt, 50000);
AES.Key = key.GetBytes(AES.KeySize / 8);
AES.IV = key.GetBytes(AES.BlockSize / 8);
fsCrypt.Write(salt, 0, salt.Length);
CryptoStream cs = new CryptoStream(fsCrypt, AES.CreateEncryptor(), CryptoStreamMode.Write);
FileStream fsIn = new FileStream(inputFile, FileMode.Open);
byte[] buffer = new byte[1048576];
int read;
long bufferLength = 0;
long fileLength = new FileInfo(inputFile).Length;
float bufferFloat = 0;
float fileFloat = 0;
float finalFloat = 0;
try
{
while ((read = fsIn.Read(buffer, 0, buffer.Length)) > 0)
{
Application.DoEvents();
cs.Write(buffer, 0, read);
bufferLength += read;
bufferFloat = (float)bufferLength;
fileFloat = (float)fileLength;
finalFloat = (bufferFloat / fileFloat) * 100;
int finalInt = (int)finalFloat;
percentage.Value = finalInt;
percentageText.Text = finalFloat.ToString() + "%";
double kBytes = (double)bufferLength / 1000;
double mBytes = (double)bufferLength / 1000000;
double gBytes = (double)bufferLength / 1000000000;
bytesProcessed.Text = bufferLength.ToString() + " bytes, or " + kBytes.ToString() + " kilobytes, or " + mBytes.ToString() + " megabytyes, or " + gBytes.ToString() + " gigabytes.";
}
fsIn.Close();
}
catch (Exception ex)
{
output.Items.Add("Error: " + ex.Message);
}
finally
{
cs.Close();
fsCrypt.Close();
output.Items.Add("Successfully encrypted file: " + inputFile);
percentage.Value = 0;
percentageText.Text = "";
bytesProcessed.Text = "";
}
}
else
{
FileStream fsCrypt = new FileStream(inputFile + ".encrypted", FileMode.Create);
byte[] passwordBytes = System.Text.Encoding.UTF8.GetBytes(password);
AesManaged AES = new AesManaged();
AES.KeySize = 256;
AES.BlockSize = 128;
var key = new Rfc2898DeriveBytes(passwordBytes, salt, 50000);
AES.Key = key.GetBytes(AES.KeySize / 8);
AES.IV = key.GetBytes(AES.BlockSize / 8);
fsCrypt.Write(salt, 0, salt.Length);
CryptoStream cs = new CryptoStream(fsCrypt, AES.CreateEncryptor(), CryptoStreamMode.Write);
FileStream fsIn = new FileStream(inputFile, FileMode.Open);
byte[] buffer = new byte[1048576];
int read;
long bufferLength = 0;
long fileLength = new FileInfo(inputFile).Length;
float bufferFloat = 0;
float fileFloat = 0;
float finalFloat = 0;
try
{
while ((read = fsIn.Read(buffer, 0, buffer.Length)) > 0)
{
Application.DoEvents();
cs.Write(buffer, 0, read);
bufferLength += read;
bufferFloat = (float)bufferLength;
fileFloat = (float)fileLength;
finalFloat = (bufferFloat / fileFloat) * 100;
int finalInt = (int)finalFloat;
percentage.Value = finalInt;
percentageText.Text = finalFloat.ToString() + "%";
double kBytes = (double)bufferLength / 1000;
double mBytes = (double)bufferLength / 1000000;
double gBytes = (double)bufferLength / 1000000000;
bytesProcessed.Text = bufferLength.ToString() + " bytes, or " + kBytes.ToString() + " kilobytes, or " + mBytes.ToString() + " megabytyes, or " + gBytes.ToString() + " gigabytes.";
}
fsIn.Close();
}
catch (Exception ex)
{
output.Items.Add("Error: " + ex.Message);
}
finally
{
cs.Close();
fsCrypt.Close();
File.Delete(inputFile);
output.Items.Add("Successfully encrypted file: " + inputFile);
percentage.Value = 0;
percentageText.Text = "";
bytesProcessed.Text = "";
}
}
}
catch (Exception e)
{
output.Items.Add(e.Message);
}
}
如果进程可以并行化,多线程可以节省时间。但并不是每个进程都可以并行化,CBC模式(默认为)是关于可并行性(也考虑注释)的讨论。加密可能需要很长时间,因为涉及到I/O,并不是因为计算很重。为什么您明确地使用<代码> AsEXECUTE < /代码>而不是<代码> AES.CREATE()<代码>?管理版本仅为软件版本;例如,它不会使用AES-NI等处理器指令。通常,使用多种威胁不会加快文件加密速度。这些操作非常依赖于I/O,顺序磁盘访问比随机I/O快得多。HDD的速度可以降到1,5 MiB/s,甚至更低。当然,这是首选SSD的一个重要原因。您可以将
Rfc2898DeriveBytes
从循环中取出,让其他线程创建密钥。让函数返回密钥和salt并缓存它们,直到需要它们为止。很明显,这种方法会减慢加密算法的速度。或者,您可以使用一个随机数据密钥,并使用在单独线程中计算的密钥对该密钥进行加密(包装)。(很抱歉有这么多评论,但它们是独立的问题,希望能有所帮助:))