|
Hello , I've to sign with an external sgnature. When i open de PDF and validate it, I get an error message that says something like document has been modified since sign was applied
The structure of code is: - calc the hash - send it to the external service - get the external signature - put it to de dic
Can you tell me what is wrong? or where to find the error?
thanks in advance, Josep Maria
The code: PdfReader reader = new PdfReader(IN_FILE); FileStream fout = new FileStream(OUT_FILE, FileMode.Create);
PdfStamper stp = PdfStamper.CreateSignature(reader, fout, '\0'); DateTime cal = DateTime.Now;
PdfSignatureAppearance sap; sap = stp.SignatureAppearance; sap.SetVisibleSignature(new Rectangle(100, 100, 300, 200), 1, "Signar");
// sap.Layer2Text = ("Prova de firma.\n\nDate: "); //sap.Layer2Text = (""); PdfSignature dic = new PdfSignature(PdfName.ADOBE_PPKMS, PdfName.ADBE_PKCS7_DETACHED); dic.Put(PdfName.TYPE, PdfName.SIG); dic.Put(PdfName.M, new PdfDate(cal));
sap.CryptoDictionary = (dic); // sap.Acro6Layers = (true); int contentEstimated = 15000; Dictionary<PdfName, int> exc = new Dictionary<PdfName, int>(); PdfName contingut = new PdfName(""); exc.Add(PdfName.CONTENTS, (contentEstimated * 2 + 2)); sap.PreClose(exc);
IDigest messageDigest = DigestUtilities.GetDigest("SHA1"); Stream data = sap.GetRangeStream();
// calc the hash byte[] buf = new byte[8192]; int n1; while ((n1 = data.Read(buf, 0, buf.Length)) > 0) { messageDigest.BlockUpdate(buf, 0, n1); }
byte[] hash = new byte[messageDigest.GetDigestSize()]; messageDigest.DoFinal(hash, 0); // write hash to sent it to the extenal sign util System.IO.File.WriteAllText("C:/Reg/hash_ToBase64.txt", Convert.ToBase64String(hash)); String shash = Convert.ToBase64String(hash); // extenal sign util process hash executeExe(shash); // get PKCS#7 String spk7 = System.IO.File.ReadAllText("C:/tmp/Envelope.txt"); byte[] bpk7 = Convert.FromBase64String(spk7);
// put the PKCS#7 to disc. PdfDictionary updates = new PdfDictionary(); byte[] out1 = new byte[contentEstimated]; System.Array.Copy(bpk7, 0, out1, 0, bpk7.Length); updates.Put(PdfName.CONTENTS, new PdfString(out1).SetHexWriting(true)); sap.Close(updates);
------------------------------------------------------------------------------ Live Security Virtual Conference Exclusive live event will cover all the ways today's security and threat landscape has changed and how IT managers can respond. Discussions will include endpoint security, mobile security and the latest in malware threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/ _______________________________________________ iText-questions mailing list [hidden email] https://lists.sourceforge.net/lists/listinfo/itext-questions iText(R) is a registered trademark of 1T3XT BVBA. Many questions posted to this list can (and will) be answered with a reference to the iText book: http://www.itextpdf.com/book/ Please check the keywords list before you ask for examples: http://itextpdf.com/themes/keywords.php |
|
Josep Maria,
Please also supply a sample document signed by your code. Regards, Michael |
| Powered by Nabble | Edit this page |
