【问题标题】:Checking timestamp with iText 2.1.7使用 iText 2.1.7 检查时间戳
【发布时间】:2013-03-05 20:32:17
【问题描述】:

我正在尝试检查 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 类来验证签名。有谁知道我怎样才能到达那里?

【问题讨论】:

    标签: java itext digital-signature trusted-timestamp


    【解决方案1】:

    您应该使用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 censeo...不过,除非您被绑定到那个古老的 iText 版本(例如由于兼容性或许可问题),否则您应该考虑更新到当前版本。

    【讨论】:

    • 我之前有这个解决方案,但它无法编译。我下载了 itext 版本,发现 af.verifySignature(name) 有效。这是我使用的经过修改的第三方版本。谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-03-31
    • 2015-02-09
    • 2012-07-30
    • 1970-01-01
    • 2022-12-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多