BouncyCastle 创建 PKCS 7 加密文件? C#

发布于 2024-09-10 00:39:36 字数 938 浏览 4 评论 0原文

我正在尝试使用 BouncyCastle 使用 PKCS 7 文件标准加密文件。这是我输出 p7m 文件的代码。当我(使用 Entrust)解密文件时,系统会提示我输入密钥存储密码,因此它知道该文件是使用 AES 128 为我加密的,但它无法解密文件正文。加密一定出了问题。

byte[] fileContent = readFile(filename);

FileStream outStream = null;
Stream cryptoStream = null;
BinaryWriter binWriter = null;

try
{
    CmsEnvelopedDataStreamGenerator dataGenerator = new CmsEnvelopedDataStreamGenerator();
    dataGenerator.AddKeyTransRecipient(cert); //cert is the user's x509cert that i am encrypting for
    outStream = new FileStream(filename + ".p7m", FileMode.Create);
    cryptoStream = dataGenerator.Open(outStream, CmsEnvelopedGenerator.Aes128Cbc);
    binWriter = new BinaryWriter(cryptoStream);

    binWriter.Write(fileContent);
}

当我尝试使用 BouncyCastle 解密文件时,当我将文件内容传递给 CMSEnveloped 对象时,我收到此错误:

IOException converting stream to byte array: Attempted to read past the end of the stream.

有什么想法吗?

I am trying to use BouncyCastle to encrypt a file using the PKCS 7 file standard. Here is the code I have which outputs a p7m file. When I go to decrypt the file (using Entrust) I am prompted for my key store password, so it knows the file was encrypted for me using AES 128, but it cannot decrypt the body of the file. Something has to be going wrong on the encrypt.

byte[] fileContent = readFile(filename);

FileStream outStream = null;
Stream cryptoStream = null;
BinaryWriter binWriter = null;

try
{
    CmsEnvelopedDataStreamGenerator dataGenerator = new CmsEnvelopedDataStreamGenerator();
    dataGenerator.AddKeyTransRecipient(cert); //cert is the user's x509cert that i am encrypting for
    outStream = new FileStream(filename + ".p7m", FileMode.Create);
    cryptoStream = dataGenerator.Open(outStream, CmsEnvelopedGenerator.Aes128Cbc);
    binWriter = new BinaryWriter(cryptoStream);

    binWriter.Write(fileContent);
}

And when i try and decrypt the file using BouncyCastle I get this error when i pass the file contents to a CMSEnveloped Object:

IOException converting stream to byte array: Attempted to read past the end of the stream.

Any ideas whats going on here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

守不住的情 2024-09-17 00:39:36

我使用 EnvelopedCMS 类来完成此任务。

http://msdn.microsoft.com/en-us /library/bb924575(VS.90).aspx

I used the EnvelopedCMS class to accomplish this.

http://msdn.microsoft.com/en-us/library/bb924575(VS.90).aspx

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文