Verify Digital Signature

Skip Navigation LinksHome  /  Support  /  Forums  /  DynamicPDF CoreSuite for .NET (v11)  /  Re: Verify Digital Signature

DynamicPDF CoreSuite for .NET (v11) Forum

 Jul 29 2021 6:24 AM
Hello everyone,

I'm using Dynamic PDF to export PDF with Digital Sign. But I want to know if exist something that when my customer upload again this PDF with his digital signature to verify if mine Signature is OK.

Best regards
 Jul 29 2021 6:14 PM
Posted by a ceTe Software moderator
Hi,

You can verify the signature contents in an existing PDF using DynamicPDF Core Suite for .NET.  Here is a code sample.  Please note, it will throw an exception if the data is invalid for the given signature.

            string input = @"Path for a singed PDF";

            // Create a PdfDocument object using the source PDF that is being verified for signatures
            PdfDocument pdf = new PdfDocument(input);

            // Get the list of PdfSignatureField from the pdf document
            List<PdfSignatureField> signedSignatureFields = pdf.GetSignedSignatureFields();
            PdfSignatureField pdfSignatureField = signedSignatureFields[0];

            // Get the content that is signed using the selected signature field
            byte[] signatureContent = pdfSignatureField.SignatureContent;

            // Create a byte array to hold the data that is present in the signature range
            byte[] dataToVerify = new byte[pdfSignatureField.Range[0].Length + pdfSignatureField.Range[1].Length];

            // Get all the PDF file data that is being validated
            byte[] fileData = File.ReadAllBytes(input);

            // Load the dataToVerify from the PDF, using the range of the signature
            Array.Copy(fileData, dataToVerify, pdfSignatureField.Range[0].Length);
            Array.Copy(fileData, pdfSignatureField.Range[1].StartIndex, dataToVerify, pdfSignatureField.Range[0].Length, pdfSignatureField.Range[1].Length);

            // Create a ContentInfo object using the data to be verified
            System.Security.Cryptography.Pkcs.ContentInfo contentInfo = new ContentInfo(dataToVerify);

            // Create a SignedCms object using contentInfo
            SignedCms signedCms = new SignedCms(contentInfo, true);

            // Decode the signature content
            signedCms.Decode(signatureContent);

            // Verify the signature. This method will throw an exception if the data is invalid for the given signature.
            //passing boolean as false will verify both signature and certificate chain.
            //passing true will verify only signature contents.
            signedCms.CheckSignature(true);

Thanks,
ceTe Software Support Team
 Aug 02 2021 5:24 AM
Thanks very much. Works fine but is any way to check if a signature is the same with the original?
 Aug 03 2021 12:13 PM
Posted by a ceTe Software moderator
Hi,

You can verify the signature contents using the above posted code sample, but DynamicPDF Core Suite product does not support detecting whether the signature is same as the original.

Thanks,
ceTe Software Support Team

All times are US Eastern Standard time. The time now is 7:21 AM.