Digital signature of PDF file using DSC Token C# with Dynamic settings
Digital Signature in PDF file Sign in Process -
Console Application with c# and ItextSharp.dll File
I have Created two Folder in Drive and dynamically set path in App.config File.
First Folder i will kepp PDF File and after Signing in PDF file transfer on another Folder and Delete Signed PDF File from First Folder.
Abount DSC Token How to get and Install -
Get Information about DSC Token Click on DSC Token Info
If you have DSC Token then Install on your syatem after installing you can check
Internet Options -> Click on Contents -> Click On certificates your DSC information will Find Here
Now Coding Part are As below :-
App.Config settings
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5"/></startup>
<appSettings>
<add key="DscPassword" value="abc@123"/> <!-- DSC Password-->
<add key="FilePath" value="E:/DigitalSignature/invoice/"/> <!--PDF File Path-->
<add key="SignFilePath" value="E:/DigitalSignature/Signinvoice/"/> <!--Signed PDF File Path-->
<add key="TokenCertificateName" value="SANJAY SHARMA"/> <!--DSC Token Name-->
<add key="Left" value="400"/> <!--From Left to right-->
<add key="Right" value="500"/> <!--Content with to show data after giving space from right side-->
<add key="Top" value="70"/> <!--Display Signature from giving space from bottom to up-->
<add key="Bottom" value="110"/> <!--Data will Display between to and bottom value given height-->
</appSettings>
</configuration>
Coding Part With All Information used in
1.Add ItextSharp.dll
2.Add Assembly System.Security (For Adding Right Click on Project->Add Reference ->Assemblies -> Framework ->System.Security)
Complete c# Code are As :-
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.pdf.security;
using Org.BouncyCastle.Security;
using Org.BouncyCastle.X509;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Net.NetworkInformation;
using System.Security;
using System.Security.AccessControl;
using System.Security.Cryptography;
using System.Security.Cryptography.Pkcs;
using System.Security.Cryptography.X509Certificates;
using System.Security.Permissions;
namespace DigitalSignConsole
{
class DigitalWithoutPassword
{
static string subj = "";
static void Main(string[] args)
{
string TokenCertificateName = ConfigurationManager.AppSettings["TokenCertificateName"];
int num = 0;
string text = "";
string text2 = "";
int num2 = 0;
bool useSmartCardPIN;
string FilePath = ConfigurationManager.AppSettings["FilePath"];
string SignFilePath = ConfigurationManager.AppSettings["SignFilePath"];
string DscPassword = ConfigurationManager.AppSettings["DscPassword"];
//cordinate
float left = float.Parse(ConfigurationManager.AppSettings["Left"]);
float Top = float.Parse(ConfigurationManager.AppSettings["Top"]);
float right = float.Parse(ConfigurationManager.AppSettings["Right"]);
float Buttom = float.Parse(ConfigurationManager.AppSettings["Bottom"]);
//string FilePath = @"E:/DigitalSignature/invoice/";
// string SignFilePath = @"E:/DigitalSignature/Signinvoice/";
string[] files = Directory.GetFiles(FilePath);
DirectoryInfo dir = new DirectoryInfo(FilePath);
foreach (FileInfo flInfo in dir.GetFiles())
{
string text3 = "", SignPdfName = "";
text3 = flInfo.Name;
string path = "OldFilePath:" + flInfo.DirectoryName + "\\" + text3 + "$";
SignPdfName = "NewFilePath:" + SignFilePath + text3 + "$";
text = getBetween(path, "OldFilePath:", "$");
text2 = getBetween(SignPdfName, "NewFilePath:", "$");
}
//here we can set Path Information on text File and Used it
//StreamReader streamReader = new StreamReader("E:/QRCodeImages/FilesInfo.txt");
//string text3;
//while ((text3 = streamReader.ReadLine()) != null)
//{
// if (text3.Contains("OldFilePath:"))
// {
// text = getBetween(text3, "OldFilePath:", "$");
// }
// if (text3.Contains("NewFilePath:"))
// {
// text2 = getBetween(text3, "NewFilePath:", "$");
// }
// num++;
//}
using (MemoryStream ms = new MemoryStream())
{
PdfCopyFields copy = new PdfCopyFields(ms);
copy.Writer.ViewerPreferences = PdfWriter.PageModeUseOutlines;
ArrayList outlines = new ArrayList();
int pageOffset = 0;
string file = text;
PdfReader reader = new PdfReader(file);
copy.AddDocument(reader);
pageOffset += reader.NumberOfPages;
copy.Close();
reader.Close();
System.GC.Collect();
System.GC.WaitForPendingFinalizers();
MemoryStreamToFile(ms, text);
}
Console.WriteLine("Reading File Path");
// streamReader.Close();
Console.WriteLine(text+" "+ text2);
try
{
//Console.WriteLine("Searching for certificate");
//X509CertificateParser x509CertificateParser = new X509CertificateParser();
//X509Certificate2 certificate = null;
//X509Store x509Store = new X509Store(StoreName.My, StoreLocation.CurrentUser);
//x509Store.Open(OpenFlags.OpenExistingOnly);
//X509Certificate2Collection certificates = x509Store.Certificates;
//x509Store.Open(OpenFlags.MaxAllowed);
//X509Certificate2Collection x509Certificate2Collection = X509Certificate2UI.SelectFromCollection(x509Store.Certificates, "Please choose certificate:", "", X509SelectionFlag.SingleSelection);
//if (x509Certificate2Collection.Count > 0)
//{
// certificate = x509Certificate2Collection[0];
// subj = certificate.Subject;
//}
//x509Store.Close();
// string TokenCertificateName = "SANJAY SHARMA";
X509Certificate2 certificate = null;
X509Store store = new X509Store(StoreName.My, StoreLocation.CurrentUser);
store.Open(OpenFlags.OpenExistingOnly);
X509Certificate2Collection certificates = store.Certificates;
X509Certificate2 cert = new X509Certificate2();
for (int i = 0; i < certificates.Count; i++)
{
cert = certificates[i];
List<X509KeyUsageExtension> extensions = cert.Extensions.OfType<X509KeyUsageExtension>().ToList();
if (cert.GetNameInfo(X509NameType.SimpleName, false).ToString() == TokenCertificateName)
{
for (int j = 0; j < extensions.Count; j++)
{
if ((extensions[j].KeyUsages & X509KeyUsageFlags.DigitalSignature) == X509KeyUsageFlags.DigitalSignature)
{
certificate = cert;
subj = cert.Subject;
}
}
}
}
store.Close();
IList<Org.BouncyCastle.X509.X509Certificate> list = new List<Org.BouncyCastle.X509.X509Certificate>();
X509Chain x509Chain = new X509Chain();
x509Chain.Build(certificate);
X509ChainElementEnumerator enumerator = x509Chain.ChainElements.GetEnumerator();
while (enumerator.MoveNext())
{
X509ChainElement current = enumerator.Current;
list.Add(DotNetUtilities.FromX509Certificate(current.Certificate));
}
if (File.Exists(text))
{
}
PdfReader pdfReader = new PdfReader(text);
num2 = pdfReader.NumberOfPages;
pdfReader.Close();
Console.WriteLine("Signing file...");
//*****************************Password Prompt Disable****************************
RSACryptoServiceProvider rSACryptoServiceProvider = new RSACryptoServiceProvider();
try
{
if (certificate.HasPrivateKey)
{
rSACryptoServiceProvider = (certificate.PrivateKey as RSACryptoServiceProvider);
if (rSACryptoServiceProvider.CspKeyContainerInfo.HardwareDevice)
{
SecureString secureString = new SecureString();
string smartCardPin = DscPassword;
foreach (char c in smartCardPin)
{
secureString.AppendChar(c);
}
CspParameters parameters = new CspParameters(rSACryptoServiceProvider.CspKeyContainerInfo.ProviderType, rSACryptoServiceProvider.CspKeyContainerInfo.ProviderName, rSACryptoServiceProvider.CspKeyContainerInfo.KeyContainerName, new CryptoKeySecurity(), secureString);
rSACryptoServiceProvider = new RSACryptoServiceProvider(parameters);
// SignTestMessage(certificate);
useSmartCardPIN = true;
}
}
}
catch (Exception ex)
{
throw new CryptographicException("Bypassing PIN Exception: " + ex.Message);
}
finally
{
rSACryptoServiceProvider.Clear();
}
//***************************** End Password Prompt Disable****************************
for (int i = 1; i <= num2; i++)
{
if (i != 1)
{
File.Delete(text);
File.Copy(text2, text);
File.Delete(text2);
}
PdfReader pdfReader2 = new PdfReader(text);
FileStream fileStream = new FileStream(text2, FileMode.Create);
PdfStamper pdfStamper = PdfStamper.CreateSignature(pdfReader2, fileStream, '\0', Path.GetTempFileName(), append: true);
PdfSignatureAppearance signatureAppearance = pdfStamper.SignatureAppearance;
signatureAppearance.Acro6Layers = false;
signatureAppearance.Reason = "Digitily Sign Invoice";
string[] sub = subj.Split(',');
string Loc = sub[2].ToString();
string[] Sign = Loc.Split(new char[] { '=' }, 2);
string Location = Sign[1].ToString();
signatureAppearance.Location = Location;
signatureAppearance.SignDate = DateTime.Now;
signatureAppearance.SetVisibleSignature(new Rectangle(left,Top, right, Buttom), i, null);
// signatureAppearance.SetVisibleSignature(new Rectangle(600f, 55f, 450f, 90f), i, null);
IExternalSignature externalSignature = new X509Certificate2Signature(certificate, "SHA-1");
MakeSignature.SignDetached(signatureAppearance, externalSignature, list, null, null, null, 0, CryptoStandard.CMS);
fileStream.Close();
pdfReader2.Close();
pdfStamper.Close();
}
File.Delete(text);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
Console.ReadLine();
}
}
// CertSelect
public static string getBetween(string strSource, string strStart, string strEnd)
{
if (strSource.Contains(strStart) && strSource.Contains(strEnd))
{
int num = strSource.IndexOf(strStart, 0) + strStart.Length;
int num2 = strSource.IndexOf(strEnd, num);
return strSource.Substring(num, num2 - num);
}
return "";
}
public static void SignTestMessage(X509Certificate2 cert)
{
try
{
byte[] content = new byte[12]
{
166,
100,
234,
184,
137,
4,
194,
172,
72,
67,
65,
14
};
System.Security.Cryptography.Pkcs.ContentInfo contentInfo = new System.Security.Cryptography.Pkcs.ContentInfo(content);
SignedCms signedCms = new SignedCms(contentInfo);
CmsSigner cmsSigner = new CmsSigner(cert);
cmsSigner.IncludeOption = X509IncludeOption.EndCertOnly;
signedCms.ComputeSignature(cmsSigner, silent: false);
signedCms.Encode();
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
public static void MemoryStreamToFile(MemoryStream MS, string FileName)
{
using (FileStream fs = new FileStream(@FileName, FileMode.Create))
{
byte[] data = MS.ToArray();
fs.Write(data, 0, data.Length);
fs.Close();
}
}
}
}