Maybe I have mistaken, but I think you should change the following line:
dic2.put(PdfName.CONTENTS, new PdfString(out).setHexWriting(false));
to
dic2.put(PdfName.CONTENTS, new PdfString(out).setHexWriting(true));

Of course your 'out' pkcs7 data should be binary data in this case.

Adobe expects to a hex string, but you put a normal string into the
/Contents field.

Explanation from the pdf reference:
"String objects can be written in two ways:
• As a sequence of literal characters enclosed in parentheses ( ); see "Literal
Strings,"
• As hexadecimal data enclosed in angle brackets < >; see "Hexadecimal Strings""

"The signature value. When ByteRange is present, the value is a
hexadecimal string (see "Hexadecimal Strings") representing the
value of the byte range digest."

Best Regards,
Tamas

2005/10/27, Shivakumar B <[EMAIL PROTECTED]>:
>
> Hi,
>
>         i am putting the code that i have executed below,
>
>                         try {
>                         log("reading the input...");
>                         PdfReader reader = new PdfReader(args[0]);
>                         FileOutputStream fout = new
> FileOutputStream(args[1]);
>                         PdfStamper stp = PdfStamper.createSignature(reader,
> fout, '\0');
>                         Calendar now = Calendar.getInstance();
>                         PdfSignatureAppearance sap =
> stp.getSignatureAppearance();
>                         log("creating visible signature ...");
>                         sap.setVisibleSignature(new Rectangle(100, 100, 200,
> 200), 1, null);
>                         sap.setLayer2Text("This is some custom made
> text.\n\nDate: some date");
>                         log("creating the pdf dictionary to set the
> flags...");
>                         PdfDictionary dic = new PdfDictionary();
>                         dic.put(PdfName.FT, PdfName.SIG);
>                         dic.put(PdfName.FILTER, new PdfName("SAFE.PPKSF"));
>                         dic.put(PdfName.SUBFILTER, new
> PdfName("adbe.pkcs7.detached"));
>                         dic.put(PdfName.M, new PdfDate(now));
>                         log("setting the dictionary to the appearance...");
>                         sap.setCryptoDictionary(dic);
>                         HashMap exc = new HashMap();
>                         int file_len = reader.getFileLength();
>                         exc.put(PdfName.CONTENTS, new Integer(file_len));
>                         log("preclosing the signature appearance");
>                         sap.preClose(exc);
>                         log("creating another dictionary object...");
>                         PdfDictionary dic2 = new PdfDictionary();
>                         byte sg[] = readFile("signature_pdf.p7s");
>                         byte out[] = new byte[(file_len-2) / 2];
>                         System.arraycopy(sg, 0, out, 0, sg.length);
>                         dic2.put(PdfName.CONTENTS, new
> PdfString(out).setHexWriting(false));
>                         sap.close(dic2);
>                         log("complete");
>                 } catch (Exception e) {
>                         log("exception is ..."+e);
>                 }
> regards
> Sivakumar.B
>
>
>
> [EMAIL PROTECTED]
>
> 10/26/2005 07:28 PM
> ToShivakumar B/SP/DBAsia/[EMAIL PROTECTED]
> cc
> [email protected]
> SubjectRE: [iText-questions] Reg: Pdf Signature Clarifications
>
>
>
>
>
>
>
> Signatures require knowledge and if you don't understand what you are
> doing you won't get far. Post a snippet of your code and I'll see if I
> can correct it.
>
> > -----Original Message-----
> > From: [EMAIL PROTECTED]
> > [mailto:[EMAIL PROTECTED] On
> > Behalf Of Shivakumar B
> > Sent: Wednesday, October 26, 2005 12:04 PM
> > To: Paulo Soares
> > Cc: [email protected]
> > Subject: RE: [iText-questions] Reg: Pdf Signature Clarifications
> >
> >
> > Hi Paulo,
> >         thankx for the tip, i've corrected the error & gave
> > the exclusion range as the length of the original pdf file.
> > Is that correct?
> >         While putting the signature into the pdf, i gave the
> > following command.
> >
> >                 dic2.put(PdfName.CONTENTS, new
> > PdfString(out).setHexWriting(false));
> >
> >         when i open in Adobe, i get the following error,
> >
> >         Error during signature verification.
> >
> >                 Signature contains incorrect, unrecognized,
> > corrupted or suspicious data.
> >                 Support Information: SigDict /Contents illegal data
> >
> >         Is this something wrong with the code or should I use
> > trusted certificates to sign the pdf and then verify?
> >
> >
> > regards
> > Sivakumar.B
> >
> >
> >
> >
> > [EMAIL PROTECTED]
> >
> > 10/26/2005 06:17 PM To
> > Shivakumar B/SP/DBAsia/[EMAIL PROTECTED],
> > [email protected]
> > cc
> > Subject
> > RE: [iText-questions] Reg: Pdf Signature Clarifications
> >
> >
> >
> >
> >
> >
> > Several errors:
> >
> > - you can only sign a field at a time. The second field must be signed
> > in append mode.
> >
> > - the content "out" must have the size of the (exclusion - 2) / 2
> >
> > > -----Original Message-----
> > > From: [EMAIL PROTECTED]
> > > [mailto:[EMAIL PROTECTED] On
> > > Behalf Of Shivakumar B
> > > Sent: Wednesday, October 26, 2005 8:13 AM
> > > To: [email protected]
> > > Subject: [iText-questions] Reg: Pdf Signature Clarifications
> > >
> > >
> > > Hi,
> > >         I am trying to create a Blank Signature Field for an
> > > existing pdf, later, I will fill it up with the actual
> > > signature. The following are the steps done,
> > >
> > >         1. Used PdfStamper to read the existing pdf.
> > >         2. Used PdfFormField to create the Blank Signature
> > > Field. (Set the flags, added the annotation to the
> > > PdfStamper, closed the PdfStamper)
> > >         3. Signatures were generated outside.
> > >         4. Putting back the signatures to the pdf.  This step
> > > is not quite clear to me.
> > >              These steps were followed as given in the
> > > "howtosign.html" page with little modification.
> > >
> > >                 a) I read the pdf using the PdfReader,
> > > created the PdfStamper using the createSignature() method.
> > >                 b) Get the List of Acro Fields from the
> > > PdfStamper and get the Blank Signature Field Names.
> > >                 c) Created PdfSignatureAppearance from the
> > > PdfStamper.
> > >                 d) Created the PdfDictionary with the fields
> > > given in the sample
> > >                         (Is there any page which documents
> > > the constants and what we have to use for which purpose...)
> > >                                                 (How are we
> > > allocating the space for the contents?)
> > >                 e) Created a HashMap and called the
> > > preClose()..The Code throws NullPointer Exception here.
> > >                 f)  Created another dictionary object, to
> > > which the signatures were added.
> > >                                          (How do we calculate
> > > the byte space for the contents?)
> > >
> > >
> > >         for your reference, the actual code is below,
> > >
> > >                 PdfStamper stp =
> > > PdfStamper.createSignature(reader, fout, '\0');
> > >                 AcroFields af = stp.getAcroFields();
> > >                 HashMap af_fields = af.getFields();
> > >                 Iterator iter = af_fields.keySet().iterator();
> > >                 Object key1 = null;
> > >                 while (iter.hasNext()) {
> > >                         key1 = iter.next();
> > >                         log("Acro Fields Key : "+key1+" Value
> > > : "+af_fields.get(key1));
> > >                 }
> > >                 ArrayList names = af.getBlankSignatureNames();
> > >
> > >                 for (int k = 0; k < names.size(); ++k) {
> > >                         String name = (String)names.get(k);
> > >                         log("Signature name: " + name);
> > >                         PdfSignatureAppearance sap =
> > > stp.getSignatureAppearance();
> > >                         log("creating the visible signature
> > > with name...");
> > >                         sap.setVisibleSignature(new
> > > Rectangle(100,100,200,200), 1, name);
> > >                         log("creating the pdf dictionary to
> > > set the flags...");
> > >                         PdfDictionary dic = new PdfDictionary();
> > >                         dic.put(PdfName.FT, PdfName.SIG);
> > >                         dic.put(PdfName.FILTER, new
> > > PdfName("SAFE.PPKSF"));
> > >                         dic.put(PdfName.SUBFILTER, new
> > > PdfName("adbe.pkcs7.detached"));
> > >                         dic.put(PdfName.NAME, new
> > > PdfString(PdfPKCS7.getSubjectFields((X509Certificate)
> > > chain[0]).getField("CN")));
> > >                         log("setting the dictionary to the
> > > appearance...");
> > >                         sap.setCryptoDictionary(dic);
> > >                         HashMap exc = new HashMap();
> > >                         exc.put(PdfName.CONTENTS, new
> > > Integer(0x2502));
> > >                         log("preclosing the appearance");
> > >                         sap.preClose(exc);
> > >                         PdfDictionary dic2 = new PdfDictionary();
> > >                         byte[] out = readFile("signature_pdf.p7s");
> > >                         dic2.put(PdfName.CONTENTS, new
> > > PdfString(out).setHexWriting(true));
> > >                         sap.close(dic2);
> > >                 }
> > >                 stp.close();
> > >
> > >         any suggestion or help..
> > >
> > >
> > > regards
> > > Sivakumar.B
> > >
> > >
> >
> >
> >
> >
>
>
>

Reply via email to