Hello!
could you help me?
I'm trying to sign a pdf file with a smart card and the private key is not included.... normally it would open a model for insert the password, but I just got an error...
Internal.Cryptography.CryptoThrowHelper.WindowsCryptographicException: 'The keyset is not defined.'
My Code c#
using Syncfusion.Drawing;
using Syncfusion.Pdf;
using Syncfusion.Pdf.Graphics;
using Syncfusion.Pdf.Interactive;
using Syncfusion.Pdf.Parsing;
using Syncfusion.Pdf.Security;
using System;
using System.IO;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
//Creates a new PDF document
PdfDocument document = new PdfDocument();
//Adds a new page
PdfPage page = document.Pages.Add();
//Create PDF graphics for the page
PdfGraphics graphics = page.Graphics;
X509Store store = new X509Store("MY", StoreLocation.CurrentUser);
store.Open(OpenFlags.ReadOnly | OpenFlags.OpenExistingOnly);
X509Certificate2Collection collection = (X509Certificate2Collection)store.Certificates;
X509Certificate2Collection fcollection = (X509Certificate2Collection)collection.Find(X509FindType.FindByTimeValid, DateTime.Now, false);
X509Certificate2Collection scollection = X509Certificate2UI.SelectFromCollection(fcollection, "Selecione o Certificado", "Selecione o certificado da lista abaixo para assinar seu documento", X509SelectionFlag.SingleSelection);
store.Close();
X509Certificate2 certificate = new X509Certificate2();
if (scollection.Count != 0)
certificate = (X509Certificate2)scollection[0];
PdfCertificate pdfCertificate = new PdfCertificate(certificate);
//Creates a digital signature
PdfSignature signature = new PdfSignature(document, page, pdfCertificate, "DigitalSignature");
signature.Bounds = new RectangleF(40, 40, 350, 100);
//Create a font to draw text.
PdfStandardFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 15);
//Drawing text, shape, and image into the signature appearance.
signature.Appearance.Normal.Graphics.DrawRectangle(PdfPens.Black, PdfBrushes.White, new RectangleF(50, 0, 300, 100));
signature.Appearance.Normal.Graphics.DrawString("Digitally Signed by Syncfusion", font, PdfBrushes.Black, 120, 17);
signature.Appearance.Normal.Graphics.DrawString("Reason: Testing signature", font, PdfBrushes.Black, 120, 39);
signature.Appearance.Normal.Graphics.DrawString("Location: USA", font, PdfBrushes.Black, 120, 60);
//Changing the digital signature standard and hashing algorithm.
signature.Settings.CryptographicStandard = CryptographicStandard.CADES;
signature.Settings.DigestAlgorithm = DigestAlgorithm.SHA512;
System.IO.Stream stream = new System.IO.FileStream("pdf3.pdf", FileMode.Create);
//Save the document
document.Save(stream);
//Close the document
document.Close(true);
If your application is trying to access a certificate from certificate MMC where the certificate corresponds to a private key, you will probably encounter this cryptographic exception error (Keyset does not exist or Access is denied.) The reason behind this is the Private Key is saved in a special file system named as “Machinekeys” folder and it’s not readable for every user. You need to provide read access to the application pool’s user to the key.
We suggest you to follow the below steps and try if it helps you in resolving the issue: