Sigining PDF use Token RFC 3161 with itext

264 Views Asked by At

How to signed PDF with token RFC3161 use itext and BouncyCastle ?

Thanks in advance!!

String token = "my jeton ";
ASN1InputStream in  = new ASN1InputStream(Base64.getDecoder().decode(token));
ASN1Sequence seq = (ASN1Sequence)in.readObject();

convert token to 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);
}

file to sign

String SRC = "original.pdf";
String DEST  = "signed.pdf";
BouncyCastleProvider provider = new BouncyCastleProvider();
Security.addProvider(provider);

generate private key

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

itext signing method with token

sign(SRC, DEST, cert, pk, DigestAlgorithms.SHA1, provider.getName(), CryptoStandard.CMS, "", "", null, null, null, 0);
0

There are 0 best solutions below