如何使用iText签名和验证多个签名pdf文件?

时间:2018-04-23 03:33:42

标签: java pdf itext signature

我正在尝试使用iText制作一个简单的演示标志并使用pdf文件验证多个签名。签名过程没问题,当我使用福昕阅读器查看pdf文件时,出现了两个签名字段。但是第一个签名,foxit宣布它已被改变。我尝试使用iText再次验证java代码,但得到了相同的结果。这是我的代码:

登录

    PdfReader reader;
    try {
        reader = new PdfReader(inputData);
    } catch (IOException e1) {
        LOG.error("IOException: " + e1.getMessage());
        return null;
    }
    ByteArrayOutputStream outStream = new ByteArrayOutputStream();
    PdfStamper stamper = null;
    try {
        stamper = PdfStamper.createSignature(reader, outStream, '\0');
    } catch (DocumentException e1) {
        LOG.error("DocumentException: " + e1.getMessage());
        return null;
    } catch (IOException e1) {
        LOG.error("IOException: " + e1.getMessage());
        return null;
    }
    PdfSignatureAppearance appearance = stamper.getSignatureAppearance();
    appearance.setVisibleSignature(rect, 1, gen.nextString());
    SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
    String singingTime = df.format(new Date());
    String signature = "Signed by: " + author + "\nReason: " + appearance.getReason()  + "\nDate: " + singingTime;
    appearance.setLayer2Text(signature);
    TSAClient tsaCli = null;
    if (useTsa) {
        tsaCli = new TSAClientBouncyCastle(tsaUrl, tsaAccount, tsaPwd);
    }

    try {
        MakeSignature.signDetached(appearance, digest, es, certChain, null,
                null, tsaCli, 0, CryptoStandard.CMS);

        result = outStream.toByteArray();

        outStream.close();
    } catch (IOException e) {
        LOG.error("IOException: " + e.getMessage());
    } catch (DocumentException e) {
        LOG.error("DocumentException: " + e.getMessage());
    } catch (GeneralSecurityException e) {
        LOG.error("GeneralSecurityException: " + e.getMessage());
    }

验证

 private int verify(byte[] signedData) {
    PdfReader reader;
    try {
        reader = new PdfReader(signedData);
    } catch (IOException e) {
        LOG.error("CANNOT LOAD SIGNED DATA. " + e.getMessage());

        return ValidationError.CANNOT_LOAD_SIGNED_DATA;
    }

    AcroFields fields = reader.getAcroFields();
    ArrayList<String> names = fields.getSignatureNames();

    if (names == null || names.isEmpty()) {
        LOG.error("SIGNATURE NOT FOUND");

        return ValidationError.SIGNATURE_NOT_FOUND;
    }

    for (String name : names) {
        PdfPKCS7 pkcs7 = fields.verifySignature(name);

        boolean signatureValid = false;
        try {
            signatureValid = pkcs7.verify();
        } catch (GeneralSecurityException e) {
            LOG.error("Signature with field name " + name + " is invalid");
        }

        if (!signatureValid) {

            LOG.error("[CONTENT]: Document has modified.");
            return ValidationError.SIGNATURE_INVALID; //The first signature return false here.
        }
    }

    return ValidationError.SIGNATURE_VALID;
}

我使用的是iText ver 5.5.8。

感谢。

0 个答案:

没有答案