我使用 iText 生成并正确数字签名 pdf。
当我尝试再次签名(两到三次)时,pdf 说之前的修订版已被修改,第一个签名无效,但我只是在另一次签名,没有触及任何其他内容。
这是我的代码:
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);
}
我没有得到真正的错误,因为当我曾经像在这个例子中那样单独签署两次时,它曾经工作过。我究竟做错了什么?