Mis*_*ssy 5 c# encryption filestream
我有加密方法,运行缓慢.加密数百MB的数据大约需要20分钟.我不确定我是否采取了正确的方法.任何帮助,想法,建议将不胜感激.
private void AES_Encrypt(string inputFile, string outputFile, byte[] passwordBytes, byte[] saltBytes)
{
FileStream fsCrypt = new FileStream(outputFile, FileMode.Create);
RijndaelManaged AES = new RijndaelManaged();
AES.KeySize = 256;
AES.BlockSize = 128;
var key = new Rfc2898DeriveBytes(passwordBytes, saltBytes, 1000);
AES.Key = key.GetBytes(AES.KeySize / 8);
AES.IV = key.GetBytes(AES.BlockSize / 8);
AES.Padding = PaddingMode.Zeros;
AES.Mode = CipherMode.CBC;
CryptoStream cs = new CryptoStream(fsCrypt,
AES.CreateEncryptor(),
CryptoStreamMode.Write);
FileStream fsIn = new FileStream(inputFile, FileMode.Open);
int data;
while ((data = fsIn.ReadByte()) != -1)
cs.WriteByte((byte)data);
fsCrypt.Flush();
cs.Flush();
fsIn.Flush();
fsIn.Close();
cs.Close();
fsCrypt.Close();
}
Run Code Online (Sandbox Code Playgroud)
谢谢你的帮助!
Jon*_*eet 11
虽然加密速度很慢,但我不希望这是问题.我怀疑这是逐字节的IO导致不必要的开销.解决问题的最简单方法是通过明智的调用Stream.CopyTo- 当你处于它时,你应该使用using语句来适当地清理:
private void AesEncrypt(string inputFile, string outputFile, byte[] passwordBytes, byte[] saltBytes)
{
var key = new Rfc2898DeriveBytes(passwordBytes, saltBytes, 1000);
RijndaelManaged aes = new RijndaelManaged
{
KeySize = 256,
BlockSize = 128,
Key = key.GetBytes(AES.KeySize / 8),
IV = key.GetBytes(AES.BlockSize / 8),
Padding = PaddingMode.Zeros,
Mode = CipherMode.CBC
};
using (var output = File.Create(outputFile))
{
using (var crypto = new CryptoStream(output, aes.CreateEncryptor(), CryptoStreamMode.Write))
{
using (var input = File.OpenRead(inputFile))
{
input.CopyTo(crypto);
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
如其他答案所述,这不是产生IV的好方法.一般来说,我更愿意使用Rijndael.Create()而不是指定RijndaelManaged- 而且你可能也想要使用一个using语句.
您正在一次读取一个字节.这会产生很多开销.
要加快处理速度,请立即开始使用更多字节或调用内部复制功能:
fsIn.CopyTo(cs);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
405 次 |
| 最近记录: |