Introduction
This blog will give you a brief on how to create a Digital Signature using C# and a desktop executable application.
The Digital Signature will be performed at the client system. The Digital Signature token (DSC Token) is installed in the client local system.
Why use an executable file?
We are unable to read the client-side digital signature tokens from web applications due to security concerns, and all other possibilities are blocked. Therefore, we are proceeding with a local windows application (i.e. the Executable file (.exe)).
Check the below mentioned references to proceed:
- using iTextSharp.text.pdf;
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Security.Cryptography.X509Certificates;
- using Org.BouncyCastle.Security;
- using Org.BouncyCastle.X509;
- using X509Certificate = Org.BouncyCastle.X509.X509Certificate;
- using iTextSharp.text.pdf.security;
All the above-mentioned references are used for accessing DSC, Signing DSC, Reading PDF and Creating new signed PDF.
Create a variable for X509Certificate2
- X509Certificate2 certClient = null;
Get all the DSC users registered to local store and for current user using X509Store, PFB
- X509Store st = new X509Store(StoreName.My, StoreLocation.CurrentUser);
- st.Open(OpenFlags.MaxAllowed);
- X509Certificate2Collection collection = st.Certificates;
Select the certificate.
If we have multiple certificates registered in local, and if we want to pick the valid certificates using the name, please follow the below process:
Send "Name" as the DSC token owner name. You can check if the DSC token is expired or not. After all these checks, assign the value to "certClient".
- for (int i = 0; i < collection.Count; i++)
- {
- foreach (X509Certificate2 cert in collection)
- {
- certClient = cert;
- username = certClient.Subject;
- ErrorLogs(username);
- startdate = certClient.GetEffectiveDateString();
- enddate = certClient.GetExpirationDateString();
- if (collection[i].Subject.Contains("Name"))
- {
- certClient = collection[i];
- }
- }
- }
- st.Close();
Close the X509Store.
- //Get Certificate Chain
- IList<X509Certificate> chain = new List<X509Certificate>();
- X509Chain x509Chain = new X509Chain();
- x509Chain.Build(certClient);
- foreach (X509ChainElement x509ChainElement in x509Chain.ChainElements)
- {
- chain.Add(DotNetUtilities.FromX509Certificate(x509ChainElement.Certificate));
- }
Select the file to be signed (input file)
- string filename = @"C:\Users\admin\Desktop\sample.pdf";
- PdfReader inputPdf = new PdfReader(filename);
- FileStream signedPdf = new FileStream(@"C:\Users\admin\Desktop\Sample_signed.pdf", FileMode.Create);
- PdfStamper pdfStamper = PdfStamper.CreateSignature(inputPdf, signedPdf, '\0');
- IExternalSignature externalSignature = new X509Certificate2Signature(certClient, "SHA-256");
This will create a signature text in the input pdf
- PdfSignatureAppearance signatureAppearance = pdfStamper.SignatureAppearance;
- signatureAppearance.Reason = "My Signature";
- signatureAppearance.SetVisibleSignature(new iTextSharp.text.Rectangle(0, 00, 200, 100), inputPdf.NumberOfPages, "Signature");
- signatureAppearance.SignatureRenderingMode = PdfSignatureAppearance.RenderingMode.DESCRIPTION;
- MakeSignature.SignDetached(signatureAppearance, externalSignature, chain, null, null, null, 0,CryptoStandard.CMS);
- inputPdf.Close();
- pdfStamper.Close();
Above details are verified and working.

devendra kumarPosted May 14, 2025, 7:22 AM
Thank you sir
Lovely SinglaPosted May 19, 2024, 1:42 PM
Thanks, It's working, really very amazing code
Om Shankar TrivediPosted Nov 23, 2023, 6:13 AM
Hi, getting error unknown encryption algorithm system.security.cryptography.rsacng and itextsharp.pdf.security2signature..ctor
Sarfraz AlamPosted Sep 1, 2023, 5:37 PM
How to implement in web application this code working perfectly on window application
Uday DodiyaPosted Aug 22, 2022, 6:03 AM
Digital Signature Using USB Token. How to pass a password automatically with the iTextSharp MakeSignature.SignDetached method?
utkarsh shuklaPosted May 19, 2022, 4:43 AM
How To Digitally Sign A word Document Using USB token In c#
utkarsh shuklaPosted May 19, 2022, 4:41 AM
How to add digital signature on word file ? in c#
monish kumarPosted Sep 6, 2021, 8:44 AM
When i deployed on server, X509Store is not getting Digitial Signature certificate from client machine.
anagha bhorePosted Jul 17, 2021, 8:10 AM
Where to add PIN ?
sunil patelPosted Mar 15, 2021, 9:10 AM
Is there anyway we can bypass user to enter PIN ? can u share the code with me on [email protected]
Luca TonyPosted Jan 13, 2021, 4:31 AM
I have an project with features like the project here: https://github.com/tonytao34/TokenSigning-My tool called TokenSigning.TokenSigning is the component (plugin) for web app to sign document with Usb Token, Smartcard,… - TokenSigning not need Extension or add-on for browser - TokenSigning supported: windows + macos - TokenSigning supported all browsers: chrome, firefox, safari, ie, edge,....
Sridhar NPosted Nov 5, 2020, 12:41 AM
In the above same scenario i want additionally add one more signature in last page of PDF. when i try latest page only adding signature. can u pls guide me. Thanks.
Ram PrakashPosted Jul 16, 2020, 5:37 AM
How do sign PDF with Timestamp using USB DSC Token, Please share the code on my email id, [email protected] Thanks in advance.
Bharat VasantPosted Jun 23, 2020, 12:29 AM
For digital Signature from web application please follow my answer https://www.c-sharpcorner.com/forums/how-to-create-digital-signature-in-web-application or https://stackoverflow.com/a/55676351/9659885
Sharath MadhavPosted May 19, 2020, 12:45 PM
Kindly give me the solution for 'Unknown encryption algorithm System.Security.Cryptography.RSACng'
Sharath MadhavPosted May 19, 2020, 12:45 PM
IExternalSignature externalSignature = new X509Certificate2Signature(certClient, "SHA-256"); 'Unknown encryption algorithm System.Security.Cryptography.RSACng'