file - 从 Crypto Stream 解密 PDF 文件流时未声明字符编码
问题描述
我有以下方法来加密文件并将它们保存到磁盘:
using (FileStream fileStream = new(filePath, FileMode.OpenOrCreate))
{
using (Aes aes = Aes.Create())
{
byte[] key =
{
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16
};
aes.Key = key;
byte[] iv = aes.IV;
fileStream.Write(iv, 0, iv.Length);
using (CryptoStream cryptoStream = new(fileStream, aes.CreateEncryptor(), CryptoStreamMode.Write))
{
using (StreamWriter encryptWriter = new(cryptoStream))
{
await input.File.CopyToAsync(cryptoStream);
}
}
}
}
以下方法可以从磁盘解密文件并在用户浏览器中打开:
using (FileStream fileStream = new(filePath, FileMode.Open))
{
using (Aes aes = Aes.Create())
{
byte[] iv = new byte[aes.IV.Length];
int numBytesToRead = aes.IV.Length;
int numBytesRead = 0;
while (numBytesToRead > 0)
{
int n = fileStream.Read(iv, numBytesRead, numBytesToRead);
if (n == 0) break;
numBytesRead += n;
numBytesToRead -= n;
}
byte[] key =
{
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16
};
using (CryptoStream cryptoStream = new(fileStream, aes.CreateDecryptor(key, iv), CryptoStreamMode.Read))
{
// Desired option - not saving unenrypted to disk
var ms = new MemoryStream();
await cryptoStream.CopyToAsync(ms);
return new FileStreamResult(ms, "application/pdf");
// TESTING PURPOSES ONLY - saving unenrypted to disk
//var outputStream = new FileStream("C:\\Files\\unencrypted.pdf", FileMode.Create);
//await cryptoStream.CopyToAsync(outputStream);
//return new FileStreamResult(outputStream, "application/pdf");
}
}
}
我希望用户能够在浏览器中打开文档,而无需将未加密的文件保存回磁盘。
然而,上面的解密结果如下:
未声明纯文本文档的字符编码。如果文档包含 US-ASCII 范围之外的字符,则文档将在某些浏览器配置中呈现乱码。文件的字符编码需要在传输协议中声明或文件需要使用字节顺序标记作为编码签名。
注释掉的代码将未加密的文件保存到磁盘,并且可以从 Windows 资源管理器中打开。但是,在实际应用程序中运行时仍然会导致上述相同的错误。
当然,如果其中的 PDF 具有正确的编码,那么一旦从流的开头删除 IV,未加密的版本也应该如此?
解决方案
推荐阅读
- java - 为什么在 Java 8 中删除了 HashMap 的 indexFor 方法
- rest - Spring-boot Web发送回显响应,每隔一个请求
- wordpress - 如何为网站创建 Green lock SSL 证书?
- python - WTF 表单不验证
- angular - 如何在单个中获取多个复选框值
- spring - Kafka Consumer 处理后推送记录到 S3 的可能测试场景
- r - 如何在 x 和 y 坐标中绘制水平(水平图)?
- amazon-web-services - 当 Spot 实例收到终止请求时,是否可以创建映像 (AMI)?
- flutter - 如何处理 Flutter 中的 sqlite 数据库版本控制?
- regex - RegEx 用于匹配和提取 UL 和 DL 速度到变量中