יצירת מסמך חתום דיגיטלית
-
שלום לכולם!
אני רוצה להפיק קבלות חתומות דיגיטליות,
ראיתי שאפשר באמצעות הספריה הזאת: Itext לחתום על קבצי PDF באופן דיגיטלי.השאלה שלי היא איך אני יוצר תעודה בשביל חתימה דיגיטלית והאם זה מסובך?
האם בשביל לחתום על קבלה צריך לקנות תעודה, או שמספיק ליצור בעצמי?תודה רבה!
אברהם -
@avr416 אמר ביצירת מסמך חתום דיגיטלית:
שלום לכולם!
אני רוצה להפיק קבלות חתומות דיגיטליות,
ראיתי שאפשר באמצעות הספריה הזאת: Itext לחתום על קבצי PDF באופן דיגיטלי.השאלה שלי היא איך אני יוצר תעודה בשביל חתימה דיגיטלית והאם זה מסובך?
האם בשביל לחתום על קבלה צריך לקנות תעודה, או שמספיק ליצור בעצמי?תודה רבה!
אברהםתוכל ליצור בעצמך.
-
תודה רבה!
הנה התוצאה, מחלקה הכוללת מתודה ליצירת תעודה דיגיטלית, ומתודה לחתימת מסמך PDF ע"י שימוש בספרייה ITEXTSHARP:
אני לא יודע איך אפשר להציג כאן קוד בצורה נורמלית.. אז צירפתי גם בקובץ.
0_1534451710703_Untitled.csusing System; using System.IO; using System.Threading.Tasks; using System.Web.Mvc; using System.Configuration; using System.Security.Cryptography.X509Certificates; using CERTENROLLLib; using iTextSharp.text.pdf; using iTextSharp.text.pdf.security; public static class SignPdf { public static MemoryStream SignPdf(MemoryStream streamPdf) { //get certificate from path X509Certificate2 cert1 = new X509Certificate2(@"PATH_TO_CERT_.pfx", "PASSWORD", X509KeyStorageFlags.Exportable); //get private key to sign pdf var pk = Org.BouncyCastle.Security.DotNetUtilities.GetKeyPair(cert1.PrivateKey).Private; // convert the type to be used at .SetCrypt(); Org.BouncyCastle.X509.X509Certificate bcCert = Org.BouncyCastle.Security.DotNetUtilities.FromX509Certificate(cert1); try { MemoryStream output = new MemoryStream(); // get the pdf u want to sign PdfReader pdfReader = new PdfReader(streamPdf); PdfStamper stamper = PdfStamper.CreateSignature(pdfReader, output, '\0'); PdfSignatureAppearance signatureAppearance = stamper.SignatureAppearance; signatureAppearance.SignatureRenderingMode = PdfSignatureAppearance.RenderingMode.GRAPHIC_AND_DESCRIPTION; signatureAppearance.Reason = "מסמך זה חתום דיגיטלית"; signatureAppearance.LocationCaption = signatureAppearance.Location; signatureAppearance.SignatureRenderingMode = PdfSignatureAppearance.RenderingMode.DESCRIPTION; //signatureAppearance.SignatureGraphic = Image.GetInstance(this.imageFolderPath + "sign.png"); var certParser = new Org.BouncyCastle.X509.X509CertificateParser(); var chain = new Org.BouncyCastle.X509.X509Certificate[] { certParser.ReadCertificate(cert1.RawData) }; IExternalSignature signature = new PrivateKeySignature(pk, "SHA-256"); MakeSignature.SignDetached(signatureAppearance, signature, chain, null, null, null, 0, CryptoStandard.CMS); stamper.Close(); return output; } catch (Exception ex) { throw ex; } } //generate certificate public static X509Certificate2 CreateSelfSignedCertificate(string subjectName) { // create DN for subject and issuer var dn = new CX500DistinguishedName(); dn.Encode("CN=" + subjectName, X500NameFlags.XCN_CERT_NAME_STR_NONE); // create a new private key for the certificate CX509PrivateKey privateKey = new CX509PrivateKey(); privateKey.ProviderName = "Microsoft Base Cryptographic Provider v1.0"; privateKey.MachineContext = true; privateKey.Length = 2048; privateKey.KeySpec = X509KeySpec.XCN_AT_SIGNATURE; // use is not limited privateKey.ExportPolicy = X509PrivateKeyExportFlags.XCN_NCRYPT_ALLOW_PLAINTEXT_EXPORT_FLAG; privateKey.Create(); // Use the stronger SHA512 hashing algorithm var hashobj = new CObjectId(); hashobj.InitializeFromAlgorithmName(ObjectIdGroupId.XCN_CRYPT_HASH_ALG_OID_GROUP_ID, ObjectIdPublicKeyFlags.XCN_CRYPT_OID_INFO_PUBKEY_ANY, AlgorithmFlags.AlgorithmFlagsNone, "SHA512"); // add extended key usage if you want - look at MSDN for a list of possible OIDs var oid = new CObjectId(); oid.InitializeFromValue("1.3.6.1.5.5.7.3.1"); // SSL server var oidlist = new CObjectIds(); oidlist.Add(oid); var eku = new CX509ExtensionEnhancedKeyUsage(); eku.InitializeEncode(oidlist); // Create the self signing request var cert = new CX509CertificateRequestCertificate(); cert.InitializeFromPrivateKey(X509CertificateEnrollmentContext.ContextMachine, privateKey, ""); cert.Subject = dn; cert.Issuer = dn; // the issuer and the subject are the same cert.NotBefore = DateTime.Now; // this cert expires immediately. Change to whatever makes sense for you cert.NotAfter = DateTime.Now.AddYears(10); cert.X509Extensions.Add((CX509Extension)eku); // add the EKU cert.HashAlgorithm = hashobj; // Specify the hashing algorithm cert.Encode(); // encode the certificate //// Do the final enrollment process var enroll = new CX509Enrollment(); enroll.InitializeFromRequest(cert); // load the certificate enroll.CertificateFriendlyName = subjectName; // Optional: add a friendly name string csr = enroll.CreateRequest(); // Output the request in base64 // and install it back as the response enroll.InstallResponse(InstallResponseRestrictionFlags.AllowUntrustedCertificate, csr, EncodingType.XCN_CRYPT_STRING_BASE64, ""); // no password // output a base64 encoded PKCS#12 so we can import it back to the .Net security classes var base64encoded = enroll.CreatePFX("", // no password, this is for internal consumption PFXExportOptions.PFXExportChainWithRoot); // instantiate the target class with the PKCS#12 data (and the empty password) return new System.Security.Cryptography.X509Certificates.X509Certificate2( System.Convert.FromBase64String(base64encoded), "", // mark the private key as exportable (this is usually what you want to do) System.Security.Cryptography.X509Certificates.X509KeyStorageFlags.Exportable ); } }
-
-
@golanart
תודה! ערכתי.