Verify Digital Signatures in Documents using C#
Usman Aziz
Posted on September 26, 2019
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 PDF, Word, and Excel, documents 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;
3. Load digitally signed PDF document using an instance of Signature class.
using (Signature signature = new Signature("sample.pdf"))
{
// Your code goes here.
}
4. Instantiate the DigitalVerifyOptions object and specify verification options.
DigitalVerifyOptions options = new DigitalVerifyOptions("certificate.pfx")
{
Comments = "Test comment"
};
5. Call Verify method of _Signature _class’ instance and pass _DigitalVerifyOptions _to it.
// verify document signatures
VerificationResult result = signature.Verify(options);
6. Check verification results from VerificationResult object.
if (result.IsValid)
{
Console.WriteLine("\nDocument was verified successfully!");
}
else
{
Console.WriteLine("\nDocument failed verification process.");
}
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.");
}
}
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!
Posted on September 26, 2019
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.