Hi all,

I tried to use the following code, but some exception raised:


try {
            String configFile = "c:/esigntrust/config/pkcs11.cfg";

            SunPKCS11 pkcs11 = new SunPKCS11(configFile);
            Security.addProvider(pkcs11);

            char[] password = getPassword("Input", "Password");

            KeyStore ks = KeyStore.getInstance("PKCS11");
            ks.load(null, password);

            String alias = getAlias(ks, 1);
            // String alias = getSelQCAliasFromSM(configFile, Constant.PIN);

            X509Certificate cert = getX509CertFormSM(configFile, password,
                    alias);

            System.out.println(">>>" + cert.getSubjectDN().getName());


            PrivateKey privateKey = (PrivateKey) ks.getKey(alias, password);

            Certificate[] chain = ks.getCertificateChain(alias);
            System.out.println(chain[0].getPublicKey());

            PdfReader reader = new PdfReader("doc/test1.pdf");
            FileOutputStream fout = new
FileOutputStream("doc/test1_signed.pdf");
            PdfStamper stp = PdfStamper.createSignature(reader, fout, '\0');
            PdfSignatureAppearance sap = stp.getSignatureAppearance();
            sap.setCrypto(privateKey, chain, null,
PdfSignatureAppearance.SELF_SIGNED);
            sap.setReason("I'm the author");
            sap.setLocation("Lisbon");
            // comment next line to have an invisible signature
            sap.setVisibleSignature(new Rectangle(100, 100, 200, 200), 1,
null);
            sap.setExternalDigest(new byte[2048], null, "RSA");
            sap.preClose();
            PdfPKCS7 sig = sap.getSigStandard().getSigner();
            Signature sign = Signature.getInstance("SHA1withRSA");
            sign.initSign(privateKey);
            byte buf[] = new byte[8192];
            int n;
            java.io.InputStream inp = sap.getRangeStream();
            while ((n = inp.read(buf)) > 0) {
            sign.update(buf, 0, n);
            }
            sig.setExternalDigest(sign.sign(), null, "RSA");
            PdfDictionary dic = new PdfDictionary();
            dic.put(PdfName.CONTENTS, new
PdfString(sig.getEncodedPKCS1()).setHexWriting(true));
            sap.close(dic);



        } catch(Exception e) {
            e.printStackTrace();
        }



===========================================
java.lang.NullPointerException
    at java.util.Calendar.setTime(Calendar.java:1070)
    at
com.lowagie.text.pdf.PdfSignatureAppearance.getSignDate(PdfSignatureAppearance.java:841)
    at
com.lowagie.text.pdf.PdfSignatureAppearance.preClose(PdfSignatureAppearance.java:992)
    at
com.lowagie.text.pdf.PdfSignatureAppearance.preClose(PdfSignatureAppearance.java:920)
    at Sign.main(Sign.java:96)




Any suggestion?

Best regards,
Eric








On Tue, Sep 22, 2009 at 5:45 PM, Eric Chow <eric...@gmail.com> wrote:

> Hello,
>
> Any Java iText experts that have a simple example to sign PDF with external
> signature approach?
>
> Please give me a hand and show me a simple example.
>
> Since I am new in iText, I can't totally understand the example of
> iTextSharp. It is better to show me a Java code, thank very much.
>
> Best regards,
> Eric
>
>
------------------------------------------------------------------------------
Come build with us! The BlackBerry&reg; Developer Conference in SF, CA
is the only developer event you need to attend this year. Jumpstart your
developing skills, take BlackBerry mobile applications to market and stay 
ahead of the curve. Join us from November 9&#45;12, 2009. Register now&#33;
http://p.sf.net/sfu/devconf
_______________________________________________
iText-questions mailing list
iText-questions@lists.sourceforge.net
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/

Reply via email to