hi michael, same error file corrupted please help STEP1: creating the hash of the pdf file in the server protected void Button1_Click(object sender, EventArgs e) { string _gstrFilePath = Server.MapPath("~/NewFolder1/TRANSFER_[PROVISIONAL]_29_05_2014.pdf"); SHA1Managed sha1 = new SHA1Managed(); UnicodeEncoding encoding = new UnicodeEncoding(); byte[] data1 = File.ReadAllBytes(_gstrFilePath); byte[] hash1 = sha1.ComputeHash(data1); hdnSignatureHash.Text = Convert.ToBase64String(hash1); }
STEP2: getting the certificate and signed hash in the client side javascript: function fnGetCertificate() { var obj = new ActiveXObject('PDFSIGNATURE.PDFSIG'); var cer = obj.PdfSignature("1A87CCE901002C24"); document.getElementById("FeaturedContent_hdnCertificate").value = cer; } function fnGetSignature() { var obj = new ActiveXObject('PDFSIGNATURE.PDFSIG'); var signedhash = obj.PdfSignedHash("1A87CCE901002C24", document.getElementById("FeaturedContent_hdnSignatureHash").value); document.getElementById("FeaturedContent_hdnSignature").value = signedhash; } .net dll in the client side public string PdfSignature(string SerialNumber) { bool Success = false; byte[] extCert = null; String strReturn = string.Empty; try { System.Security.Cryptography.X509Certificates.X509Store store = new System.Security.Cryptography.X509Certificates.X509Store("MY", System.Security.Cryptography.X509Certificates.StoreLocation.CurrentUser); store.Open(System.Security.Cryptography.X509Certificates.OpenFlags.ReadOnly | System.Security.Cryptography.X509Certificates.OpenFlags.OpenExistingOnly); //System.Security.Cryptography.X509Certificates.X509Certificate2Collection sel = System.Security.Cryptography.X509Certificates.X509Certificate2UI.SelectFromCollection(store.Certificates, null, null, System.Security.Cryptography.X509Certificates.X509SelectionFlag.SingleSelection); System.Security.Cryptography.X509Certificates.X509Certificate2 cert = store.Certificates.Find(System.Security.Cryptography.X509Certificates.X509FindType.FindBySerialNumber, SerialNumber, false)[0]; //System.Security.Cryptography.X509Certificates.X509Certificate2 cert = store.Certificates[0]; Org.BouncyCastle.X509.X509CertificateParser cp = new Org.BouncyCastle.X509.X509CertificateParser(); Org.BouncyCastle.X509.X509Certificate[] chain = new Org.BouncyCastle.X509.X509Certificate[] { cp.ReadCertificate(cert.RawData)}; return Convert.ToBase64String(cert.Export(System.Security.Cryptography.X509Certificates.X509ContentType.Cert, "PASSWORD")); } catch (Exception ex) { strReturn = ex.Message; return strReturn; } } [ComVisible(true)] public string PdfSignedHash(string SerialNumber,string hash) { bool Success = false; byte[] signedhash = null; String strReturn = string.Empty; try { System.Security.Cryptography.X509Certificates.X509Store store = new System.Security.Cryptography.X509Certificates.X509Store("MY", System.Security.Cryptography.X509Certificates.StoreLocation.CurrentUser); store.Open(System.Security.Cryptography.X509Certificates.OpenFlags.ReadOnly | System.Security.Cryptography.X509Certificates.OpenFlags.OpenExistingOnly); //System.Security.Cryptography.X509Certificates.X509Certificate2Collection sel = System.Security.Cryptography.X509Certificates.X509Certificate2UI.SelectFromCollection(store.Certificates, null, null, System.Security.Cryptography.X509Certificates.X509SelectionFlag.SingleSelection); System.Security.Cryptography.X509Certificates.X509Certificate2 ocert = store.Certificates.Find(System.Security.Cryptography.X509Certificates.X509FindType.FindBySerialNumber, SerialNumber, false)[0]; System.Security.Cryptography.RSACryptoServiceProvider privateKey = (System.Security.Cryptography.RSACryptoServiceProvider)ocert.PrivateKey; signedhash = privateKey.SignHash(Convert.FromBase64String(hash), "SHA1"); return Convert.ToBase64String(signedhash); } catch (Exception ex) { strReturn = ex.Message; return strReturn; } } STEP 3: sign pdf by itextsharp in the server protected void Button2_Click(object sender, EventArgs e) { string _gstrFilePath = Server.MapPath("~/NewFolder1/TRANSFER_[PROVISIONAL]_29_05_2014.pdf"); System.Security.Cryptography.X509Certificates.X509Certificate2 oCert = new System.Security.Cryptography.X509Certificates.X509Certificate2(Convert.FromBase64String(hdnCertificate.Text), "PASSWORD"); Org.BouncyCastle.X509.X509CertificateParser cp = new Org.BouncyCastle.X509.X509CertificateParser(); Org.BouncyCastle.X509.X509Certificate[] chain = new Org.BouncyCastle.X509.X509Certificate[] { cp.ReadCertificate(oCert.RawData)}; /*start verification*/ RSACryptoServiceProvider csp = (RSACryptoServiceProvider)oCert.PublicKey.Key; // Hash the data SHA1Managed sha1 = new SHA1Managed(); UnicodeEncoding encoding = new UnicodeEncoding(); byte[] data1 = File.ReadAllBytes(_gstrFilePath); byte[] hash1 = sha1.ComputeHash(data1); bool bln = csp.VerifyHash(hash1, CryptoConfig.MapNameToOID("SHA1"), Convert.FromBase64String(hdnSignature.Text)); /*end verification*/ ServerSignature externalSignature = new ServerSignature(); externalSignature.DigSign = Convert.FromBase64String(hdnSignature.Text); // reader and stamper byte[] OwnerPassword = System.Text.Encoding.ASCII.GetBytes("secret"); PdfReader reader = new PdfReader(_gstrFilePath, OwnerPassword); bool isencrypted = reader.IsEncrypted(); bool hasuserPassword = false; string OutputFilename = Server.MapPath("~/NewFolder1/TRANSFER_[PROVISIONAL]_29_05_20141.pdf"); using (FileStream fout = new FileStream(OutputFilename, FileMode.Create, FileAccess.ReadWrite)) { using (PdfStamper stamper = PdfStamper.CreateSignature(reader, fout, '\0')) { if (isencrypted) stamper.SetEncryption(PdfWriter.STRENGTH128BITS, null, "secret", PdfWriter.ALLOW_SCREENREADERS); if (hasuserPassword) stamper.SetEncryption(PdfWriter.STRENGTH128BITS, null, "secret", PdfWriter.ALLOW_SCREENREADERS); // appearance PdfSignatureAppearance appearance = stamper.SignatureAppearance; //appearance.Image = new iTextSharp.text.pdf.PdfImage(); appearance.Reason = "Reason"; appearance.Location = "Location"; appearance.SetVisibleSignature(new iTextSharp.text.Rectangle(36, 748, 244, 880), 2, "Secure-PDF"); //DateTime signatureDatetime = DateTime.Now; //appearance.SignDate = signatureDatetime; // digital signature MakeSignature.SignDetached(appearance, externalSignature, chain, null, null, null, 0, CryptoStandard.CMS); stamper.Close(); stamper.Dispose(); } } reader.Close(); reader.Dispose(); } serversignature class: class ServerSignature : iTextSharp.text.pdf.security.IExternalSignature { public byte[] DigSign { get; set; } string iTextSharp.text.pdf.security.IExternalSignature.GetEncryptionAlgorithm() { return "RSA"; } string iTextSharp.text.pdf.security.IExternalSignature.GetHashAlgorithm() { return iTextSharp.text.pdf.security.DigestAlgorithms.SHA1; } byte[] iTextSharp.text.pdf.security.IExternalSignature.Sign(byte[] message) { return DigSign; } } } signed pdf file TRANSFER_[PROVISIONAL]_29_05_20141.pdf <http://itext-general.2136553.n4.nabble.com/file/n4660339/TRANSFER_%5BPROVISIONAL%5D_29_05_20141.pdf> -- View this message in context: http://itext-general.2136553.n4.nabble.com/Sign-and-PDF-with-SmartCard-and-web-browser-only-tp4319344p4660339.html Sent from the iText - General mailing list archive at Nabble.com. ------------------------------------------------------------------------------ Slashdot TV. Video for Nerds. Stuff that matters. http://tv.slashdot.org/ _______________________________________________ iText-questions mailing list iText-questions@lists.sourceforge.net 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