验证数字签名 pdf java IText
verify digital signature pdf java IText
我试图用我的令牌验证我添加到带有 IText 的 pdf 文件的签名,但我遇到了一些错误:
java.security.InvalidKeyException: No installed provider supports this key: (null)
这是我的代码:
//verifying pdf signatures
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();
}
这是整个日志:
===== sig_2 =====
Signature covers whole document: true
Document revision: 1 of 1
Exception in thread "main" ExceptionConverter:
java.security.InvalidKeyException: No installed provider supports this key: (null)
at java.security.Signature$Delegate.chooseProvider(Unknown Source)
at java.security.Signature$Delegate.engineInitVerify(Unknown Source)
at java.security.Signature.initVerify(Unknown Source)
at com.itextpdf.text.pdf.security.PdfPKCS7.<init>(PdfPKCS7.java:452)
at com.itextpdf.text.pdf.AcroFields.verifySignature(AcroFields.java:2317)
at com.itextpdf.text.pdf.AcroFields.verifySignature(AcroFields.java:2270)
at com.pdf.sign.Main.verifySignature(Main.java:91)
at com.pdf.sign.Main.verifySignatures(Main.java:103)
at com.pdf.sign.Main.main(Main.java:147)
您的密钥库似乎没有初始化,因此您的密钥为空。
在那里您还可以查看源代码并找到抛出异常的位置和原因。
这是 Bouncy Castle 的问题(参见 https://github.com/bcgit/bc-java/issues/144)。您需要在检查 PDF 签名之前添加以下行
Security.addProvider(new BouncyCastleProvider());
我试图用我的令牌验证我添加到带有 IText 的 pdf 文件的签名,但我遇到了一些错误:
java.security.InvalidKeyException: No installed provider supports this key: (null)
这是我的代码:
//verifying pdf signatures
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();
}
这是整个日志:
===== sig_2 =====
Signature covers whole document: true
Document revision: 1 of 1
Exception in thread "main" ExceptionConverter:
java.security.InvalidKeyException: No installed provider supports this key: (null)
at java.security.Signature$Delegate.chooseProvider(Unknown Source)
at java.security.Signature$Delegate.engineInitVerify(Unknown Source)
at java.security.Signature.initVerify(Unknown Source)
at com.itextpdf.text.pdf.security.PdfPKCS7.<init>(PdfPKCS7.java:452)
at com.itextpdf.text.pdf.AcroFields.verifySignature(AcroFields.java:2317)
at com.itextpdf.text.pdf.AcroFields.verifySignature(AcroFields.java:2270)
at com.pdf.sign.Main.verifySignature(Main.java:91)
at com.pdf.sign.Main.verifySignatures(Main.java:103)
at com.pdf.sign.Main.main(Main.java:147)
您的密钥库似乎没有初始化,因此您的密钥为空。
在那里您还可以查看源代码并找到抛出异常的位置和原因。
这是 Bouncy Castle 的问题(参见 https://github.com/bcgit/bc-java/issues/144)。您需要在检查 PDF 签名之前添加以下行
Security.addProvider(new BouncyCastleProvider());