如何用itext验证pdf的数字签名?

时间:2017-09-11 17:53:02

标签: java itext digital-signature

我不知道为什么这段代码错了。我正在使用来自itext的代码,但是在导入所有依赖项时给出了错误evin。以下是我在项目中使用的代码。 Plase帮助我,某人。
https://developers.itextpdf.com/examples/security/digital-signatures-white-paper/digital-signatures-chapter-5

bar

文件POM就是这个。

import java.io.IOException;
import java.security.GeneralSecurityException;
import java.security.Security;
import java.util.ArrayList;

import org.bouncycastle.jce.provider.BouncyCastleProvider;

import com.itextpdf.text.log.LoggerFactory;
import com.itextpdf.text.log.SysoLogger;
import com.itextpdf.text.pdf.AcroFields;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.security.PdfPKCS7;

public class PdfReaderExample {
    public static final String EXAMPLE1 = "/opt/doc.pdf";

    public PdfPKCS7 verifySignature(AcroFields fields, String name) throws GeneralSecurityException, IOException {
        System.out.println("Signature covers whole document: " + fields.signatureCoversWholeDocument(name));
        System.out.println("Document revision: " + fields.getRevision(name) + " of " + fields.getTotalRevisions());
        PdfPKCS7 pkcs7 = fields.verifySignature(name);
        System.out.println("Integrity check OK? " + pkcs7.verify());
        return pkcs7;
    }

    public void verifySignatures(String path) throws IOException, GeneralSecurityException {
        System.out.println(path);
        PdfReader reader = new PdfReader(path);
        AcroFields fields = reader.getAcroFields();
        ArrayList<String> names = fields.getSignatureNames();
        for (String name : names) {
            System.out.println("===== " + name + " =====");
            verifySignature(fields, name);
        }
        System.out.println();
    }

    public static void main(String[] args) throws IOException, GeneralSecurityException {
        LoggerFactory.getInstance().setLogger(new SysoLogger());
        BouncyCastleProvider provider = new BouncyCastleProvider();
        Security.addProvider(provider);
        PdfReaderExample app = new PdfReaderExample();
        app.verifySignatures(EXAMPLE1);
    }
}

===== Signature2 =====
    Signature covers whole document: true
    Document revision: 1 of 1
    Exception in thread "main" java.lang.VerifyError: (class: org/bouncycastle/cms/CMSSignedHelper, method: <clinit> signature: ()V) Incompatible argument to function
        at org.bouncycastle.cms.CMSSignedData.<clinit>(Unknown Source)
        at org.bouncycastle.tsp.TimeStampToken.getSignedData(Unknown Source)
        at org.bouncycastle.tsp.TimeStampToken.<init>(Unknown Source)
        at com.itextpdf.text.pdf.security.PdfPKCS7.<init>(PdfPKCS7.java:402)
        at com.itextpdf.text.pdf.AcroFields.verifySignature(AcroFields.java:2419)
        at com.itextpdf.text.pdf.AcroFields.verifySignature(AcroFields.java:2372)
        at PdfReaderExample.verifySignature(PdfReaderExample.java:20)
        at PdfReaderExample.verifySignatures(PdfReaderExample.java:32)
        at PdfReaderExample.main(PdfReaderExample.java:42)

    Process finished with exit code 1

0 个答案:

没有答案