使用itext 5.5.5的数字签名 [英] Digital signatures using itext 5.5.5

查看:80
本文介绍了使用itext 5.5.5的数字签名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从iText 5.2.1 升级到了iText 5.5.5

I upgraded from iText 5.2.1 to iText 5.5.5

我当时在使用PdfStamper和PdfSignatureApperance来应用数字签名.

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

这是我的代码段.

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)在iText的 5.5.5 版本中,我找不到PdfSignatureApperance类的"setCrypto"方法.

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

2)PdfSignatureApperance不包含WINCER_SIGNED.

2) PdfSignatureApperance doesn't consist of WINCER_SIGNED.

如果有人可以帮助我使用新的替代方法来应用数字签名.

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

谢谢.

推荐答案

事实上,自iText 5.3起,签名API有所更改,并且setCrypto方法不再存在.我阅读了Bruno的白皮书,这是对我有用的代码:

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);

您必须将 bcprov-jdk15on-1.60.jar bcpkix-jdk15on-1.60.jar 放入您的类路径中.您可能需要对其进行一些更改以适应您的需求.

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.

这篇关于使用itext 5.5.5的数字签名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆