使用 Bouncy Castle 库 c# 解密 pdf.p7m 文件的问题 [英] Problem in decrypting a pdf.p7m file with the Bouncy Castle library c#

查看:23
本文介绍了使用 Bouncy Castle 库 c# 解密 pdf.p7m 文件的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 c# 中翻译"了 java 代码,用于 pdf 文件的 dercrypt.我不明白为什么当我启动一个新的 CmsEnvelopedData 对象时,我得到一个异常:试图读取流的末尾".我还尝试在不安装 NuGet 包的情况下下载 Bouncy Castle 源代码,但我无法弄清楚可能是什么问题.感谢那些愿意提供帮助的人.

I have "translated" java code in c # for the dercrypt of a pdf file. I don't understand why when I start a new CmsEnvelopedData object, I get an exception: "Attempted to read past the end of the stream". I also tried to download the Bouncy Castle sources without installing the NuGet package, but I couldn't figure out what the problem might be. Thanks to those who will help.

代码 Java:

 public final synchronized byte[] decryptData(byte[] cipherData, String pwd)
    throws CSException 
{

    cipherData = Base64.decode(cipherData);

    PrivateKey privKey = null;

    privKey = loadKeyFromPKCS12( this.encPrivateKeyId, pwd);

    try
    {            
        CMSEnvelopedData envelopedData = new CMSEnvelopedData(cipherData);
        RecipientInformationStore  recipients = envelopedData.getRecipientInfos();
        Collection  c = recipients.getRecipients();
        Iterator    it = c.iterator();

        if (it.hasNext()) 
        {
            RecipientInformation   recipient = (RecipientInformation)it.next();

            this.outputBuffer = recipient.getContent(privKey);
        }
        else{
            this.outputBuffer = null;
        }
    }

    return this.outputBuffer;        
}

代码 C#:

  public byte[] DecryptFile(byte[] file)
    {


        var fileDecode = Org.BouncyCastle.Utilities.Encoders.Base64.Decode(file);

        CmsEnvelopedData envelopedData = new CmsEnvelopedData(fileDecode);

        RecipientInformationStore recipients = envelopedData.GetRecipientInfos();
        var c = recipients.GetRecipients();
        foreach (RecipientInformation recipient in c)
        {
            var decrypted = recipient.GetContent(RetrievePrivateKey());
            return decrypted;


        }

        return null;
    }

读取私钥的方法C#:

 private RsaKeyParameters RetrievePrivateKey()
    {

        var obj = AppConfiguration.GetBasePath();
        var path = obj.BasePath + obj.KeystoreFolder;
        var keyfolder = new DirectoryInfo(path);
        if (!keyfolder.Exists)
        {
            keyfolder.Create();
        }
        X509Certificate2 certi = new X509Certificate2(path + obj.KeystoreFile, "Password", X509KeyStorageFlags.Exportable | X509KeyStorageFlags.PersistKeySet);

        RSA crypt = certi.GetRSAPrivateKey();

        var Akp = Org.BouncyCastle.Security.DotNetUtilities.GetKeyPair(certi.PrivateKey).Private;

        return (RsaKeyParameters)Akp;
    }

当我尝试实例化一个新的 CmsEnvelopedData 对象时返回异常:

Exception returned when I attempt to instantiate a new CmsEnvelopedData object:

我还附上了示例中使用的加密示例文件:https://www.dropbox.com/s/gkwovnifpjf1xza/offer.pdf?dl=0

I also enclose the encrypted example file used in the example: https://www.dropbox.com/s/gkwovnifpjf1xza/offer.pdf?dl=0

推荐答案

您正在尝试解密部分文件.您显示的文件是单行 base64 字符串.解码后,它会生成一个 ASN.1 编码文件,其中包含大量 OCTET STRING 值.您得到的例外是当您尝试读取 ASN.1 编码的二进制值时,但流在完全检索之前就结束了.这通常是因为文件的尾部丢失了,但它当然也可能表明文件已被更改,例如当行尾在二进制文件中转换时,或者如果传输导致(现在不太可能)错误.

You're trying to decrypt a partial file. The file you showed was a single line base64 string. When decoded, it resulted into an ASN.1 encoded file with lots of OCTET STRING values. The exception you get is when you try and read an ASN.1 encoded binary value, but the stream ends before it can be fully retrieved. This commonly is because the tail of the file is missing, but it could of course also be an indication of a file that is altered, e.g. when line endings are converted in a binary file, or if transmission caused a (nowadays unlikely) error.

文件尾部经常丢失,因为文件在完全接收之前就被复制或移动了.例如.如果您使用 FTP 服务器,可能很难判断文件上传何时完成.

The tail of the file is often missing because the file is copied or moved before it is fully received. E.g. if you use an FTP server, it may be hard to tell when a file upload has completed.

这篇关于使用 Bouncy Castle 库 c# 解密 pdf.p7m 文件的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆