我想用Java将数字签名附加到PDF文件,然后用可信的时间戳权威机构对该文件进行时间戳。
我该怎么做?
将带有私钥的数字证书导出到pfx文件。
将iText与BouncyCastle一起使用:
Document document = new Document();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PdfWriter.getInstance(document, baos);
document.open();
document.add(new Paragraph("Hello World!"));
document.close();
PdfReader reader = new PdfReader(baos.toByteArray());
OutputStream os = new FileOutputStream("c:\temp\sign\test.pdf");
PdfStamper stamper = PdfStamper.createSignature(reader, os, ' ');
// Creating the appearance
PdfSignatureAppearance appearance = stamper.getSignatureAppearance();
appearance.setReason("REASON");
appearance.setLocation("LOCATION");
appearance.setVisibleSignature(new Rectangle(36, 748, 144, 780), 1, "sig");
Security.addProvider(new BouncyCastleProvider());
FileInputStream fis = new FileInputStream("c:\ssl\test.pfx");
String password = "myPassword";
KeyStore ks = KeyStore.getInstance("pkcs12");
ks.load(fis, password.toCharArray());
String alias = ks.aliases().nextElement();
PrivateKey pk = (PrivateKey) ks.getKey(alias, password.toCharArray());
X509Certificate cert = (X509Certificate) ks.getCertificate(alias);
TSAClient tsc = new TSAClientBouncyCastle("http://timestampserverURL/");
ExternalDigest digest = new BouncyCastleDigest();
ExternalSignature signature = new PrivateKeySignature(pk, "SHA-1", "BC");
MakeSignature.signDetached(appearance, digest, signature, new Certificate[] { cert }, null, null, tsc, 0,
CryptoStandard.CMS);
Maven依赖项:
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcprov-jdk15on</artifactId>
<version>1.49</version>
</dependency>
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcmail-jdk15on</artifactId>
<version>1.49</version>
</dependency>
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bctsp-jdk15on</artifactId>
<version>1.46</version>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.4.2</version>
</dependency>
您可以通过其HTTP RESTful接口使用Securo API。
应用的时间戳采用RFC3161分离格式,所有时间戳均由欧盟认可的合格认证机构颁发。
免责声明:securo的联合创始人
DigiStamp在SecureTime API工具包中提供PDF签名和时间戳功能,当您创建免费测试帐户时,您可以获得该功能的链接(并访问测试服务器)。该工具包使用BouncyCastle和旧的免费iText版本。
Qoppa有一个更新的工具包,包含一系列PDF功能,但使用它需要付费。
免责声明:我在DigiStamp