我想使用 AES 和密钥 IV 解密 PDF 文件 文件大小约为 90 MB,最大可达 200 MB。 我使用以下代码进行解码,需要一点时间,需要20多秒,而Java和Android编写的代码立即解码文件。
var key = Convert.FromBase64String("...");
var iv = Convert.FromBase64String("...");
using (Aes aesAlg = Aes.Create())
{
aesAlg.Key = key;
aesAlg.IV = iv;
ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);
using (FileStream encryptedStream = new FileStream(encryptedFilePath, FileMode.Open, FileAccess.Read, FileShare.Read, bufferSize: 8192, useAsync: true))
using (FileStream decryptedStream = new FileStream(outputFile, FileMode.Create, FileAccess.Write, FileShare.None, bufferSize: 8192, useAsync: true))
using (CryptoStream csDecrypt = new CryptoStream(encryptedStream, decryptor, CryptoStreamMode.Read))
{
byte[] buffer = new byte[8192]; // Adjust buffer size as needed
int bytesRead;
while ((bytesRead = await csDecrypt.ReadAsync(buffer, 0, buffer.Length)) > 0)
{
await decryptedStream.WriteAsync(buffer, 0, bytesRead);
}
}
}
如何尽快解码文件?
我解决了此代码的问题:
using Aes aes = Aes.Create();
aes.Key = key;
aes.IV = iv;
byte[] encryptedData = File.ReadAllBytes(encryptedFilePath);
using MemoryStream ms = new MemoryStream();
using (CryptoStream cs = new CryptoStream(ms, aes.CreateDecryptor(), CryptoStreamMode.Write))
{
await cs.WriteAsync(encryptedData, 0, encryptedData.Length);
await cs.FlushFinalBlockAsync();
}
byte[] decryptedData = ms.ToArray();
await File.WriteAllBytesAsync(outputFile, decryptedData);