I get Error "The document has been altered or corrupted since the signature was applied" when signature was applied to pdf using itext.
Digitally signed pdf is generated but the green check mark is not coming. What has to be done to get that green check mark.
Right now it says signature is INVALID.
I used following link for reference http://itextpdf.com/examples/iia.php?id=222
I use following code to apply signature using iText.
String path = "resources/examplestore";
String keystore_password = "password";
String key_password = "password";
String alias = "signFiles";
KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());
ks.load(new FileInputStream(path), keystore_password.toCharArray());
PrivateKey pk = (PrivateKey) ks.getKey(alias, key_password.toCharArray());
Certificate[] chain = ks.getCertificateChain(alias);
String path = "resources/examplestore";
String keystore_password = "password";
String key_password = "password";
String alias = "signFiles";
KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());
ks.load(new FileInputStream(path), keystore_password.toCharArray());
PrivateKey pk = (PrivateKey) ks.getKey(alias, key_password.toCharArray());
Certificate[] chain = ks.getCertificateChain(alias);
PdfReader reader = new PdfReader(src);
FileOutputStream os = new FileOutputStream(dest);
PdfStamper stamper = PdfStamper.createSignature(reader, os, '\0', null, true);
PdfSignatureAppearance appearance = stamper
.getSignatureAppearance();
appearance.setReason("I'm approving this.");
appearance.setLocation("Foobar");
appearance.setVisibleSignature(new Rectangle(160, 732, 232, 780), 1, "second");
ExternalSignature es = new PrivateKeySignature(pk, "SHA-256", "BC");
ExternalDigest digest = new BouncyCastleDigest();
MakeSignature.signDetached(appearance, digest, es, chain, null, null, null, 0, CryptoStandard.CMS);
I also Verify the code after this.
PdfStamper.createSignature(reader, os, '\0', null, true);
creates a signature in append mode but your sample PDF has only one revision. Furthermore your code creates a signature field "second" while the PDF contains a signature field "first". – mkl