java.security.InvalidKeyException:在安装signed pdf时,没有安装的提供程序支持此键:(null)

时间:2018-04-11 07:49:10

标签: java cryptography itext digital-signature

我在

上收到此例外
PdfPKCS7 pkcs7 = fields.verifySignature(name);

这是完整的代码

import com.itextpdf.text.pdf.AcroFields;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.security.PdfPKCS7;
import java.io.IOException;
import java.security.GeneralSecurityException;
import java.util.ArrayList;

public class NewClass {

    public static void main(String arg[])
            throws IOException, GeneralSecurityException {
        String path = "C:/Akshay/output.pdf";
        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 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());

    }

}
使用

jar:

  1. itextpdf-5.5.13.jar
  2. bcprov-jdk14-1.49.jar

1 个答案:

答案 0 :(得分:3)

您应添加一个类似BouncyCastle的提供程序,并添加如下例所示的调用:

首先添加提供商

Security.addProvider(new BouncyCastleProvider());

然后添加提供者以获取签名

PdfPKCS7 pkcs7 = fields.verifySignature(name,"BC");