diff --git a/Digital Signature/Sign-PDF-with-PEM-Certificate/.NET/Sign-PDF-with-PEM-Certificate.sln b/Digital Signature/Sign-PDF-with-PEM-Certificate/.NET/Sign-PDF-with-PEM-Certificate.sln new file mode 100644 index 00000000..96541826 --- /dev/null +++ b/Digital Signature/Sign-PDF-with-PEM-Certificate/.NET/Sign-PDF-with-PEM-Certificate.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.14.36616.10 d17.14 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Sign-PDF-with-PEM-Certificate", "Sign-PDF-with-PEM-Certificate\Sign-PDF-with-PEM-Certificate.csproj", "{15D14533-D3DD-4F05-9652-4DF196C648E3}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {15D14533-D3DD-4F05-9652-4DF196C648E3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {15D14533-D3DD-4F05-9652-4DF196C648E3}.Debug|Any CPU.Build.0 = Debug|Any CPU + {15D14533-D3DD-4F05-9652-4DF196C648E3}.Release|Any CPU.ActiveCfg = Release|Any CPU + {15D14533-D3DD-4F05-9652-4DF196C648E3}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {6F0575C6-8A8D-414F-84C6-11C432B44578} + EndGlobalSection +EndGlobal diff --git a/Digital Signature/Sign-PDF-with-PEM-Certificate/.NET/Sign-PDF-with-PEM-Certificate/Data/certificate.cer b/Digital Signature/Sign-PDF-with-PEM-Certificate/.NET/Sign-PDF-with-PEM-Certificate/Data/certificate.cer new file mode 100644 index 00000000..b1452732 --- /dev/null +++ b/Digital Signature/Sign-PDF-with-PEM-Certificate/.NET/Sign-PDF-with-PEM-Certificate/Data/certificate.cer @@ -0,0 +1,21 @@ +-----BEGIN CERTIFICATE----- +MIIEIDCCA+CgAwIBAgIUd5tYHFakkBrSoRMsnF9bwlIdj7YwCQYHKoZIzjgEAzAjMSEwHwYDVQQD +ExhUb2tlbiBTaWduaW5nIFB1YmxpYyBLZXkwHhcNMTcwNTExMDUxMjE4WhcNMTcwNTE4MDUxMjE4 +WjAtMSswKQYDVQQDHiIANQAxADUANQA4AGIANAAxAGQANwBhADgAYgAzADgANQAAMIGfMA0GCSqG +SIb3DQEBAQUAA4GNADCBiQKBgQCreQXHLNcWSKp4pKYo0p8J/t77ZWGdJdlM1RxgrjPxnCpUtrhb +2mqwha5fJ/sKRzU/dEUa5dPZCZWcT6sqX5/Hlrvm2/c5Ij59vohOxdwEhxuL1HMLjQdtz3hZTE91 +DD6/uu330xWUoEV9IobGitsrhRTykVVNnvA64I09lvnEuwIDAQABo4ICoDCCApwwDgYDVR0PAQH/ +BAQDAgWgMBMGA1UdJQQMMAoGCCsGAQUFBwMCMIIB/wYDVR0gBIIB9jCCAfIwggHuBgorBgEEAYI3 +MwMCMIIB3jCCAdoGCCsGAQUFBwICMIIBzB6CAcgATQBpAGMAcgBvAHMAbwBmAHQAIABkAG8AZQBz +ACAAbgBvAHQAIAB3AGEAcgByAGEAbgB0ACAAbwByACAAYwBsAGEAaQBtACAAdABoAGEAdAAgAHQA +aABlACAAaQBuAGYAbwByAG0AYQB0AGkAbwBuACAAZABpAHMAcABsAGEAeQBlAGQAIABpAG4AIAB0 +AGgAaQBzACAAYwBlAHIAdABpAGYAaQBjAGEAdABlACAAaQBzACAAYwB1AHIAcgBlAG4AdAAgAG8A +cgAgAGEAYwBjAHUAcgBhAHQAZQAsACAAbgBvAHIAIABkAG8AZQBzACAAaQB0ACAAbQBhAGsAZQAg +AGEAbgB5ACAAZgBvAHIAbQBhAGwAIABzAHQAYQB0AGUAbQBlAG4AdABzACAAYQBiAG8AdQB0ACAA +dABoAGUAIABxAHUAYQBsAGkAdAB5ACAAbwByACAAcwBhAGYAZQB0AHkAIABvAGYAIABkAGEAdABh +ACAAcwBpAGcAbgBlAGQAIAB3AGkAdABoACAAdABoAGUAIABjAG8AcgByAGUAcwBwAG8AbgBkAGkA +bgBnACAAcAByAGkAdgBhAHQAZQAgAGsAZQB5AC4wUwYDVR0jBEwwSoAURtLCWz4DNCN5LhccABko +THEQK2ShJ6QlMCMxITAfBgNVBAMTGFRva2VuIFNpZ25pbmcgUHVibGljIEtleYIJAL67zDTCqZpD +MB0GA1UdDgQWBBR+L+E5CKYJmufz2XcGMOFu+hb3LjAJBgcqhkjOOAQDAy8AMCwCFErwQFvnFgVT +Pcnh4wfjaGMjKStIAhQJ9ouuyB8Ot8+emod0q68bbGuEWg== +-----END CERTIFICATE----- diff --git a/Digital Signature/Sign-PDF-with-PEM-Certificate/.NET/Sign-PDF-with-PEM-Certificate/Data/logo.png b/Digital Signature/Sign-PDF-with-PEM-Certificate/.NET/Sign-PDF-with-PEM-Certificate/Data/logo.png new file mode 100644 index 00000000..02194707 Binary files /dev/null and b/Digital Signature/Sign-PDF-with-PEM-Certificate/.NET/Sign-PDF-with-PEM-Certificate/Data/logo.png differ diff --git a/Digital Signature/Sign-PDF-with-PEM-Certificate/.NET/Sign-PDF-with-PEM-Certificate/Data/privatekey.pem b/Digital Signature/Sign-PDF-with-PEM-Certificate/.NET/Sign-PDF-with-PEM-Certificate/Data/privatekey.pem new file mode 100644 index 00000000..0792a792 --- /dev/null +++ b/Digital Signature/Sign-PDF-with-PEM-Certificate/.NET/Sign-PDF-with-PEM-Certificate/Data/privatekey.pem @@ -0,0 +1,15 @@ +-----BEGIN RSA PRIVATE KEY----- +MIICWwIBAAKBgQCreQXHLNcWSKp4pKYo0p8J/t77ZWGdJdlM1RxgrjPxnCpUtrhb +2mqwha5fJ/sKRzU/dEUa5dPZCZWcT6sqX5/Hlrvm2/c5Ij59vohOxdwEhxuL1HML +jQdtz3hZTE91DD6/uu330xWUoEV9IobGitsrhRTykVVNnvA64I09lvnEuwIDAQAB +AoGAFVZfeNv5vdz3/ZWQ1Ev1R2ikSwiKv46J/kzB6xPyWcfoNUIcKdiW5cq/b+Qz +5yEE8sBJ/cwIncgwClNC8OhqTiyBJjK1KdyfMkUgdBTtwTiHcPMyxy5XTXyiSnVO +2ig91PxVu7B+gTjZYX0/Sv3KxiPmQvQ7KF/3iAoGDoQpwrkCQQDUYiKyqLuJyU3z +G7MxCX3fxCAdUndtTXq5IRGklLtsuRFG2qvA3psgptDBnG+Damm1/5/YIV47lMe9 +ye6yiT75AkEAzrAMhb3+uCKEK0p//Ktdt0h5WF2uFf2KQ4ZmfRiAgl2KbpyXPZYC +fc5t+WTs40D7W/A9NdUpc9yHqfN7WzGqUwJAVnUouOA1kA7R27CgDstVYZ1ekXE7 +ytQqrGiI3R3q+oSQQu7L0nUMY4tlGhwLzUypgfxw3rQev1R+x4UH1chHSQJAdJ3O +IvPFMT2AVzCsr/I8aTpCBQKuQ4gPJ/RABVr6vxNqFOjTSJNICpWbZTh1k+9JI2GU +F7iSr+WAwfo34odOMwJAJj9EibYjBlwrtIkv+cPSQ6OeBeZUrHCjOsnmS4z98Ju7 +mtYfb45W8VbAp5USJJP1t2jbywMutgDpsLw6wW/3jg== +-----END RSA PRIVATE KEY----- diff --git a/Digital Signature/Sign-PDF-with-PEM-Certificate/.NET/Sign-PDF-with-PEM-Certificate/Output/gitkeep.txt b/Digital Signature/Sign-PDF-with-PEM-Certificate/.NET/Sign-PDF-with-PEM-Certificate/Output/gitkeep.txt new file mode 100644 index 00000000..e69de29b diff --git a/Digital Signature/Sign-PDF-with-PEM-Certificate/.NET/Sign-PDF-with-PEM-Certificate/Program.cs b/Digital Signature/Sign-PDF-with-PEM-Certificate/.NET/Sign-PDF-with-PEM-Certificate/Program.cs new file mode 100644 index 00000000..7b795192 --- /dev/null +++ b/Digital Signature/Sign-PDF-with-PEM-Certificate/.NET/Sign-PDF-with-PEM-Certificate/Program.cs @@ -0,0 +1,61 @@ +using Org.BouncyCastle.Asn1.Pkcs; +using Org.BouncyCastle.Crypto; +using Org.BouncyCastle.OpenSsl; +using Org.BouncyCastle.Pkcs; +using Org.BouncyCastle.Security; +using Syncfusion.Drawing; +using Syncfusion.Pdf; +using Syncfusion.Pdf.Graphics; +using Syncfusion.Pdf.Security; + +//Creates a new PDF document. +using (PdfDocument document = new PdfDocument()) +{ + //Add a new page. + PdfPageBase page = document.Pages.Add(); + PdfGraphics graphics = page.Graphics; + //Get the certificate file. + Org.BouncyCastle.X509.X509CertificateParser cp = new Org.BouncyCastle.X509.X509CertificateParser(); + Org.BouncyCastle.X509.X509Certificate cert = cp.ReadCertificate(File.ReadAllBytes(Path.GetFullPath(@"Data/certificate.cer"))); + //Read the PEM file. + PemReader pmr = new PemReader(new StringReader(File.ReadAllText(Path.GetFullPath(@"Data/privateKey.pem")))); + AsymmetricCipherKeyPair KeyPair = (AsymmetricCipherKeyPair)pmr.ReadObject(); + //Get the PFX file stream. + Stream pfxFile = CreatePfxFile(cert, KeyPair.Private); + //Creates a certificate instance from the PFX file with a private key. + PdfCertificate pdfCert = new PdfCertificate(pfxFile, "syncfusion"); + //Creates a digital signature. + PdfSignature signature = new PdfSignature(document, page, pdfCert, "Signature"); + //Sets the signature information. + signature.Bounds = new RectangleF(new PointF(0, 0), new SizeF(300, 100)); + signature.ContactInfo = "johndoe@owned.us"; + signature.LocationInfo = "Honolulu, Hawaii"; + signature.Reason = "I am author of this document."; + FileStream imageStream = new FileStream(Path.GetFullPath(@"Data/Logo.png"), FileMode.Open, FileAccess.Read); + //Load an image file. + PdfBitmap image = new PdfBitmap(imageStream); + //Draw an image in the signature appearance. + signature.Appearance.Normal.Graphics.DrawImage(image, new RectangleF(0, 0, 300, 100)); + //Saves the document. + document.Save(Path.GetFullPath(@"Output/Output.pdf")); +} + +// Create a PFX file using the BouncyCastle. + static Stream CreatePfxFile(Org.BouncyCastle.X509.X509Certificate certificate, AsymmetricKeyParameter privateKey) +{ + //Create a certificate entry. + X509CertificateEntry certEntry = new X509CertificateEntry(certificate); + string friendlyName = certificate.SubjectDN.ToString(); + //Get bytes of the private key. + PrivateKeyInfo keyInfo = PrivateKeyInfoFactory.CreatePrivateKeyInfo(privateKey); + byte[] keyBytes = keyInfo.ToAsn1Object().GetEncoded(); + Pkcs12StoreBuilder builder = new Pkcs12StoreBuilder(); + builder.SetUseDerEncoding(true); + Pkcs12Store store = builder.Build(); + //Create a store entry. + store.SetKeyEntry("private", new AsymmetricKeyEntry(privateKey), new X509CertificateEntry[] { certEntry }); + //Save the story to the stream. + MemoryStream stream = new MemoryStream(); + store.Save(stream, "syncfusion".ToCharArray(), new SecureRandom()); + return stream; +} \ No newline at end of file diff --git a/Digital Signature/Sign-PDF-with-PEM-Certificate/.NET/Sign-PDF-with-PEM-Certificate/Sign-PDF-with-PEM-Certificate.csproj b/Digital Signature/Sign-PDF-with-PEM-Certificate/.NET/Sign-PDF-with-PEM-Certificate/Sign-PDF-with-PEM-Certificate.csproj new file mode 100644 index 00000000..13546e8f --- /dev/null +++ b/Digital Signature/Sign-PDF-with-PEM-Certificate/.NET/Sign-PDF-with-PEM-Certificate/Sign-PDF-with-PEM-Certificate.csproj @@ -0,0 +1,16 @@ + + + + Exe + net8.0 + Sign_PDF_with_PEM_Certificate + enable + enable + + + + + + + +