【问题标题】:Problem in decrypting a pdf.p7m file with the Bouncy Castle library c#使用 Bouncy Castle 库 c# 解密 pdf.p7m 文件的问题
【发布时间】:2019-05-08 08:23:49
【问题描述】:

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

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 对象时返回异常:

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

【问题讨论】:

  • 等一下,您是否要直接通过 CMS 解密实际的 PDF 文件? PDF 可能包含签名或加密数据,但 PDF 不符合 CMS 规范(或者无法使用 PDF 查看器打开)。我不会在我的计算机上“执行”不受信任的 PDF 文件,非常感谢。
  • 这是一个经过数字签名和加密的文件。别担心,我正在寻找解决方案,而不是“感染”人!
  • 我下载的文件首先编码为base 64。解码后它似乎在OCTET STRING中包含很多数据,但我感觉文件被缩短为openssl asn1parse也失败了。也许您的 IT 部门认为执行 FTP-then-copy 会很有用,但在 FTP 完成传输时没有任何明确的指示。就我而言,双重尴尬,因为我告诉他们这会失败,并且当它失败时他们不应该带着部分文件来找我。确实如此,他们来找我了。还要提防文本文件传输等。
  • 如果不是PDF,那就别叫PDF了。
  • 当然是这样的。例外和错误似乎表明了这一点,但老实说,它们都没有过于具体。

标签: java c# encryption .net-core bouncycastle


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 2012-06-21
    • 2011-08-22
    • 2011-08-20
    • 2012-04-25
    • 2012-06-15
    • 1970-01-01
    • 1970-01-01
    • 2017-04-30
    • 2012-05-10
    相关资源
    最近更新 更多