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

时间:2010-07-16 13:34:31

标签: c# bouncycastle encryption-asymmetric pkcs#7

我正在尝试使用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.

这里有什么想法吗?

1 个答案:

答案 0 :(得分:0)

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

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