- c# – X509Certificate2 p12 is store required? – Stack Overflow
- X509Certificate2.Import Method (System.Security.Cryptography.X509Certificates) | Microsoft Docs
ดูข้อมูลของ certificate
using System.Reflection; using System.Security.Cryptography.X509Certificates; using System.Xml; namespace ConsoleApp6; class Program { public static readonly log4net.ILog log = log4net.LogManager.GetLogger(typeof(Program)); static void Main() { try { XmlDocument log4netConfig = new XmlDocument(); log4netConfig.Load(File.OpenRead("log4net.config")); var repo = log4net.LogManager.CreateRepository(Assembly.GetEntryAssembly(), typeof(log4net.Repository.Hierarchy.Hierarchy)); log4net.Config.XmlConfigurator.Configure(repo, log4netConfig["log4net"]); log.Info("Hello World!"); //Create X509Certificate2 object from .p12 file. X509Certificate2 x509 = new X509Certificate2(@"C:\file.p12", "password", X509KeyStorageFlags.Exportable | X509KeyStorageFlags.PersistKeySet); //Create X509Certificate2 object from .cer file. //byte[] rawData = ReadFile(args[0]); //x509.Import(rawData); //Log information contained in the certificate. log.Info(string.Format("Subject: {0}", x509.Subject)); log.Info(string.Format("Issuer: {0}", x509.Issuer)); log.Info(string.Format("Version: {0}", x509.Version)); log.Info(string.Format("Valid Date: {0}", x509.NotBefore)); log.Info(string.Format("Expiry Date: {0}", x509.NotAfter)); log.Info(string.Format("Thumbprint: {0}", x509.Thumbprint)); log.Info(string.Format("Serial Number: {0}", x509.SerialNumber)); log.Info(string.Format("Friendly Name: {0}", x509.PublicKey.Oid.FriendlyName)); log.Info(string.Format("Public Key Format: {0}", x509.PublicKey.EncodedKeyValue.Format(true))); log.Info(string.Format("Raw Data Length: {0}", x509.RawData.Length)); log.Info(string.Format("Certificate to string: {0}", x509.ToString(true))); log.Info(string.Format("Certificate to XML String: {0}", x509.PublicKey.Key.ToXmlString(false))); ////Add the certificate to a X509Store. //X509Store store = new X509Store(); //store.Open(OpenFlags.MaxAllowed); //store.Add(x509); //store.Close(); } catch (Exception ex) { log.Error(ex.Message); log.Error(ex.ToString()); } } }
Stamp CA ลงไฟล์ PDF
- [Solved] C# Sign PDF with iTextSharp 5.3.3 and USB token – Code Redirect
- CA1416: Validate platform compatibility (code analysis) – .NET | Microsoft Docs
ติดตั้ง Package
PM> Install-Package iTextSharp -Version 5.5.13.2 PM> Install-Package System.Windows.Extensions -Version 6.0.0
using iTextSharp.text; using iTextSharp.text.pdf; using iTextSharp.text.pdf.security; using System.Reflection; using System.Runtime.Versioning; using System.Security.Cryptography.X509Certificates; using System.Xml; namespace ConsoleApp6; [SupportedOSPlatform("windows")] class Program { public static readonly log4net.ILog log = log4net.LogManager.GetLogger(typeof(Program)); static void Main() { try { XmlDocument log4netConfig = new XmlDocument(); log4netConfig.Load(File.OpenRead("log4net.config")); var repo = log4net.LogManager.CreateRepository(Assembly.GetEntryAssembly(), typeof(log4net.Repository.Hierarchy.Hierarchy)); log4net.Config.XmlConfigurator.Configure(repo, log4netConfig["log4net"]); log.Info("Hello World!"); //Create X509Certificate2 object from .p12 file. X509Certificate2 x509 = new X509Certificate2(@"C:\file.p12", "password", X509KeyStorageFlags.Exportable | X509KeyStorageFlags.PersistKeySet); //Create X509Certificate2 object from .cer file. //byte[] rawData = ReadFile(args[0]); //x509.Import(rawData); //Add the certificate to a X509Store. X509Store store = new X509Store(); store.Open(OpenFlags.MaxAllowed); store.Add(x509); X509Certificate2Collection sel = X509Certificate2UI.SelectFromCollection(store.Certificates, null, null, X509SelectionFlag.SingleSelection); // X509Certificate2Collection sel = store.Certificates; X509Certificate2 cert = sel[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)}; //IExternalSignature externalSignature = new X509Certificate2Signature(cert, "SHA-1"); //IExternalSignature externalSignature = new X509Certificate2Signature(cert, DigestAlgorithms.SHA256); // var pk = Org.BouncyCastle.Security.DotNetUtilities.GetKeyPair(x509.PrivateKey).Private; var pk = Org.BouncyCastle.Security.DotNetUtilities.GetKeyPair(x509.GetRSAPrivateKey()).Private; IExternalSignature externalSignature = new PrivateKeySignature(pk, "SHA-256"); string pathToBasePdf = @"c:\tmp\InputPDF.pdf"; string pathToSignPdf = @"c:\tmp\OutputPDF.pdf"; //string pathToSignatureImage = ""; PdfReader pdfReader = new PdfReader(pathToBasePdf); var signedPdf = new FileStream(pathToSignPdf, FileMode.Create); PdfStamper pdfStamper = PdfStamper.CreateSignature(pdfReader, signedPdf, '\0'); PdfSignatureAppearance signatureAppearance = pdfStamper.SignatureAppearance; // signatureAppearance.SignatureGraphic = Image.GetInstance(pathToSignatureImage); signatureAppearance.SetVisibleSignature(new Rectangle(100, 100, 250, 150), pdfReader.NumberOfPages, "Signature"); //signatureAppearance.SignatureRenderingMode = PdfSignatureAppearance.RenderingMode.GRAPHIC_AND_DESCRIPTION; signatureAppearance.SignatureRenderingMode = PdfSignatureAppearance.RenderingMode.DESCRIPTION; MakeSignature.SignDetached(signatureAppearance, externalSignature, chain, null, null, null, 0, CryptoStandard.CMS); store.Close(); } catch (Exception ex) { log.Error(ex.Message); log.Error(ex.ToString()); } } }