6
votes

I upgraded from iText 5.2.1 to iText 5.5.5

I was using PdfStamper along with PdfSignatureApperance to apply digital signatures.

Here is my snippet of code.

PdfStamper stamper = PdfStamper.createSignature(reader, byteArrayOutputStream,'\0');
PdfSignatureAppearance appearance = stamper.getSignatureAppearance();
appearance.setCrypto(key, chain, null, PdfSignatureAppearance.WINCER_SIGNED);

From the above code, there are couple of things which are not present in the current version.

1) In version 5.5.5 of iText I can't find "setCrypto" method of PdfSignatureApperance class.

2) PdfSignatureApperance doesn't consist of WINCER_SIGNED.

If anyone can help me with applying digital signatures with new alternatives.

Thanks.

1
In the course of the 5.3.x versions iText signing has been fundamentally improved. You might want to download Digital Signatures for PDF documents, A White Paper by Bruno Lowagie (iText Software) on creating and verifying digital PDF signatures using iText to catch up.mkl
Thanks. I will have a look.Javish

1 Answers

2
votes

In fact since iText 5.3 the signature API has a bit changed and the setCrypto method is not present anymore. I read the white paper of Bruno and here is the code that is working for me:

PdfReader reader = new PdfReader(new FileInputStream(file));
FileOutputStream fout = new FileOutputStream(new File(targetDir, fileName));
PdfStamper stp = PdfStamper.createSignature(reader, fout, '\0');

PdfSignatureAppearance sap = stp.getSignatureAppearance();
sap.setReason(reason);
sap.setLocation("Pleber-Christ");

ExternalDigest digest = new BouncyCastleDigest();
BouncyCastleProvider provider = new BouncyCastleProvider();
ExternalSignature signature = new PrivateKeySignature(key, DigestAlgorithms.SHA256, provider.getName());
MakeSignature.signDetached(sap, digest, signature, chain,   null, null, null, 0, CryptoStandard.CMS);

You have to put bcprov-jdk15on-1.60.jar and bcpkix-jdk15on-1.60.jar in your classpath. You may have to change it a bit to fit your needs.