Digital signatures using itext 5.5.5

陌路散爱 提交于 2020-08-04 06:07:05

问题


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 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.



来源:https://stackoverflow.com/questions/28753918/digital-signatures-using-itext-5-5-5

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!