如何使用令牌RFC3161签署PDF使用itext和BouncyCastle?
提前致谢!!
String token = "my jeton ";
ASN1InputStream in = new ASN1InputStream(Base64.getDecoder().decode(token));
ASN1Sequence seq = (ASN1Sequence)in.readObject();
将令牌转换为TimeStampToken
TimeStampToken stp = new TimeStampToken(new ContentInfo((ASN1Sequence) seq.getObjectAt(seq.getObjectAt(0) instanceof DERTaggedObject ? 3 : 1)));
CollectionStore store = (CollectionStore) stp.getCertificates();
Iterator itCert = store.iterator();
JcaX509CertificateConverter jcaConvertor = new JcaX509CertificateConverter();
X509Certificate[] cert = new X509Certificate[1] ;
while(itCert.hasNext()){
X509CertificateHolder certH = (X509CertificateHolder)itCert.next();
cert[0] = jcaConvertor.getCertificate((certH));
System.err.println(cert);
}
要签名的文件
String SRC = "original.pdf";
String DEST = "signed.pdf";
BouncyCastleProvider provider = new BouncyCastleProvider();
Security.addProvider(provider);
生成私钥
KeyStore ks = KeyStore.getInstance("pkcs12", provider.getName());
KeyPairGenerator generator = KeyPairGenerator.getInstance("RSA", "BC");
Base64Encoder b64 = new Base64Encoder();
generator.initialize(1024);
KeyPair pair = generator.generateKeyPair();
PrivateKey pk = (PrivateKey)pair.getPrivate();
使用令牌
的迭代签名方法sign(SRC, DEST, cert, pk, DigestAlgorithms.SHA1, provider.getName(), CryptoStandard.CMS, "", "", null, null, null, 0);