【问题标题】:Why does passing a FileStream to a PGP Decryption function work but not a MemoryStream?为什么将 FileStream 传递给 PGP Decryption 函数而不是 MemoryStream?
【发布时间】:2020-09-07 05:49:35
【问题描述】:

我正在尝试通过内存流解密 PGP 加密文本。因为 PgpCore NuGet 包采用通用 Stream 对象,所以我希望 MemoryStream 能够工作。相反,我收到了诸如“流 47 中的未知对象”之类的模糊错误。

我查看了一堆在线资料表明编码是关键,但我只是得到不同编码的不同错误。当我使用 FileStream 对象对其进行测试时,解密函数运行良好,运行没有问题。

这不起作用:

using var pgp = new PGP();
var fileStream = new MemoryStream(Encoding.Default.GetBytes(File.ReadAllText(file)));
var privateKeyStream = new MemoryStream(Encoding.Default.GetBytes(File.ReadAllText("prod")));
pgp.DecryptStream(fileStream, outputStream, privateKeyStream, "[redacted]");

这样做:

using var pgp = new PGP();
var fileStream = File.OpenRead(file);
var privateKeyStream = new MemoryStream(Encoding.Default.GetBytes(File.ReadAllText("prod")));
pgp.DecryptStream(fileStream, outputStream, privateKeyStream, "[redacted]");

我错过了什么? FileStream 和 MemoryStream 有什么不同,一个有效,另一个无效?

【问题讨论】:

  • 您可能缺少 BOM

标签: c# encryption bouncycastle pgp openpgp


【解决方案1】:

Encoding.Default.GetBytes(File.ReadAllText(file))

这是一种相当奇怪的文件读取方式,并且可能会引入一些编码问题。尝试改用这个:

new MemoryStream(File.ReadAllBytes(file));

【讨论】:

  • 天哪,谢谢。老实说,我什至从未注意到 ReadAllBytes 可供我使用。
猜你喜欢
  • 1970-01-01
  • 2021-03-26
  • 2011-11-25
  • 2013-05-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多