DEV Community

Usman Aziz
Usman Aziz

Posted on • Originally published at blog.groupdocs.com

Verify Digital Signatures in Documents using C#

Digital Signatures in documents look similar to the paper-based signatures, however, being certificate-based electronic signatures they contain the identity of the signer in encrypted form. The certificates are issued by trusted and authorized Certificate Authorities and they identify the person they are issued to. This is why the digitally signed documents can be verified at any time. In this article, I’ll show you how to programmatically verify the digital signature in PDFWord, and Exceldocuments by using GroupDocs.Signature for .NET API with C#.

Steps to verify digitally signed PDF document

For demonstration, I am using a PDF document for digital signature verification. However, the same code will work for MS Word and Excel document formats.

1. Download GroupDocs.Signature for .NET or install it using NuGet.

2. Add the following namespaces in your code.

using GroupDocs.Signature;
using GroupDocs.Signature.Domain;
using GroupDocs.Signature.Options;
Enter fullscreen mode Exit fullscreen mode

3. Load digitally signed PDF document using an instance of Signature class.

using (Signature signature = new Signature("sample.pdf"))
{
    // Your code goes here.
}
Enter fullscreen mode Exit fullscreen mode

4. Instantiate the DigitalVerifyOptions object and specify verification options.

DigitalVerifyOptions options = new DigitalVerifyOptions("certificate.pfx")
{
    Comments = "Test comment"
};
Enter fullscreen mode Exit fullscreen mode

5. Call Verify method of _Signature _class’ instance and pass _DigitalVerifyOptions _to it.

// verify document signatures
VerificationResult result = signature.Verify(options);
Enter fullscreen mode Exit fullscreen mode

6. Check verification results from VerificationResult object.

if (result.IsValid)
{
    Console.WriteLine("\nDocument was verified successfully!");
}
else
{
    Console.WriteLine("\nDocument failed verification process.");
}
Enter fullscreen mode Exit fullscreen mode

Complete Code

using GroupDocs.Signature;
using GroupDocs.Signature.Domain;
using GroupDocs.Signature.Options;

using (Signature signature = new Signature("sample.pdf"))
{
    DigitalVerifyOptions options = new DigitalVerifyOptions("certificate.pfx")
    {
        Comments = "Test comment"
    };
    // verify document signatures
    VerificationResult result = signature.Verify(options);
    if (result.IsValid)
    {
        Console.WriteLine("\nDocument was verified successfully!");
    }
    else
    {
        Console.WriteLine("\nDocument failed verification process.");
    }
}
Enter fullscreen mode Exit fullscreen mode

Thus, you can determine whether the digital signature in the PDF document meets the specified criteria or not. Finally, you can mark the document as valid or invalid.

Cheers!

Top comments (0)