Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Digital signatures using itext 5.5.5

Tags:

pdf

itext

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.

like image 894
Javish Avatar asked Feb 26 '15 22:02

Javish


1 Answers

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.

like image 141
Orden Avatar answered Oct 11 '22 15:10

Orden