使用iText对PDF进行多次数字签名

时间:2013-07-03 15:00:29

标签: java pdf certificate itext digital-signature

我使用iText生成并正确地对PDF格式进行数字签名。

当我再次尝试签名(两次或三次)时,pdf表示先前的修改版本已被修改且第一个签名无效,但我刚刚在其他时间签名而没有触及任何其他内容。< / p>

这是我的代码:

public void signPdf(String SRC, String DEST, String SIGN_IMAGE)
        throws IOException, DocumentException, GeneralSecurityException {

    // Gets the informations stored in the properties file
    String path = properties.getProperty("PRIVATE");
    String keystore_password = properties.getProperty("PASSWORD");
    String key_password = properties.getProperty("PASSWORD");

    // Create the keystore
    KeyStore ks = KeyStore.getInstance("pkcs12", "BC");
    ks.load(new FileInputStream(path), keystore_password.toCharArray());
    String alias = (String) ks.aliases().nextElement();
    PrivateKey pk = (PrivateKey) ks.getKey(alias,
            key_password.toCharArray());
    Certificate[] chain = ks.getCertificateChain(alias);

    // reader and stamper
    PdfReader reader = new PdfReader(SRC);
    FileOutputStream os = new FileOutputStream(DEST);

    // Get all the signatures if existing
    AcroFields acroFields = reader.getAcroFields();
    List<String> signatureNames = acroFields.getSignatureNames();
    PdfStamper stamper;

    // Choose to append or not the signature        
    if(signatureNames.isEmpty()){
        stamper = PdfStamper.createSignature(reader, os, '\0');
    } else{
        stamper = PdfStamper.createSignature(reader, os, '\0', null,
                true);
    }

    String sName = "sign_" + (signatureNames.size());

    // appearance
    PdfSignatureAppearance appearance = stamper.getSignatureAppearance();
    appearance.setVisibleSignature(new Rectangle(x_bl, y_bl, x_tr, y_tr),
            page, sName);
    appearance
            .setRenderingMode(PdfSignatureAppearance.RenderingMode.GRAPHIC);
    appearance.setSignatureGraphic(Image.getInstance(SIGN_IMAGE));
    // digital signature
    ExternalSignature es = new PrivateKeySignature(pk, "SHA-256", "BC");
    ExternalDigest digest = new BouncyCastleDigest();
    MakeSignature.signDetached(appearance, digest, es, chain, null, null,
            null, 0, CryptoStandard.CMS);
}

我没有得到什么是真正的错误,因为当我以前只用它in this example分别签名两次它曾经工作。我做错了什么?

0 个答案:

没有答案