gpt4 book ai didi

java - 使用 iText 2.1.7 检查时间戳

转载 作者:太空宇宙 更新时间:2023-11-04 07:46:19 30 4
gpt4 key购买 nike

我正在尝试检查 PDF 文件的给定签名是否存在时间戳。到目前为止,我得到了这段代码:

RandomAccessFileOrArray random = 
new RandomAccessFileOrArray(new File("temp.pdf").getAbsolutePath());

PdfReader reader = new PdfReader(random, null);
AcroFields af = reader.getAcroFields();
ArrayList<?> names = af.getSignatureNames();

//this are the signatures?
for (Object o : names){

AcroFields.Item item = (Item) af.getFields().get((String)o);

//this is the class for verifying the signature,
//how do I get it from the item?
PdfPKCS7 pdfPKCS7 = null; //XYZ ???

Calendar signingDate = pdfPKCS7.getTimeStampDate();
}

我显然可以访问签名,但我应该进入 PdfPKCS7 类来验证签名。有谁知道我怎样才能到达那里?

最佳答案

您应该使用 AcroFields 方法 verifySignature(String name),它返回一个 PdfPKCS7 对象以继续验证。

该方法的 JavaDocs 显示了其使用示例:

KeyStore kall = PdfPKCS7.loadCacertsKeyStore();
PdfReader reader = new PdfReader("my_signed_doc.pdf");
AcroFields af = reader.getAcroFields();
ArrayList names = af.getSignatureNames();
for (int k = 0; k < names.size(); ++k) {
String name = (String)names.get(k);
System.out.println("Signature name: " + name);
System.out.println("Signature covers whole document: " + af.signatureCoversWholeDocument(name));
PdfPKCS7 pk = af.verifySignature(name);
Calendar cal = pk.getSignDate();
Certificate pkc[] = pk.getCertificates();
System.out.println("Subject: " + PdfPKCS7.getSubjectFields(pk.getSigningCertificate()));
System.out.println("Document modified: " + !pk.verify());
Object fails[] = PdfPKCS7.verifyCertificates(pkc, kall, null, cal);
if (fails == null)
System.out.println("Certificates verified against the KeyStore");
else
System.out.println("Certificate failed: " + fails[1]);
}

在这里,您可以使用 PdfPKCS7 实例轻松添加其他代码。

Ceterum ceneo...除非您必须使用旧的 iText 版本(例如,由于兼容性或许可问题),否则您应该考虑更新到当前版本。

关于java - 使用 iText 2.1.7 检查时间戳,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15233508/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com