【发布时间】:2018-01-19 22:15:25
【问题描述】:
我的加密方法运行速度非常慢。加密数百 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();
}
感谢您的帮助!
【问题讨论】:
-
如果我猜的话,一次读取和写入一个字节可能会减慢您的速度。可能想尝试从
FileStream到ReadAllBytes,然后将Write生成的缓冲区到CryptoStream
标签: c# encryption filestream