I use code bellow to TSA sign pdf. After opening in Acrobat reader
i've got message of invalid signature like this:
- There are errors in the formatting or information contained in this
signature (support information: SigDict? / Contents illegal data)
Could anyone help me , where is problem ?
Security.addProvider(new
org.bouncycastle.jce.provider.BouncyCastleProvider());
KeyPairGenerator gen = KeyPairGenerator.getInstance("RSA");
gen.initialize(2048);
KeyPair kp = gen.generateKeyPair();
X500Principal p = new X500Principal ( "CN=Pavol Behul" );
X509Certificate cert = Signer.selfSign(p, new
Date(System.currentTimeMillis() - 100000000), new
Date(System.currentTimeMillis() + 100000000), kp);
PrivateKey key = kp.getPrivate();
java.security.cert.Certificate[] chain = {cert};
// input pdf for signing, outpu signed pdf
PdfReader reader = new PdfReader("input.pdf");
FileOutputStream fout = new FileOutputStream("signed.pdf");
PdfStamper stp = PdfStamper.createSignature(reader, fout, '\0');
PdfSignatureAppearance sap = stp.getSignatureAppearance();
sap.setCrypto(key, chain, null, PdfSignatureAppearance.SELF_SIGNED);
sap.setReason("Test purpose");
sap.setLocation("Praha");
sap.setVisibleSignature(new Rectangle(100, 100, 300, 200), 1, null);
sap.setExternalDigest(new byte[2048], null, "RSA");
Calendar cal = Calendar.getInstance();
int contentEstimated = 1226;
HashMap exc = new HashMap();
exc.put(PdfName.CONTENTS, new Integer(contentEstimated * 2 + 2));
sap.preClose(exc);
PdfPKCS7 sgn = new PdfPKCS7(key, chain, null, "SHA1", null, false);
InputStream data = sap.getRangeStream();
MessageDigest messageDigest = MessageDigest.getInstance("SHA1");
byte buf[] = new byte[8192];
int n;
while ((n = data.read(buf)) > 0) {
messageDigest.update(buf, 0, n);
}
byte hash[] = messageDigest.digest();
n = hash.length;
//byte[] ocsp = null;
//if (chain.length >= 2) {
// String url =
PdfPKCS7.getOCSPURL((X509Certificate)chain[0]);
// if (url != null && url.length() > 0)
// ocsp = new
OcspClientBouncyCastle((X509Certificate)chain[0],
(X509Certificate)chain[1],url).getEncoded();
byte sh[] = sgn.getAuthenticatedAttributeBytes(hash, cal, null);
sgn.update(sh, 0, sh.length);
byte[] encodedSig = sgn.getEncodedPKCS7(hash, cal, null, null);
n = encodedSig.length;
if (contentEstimated + 2 < encodedSig.length) throw new
Exception("Not enough space");
byte[] paddedSig = new byte[contentEstimated];
System.arraycopy(encodedSig, 0, paddedSig, 0, encodedSig.length);
PdfDictionary dic2 = new PdfDictionary();
dic2.put(PdfName.CONTENTS, new
PdfString(paddedSig).setHexWriting(true));
sap.close(dic2);
------------------------------------------------------------------------------
Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day
trial. Simplify your report design, integration and deployment - and focus on
what you do best, core application coding. Discover what's new with
Crystal Reports now. http://p.sf.net/sfu/bobj-july
_______________________________________________
iText-questions mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/itext-questions
Buy the iText book: http://www.1t3xt.com/docs/book.php
Check the site with examples before you ask questions:
http://www.1t3xt.info/examples/
You can also search the keywords list: http://1t3xt.info/tutorials/keywords/