【问题标题】:PDF Digital Signature in java and signature verification in c# (iText)java中的PDF数字签名和c#中的签名验证(iText)
【发布时间】:2014-04-19 21:58:03
【问题描述】:

我正在用 c# 开发一个执行数字签名验证的网络服务器,以确保不修改 pdf 文件。 我正在为此使用 iText 和 iTextSharp。

但是客户端是基于 java 小程序的。我在那个 java 小程序中执行数字签名。在java中,我能够制作签名然后验证它们。但是,如果我在 C# 中验证签名,则会得到 nullreferenceexception。

这是我的 Java 数字签名代码:

           String path = "C:/Users/a/Desktop/cert.pfx";
    String keystore_password = "fgf";
    String key_password = "fgf";

    ////

    BouncyCastleProvider provider = new BouncyCastleProvider();
    Security.addProvider(provider);


    KeyStore ks = KeyStore.getInstance("pkcs12", "BC");
    ks.load(new FileInputStream(path), keystore_password.toCharArray());

    String alias = (String)ks.aliases().nextElement();

    PrivateKey pk = (PrivateKey) ks.getKey(alias, key_password.toCharArray());

    Certificate[] chain = ks.getCertificateChain(alias);

            PdfReader reader = new PdfReader(src);
    dest = "C:/Users/a/Desktop/" + dest;
    FileOutputStream os = new FileOutputStream(dest);
    PdfStamper stamper = PdfStamper.createSignature(reader, os, '\0');

    PdfSignatureAppearance appearance = stamper.getSignatureAppearance();


    ExternalSignature es = new PrivateKeySignature(pk, "SHA-256", "BC");
    ExternalDigest digest = new BouncyCastleDigest();

    MakeSignature.signDetached(appearance, digest, es, chain, null, null, null, 0, CryptoStandard.CMS);

还有我的C#验证码:

             PdfReader reader = new PdfReader(pdfFile);
            AcroFields af = reader.AcroFields;
            var names = af.GetSignatureNames();

            if (names.Count == 0)
            {
                throw new InvalidOperationException("No Signature present in pdf file.");
            }


            foreach (string name in names)
            {
                if (!af.SignatureCoversWholeDocument(name))
                {
                    throw new InvalidOperationException(string.Format("The signature: {0} does not covers the whole document.", name));
                }


                PdfPKCS7 pk = af.VerifySignature(name);
                var cal = pk.SignDate;
                var pkc = pk.Certificates;

                if (!pk.Verify())
                {
                    Console.WriteLine("The signature is not valid.");
                    return false;
                }
             }

在 af.VerifySignature(name) 行中; NullReferenceException 被抛出!

有趣的是,如果我使用 C# 代码执行签名,我可以在 java 中验证它,因为我添加了以下指令: BouncyCastleProvider 提供者 = 新 BouncyCastleProvider(); Security.addProvider(provider);

我认为我的问题依赖于一些字节转换...但是在 C# 中我不知道如何调用 bouncycastleprovider。

你能帮帮我吗? 我最好的问候:威廉。

【问题讨论】:

  • 在 af.VerifySignature(name) 行中; NullReferenceException 被抛出! - 你能提供类似堆栈跟踪的东西吗?
  • stackTrace:在 iTextSharp.text.pdf.PdfPKCS7..ctor(Byte[] contentsKey) 在 iTextSharp.text.pdf.AcroFields 的 org.bouncycastle.security.SignerUtil.getSigner(String algorithm) .VerifySignature(String name) at SignatureLibrary.iText.PDFValidation(String pdfFile) in c:\\Users\\guilhermesousa\\Documents\\Visual Studio 2012\\Projects\\SignatureLibrary\\SignatureLibrary\\iText.cs:line 122 "
  • 您的 iTextSharp 版本是什么?我找不到只有一个 Byte[] 参数的 PdfPKCS7 构造函数...
  • 我认为它是 5.5.0 版本.. 但我不确定。我的 dll 只是说 itextsharp。但是你为什么要构建一个 PdfPKCS7 呢?您只需将其分配给 af.VerifySignature(name);构建它。
  • stacktrace 行iTextSharp.text.pdf.PdfPKCS7..ctor(Byte[] contentsKey) 表示您实际上并未使用5.5.0 版本,而是5.3.0 之前的版本,因为5.3.0 中的PdfPKCS7 已被重构到命名空间iTextSharp.text.pdf.security .因此,请更新您的 iTextSharp,再次测试,如果您仍然收到 NullReferenceException,请发布更新的堆栈跟踪。

标签: pdf itextsharp itext digital-signature pki


【解决方案1】:

OP 在评论中发布的堆栈跟踪

...
at org.bouncycastle.security.SignerUtil.getSigner(String algorithm)
at iTextSharp.text.pdf.PdfPKCS7..ctor(Byte[] contentsKey)
at iTextSharp.text.pdf.AcroFields.VerifySignature(String name)
at SignatureLibrary.iText.PDFValidation(String pdfFile)
in ...\\SignatureLibrary\\SignatureLibrary\\iText.cs:line 122

包含 iTextSharp.text.pdf.PdfPKCS7..ctor(Byte[] contentsKey) 行,这表明 OP 没有使用当前的 iTextSharp 版本 5.5.0,而是使用 5.3.0 之前的版本(2012 年 6 月发布):在版本 5.3.0 中 PdfPKCS7 已被重构进入命名空间iTextSharp.text.pdf.security

此重构是整个 iText 签名创建和验证代码的主要更新的一部分,该更新引入了许多新功能。

因此,建议 OP 更新 iTextSharp 程序集,实际上:

我下载了最新的itextsharp版本,验证效果很好。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-06-25
    • 1970-01-01
    • 2017-10-22
    • 2013-07-01
    • 2018-09-18
    • 2015-08-21
    • 1970-01-01
    相关资源
    最近更新 更多