Another way to detect whether binaries are digitally signed (using C#)

Another way to detect whether binaries are digitally signed (using C#)


Recently in a blog post, we discussed, how to detect whether an assembly is digitally signed using the C# programming language and System.Management.Automation.dll from Microsoft. That was one way to get the status of the digital signing.

 

Today we will learn another method to detect the same without using any additional dll references in our project. I hope that, it will much more interesting to learn.

 

How to detect whether binaries are digitally signed (www.kunal-chowdhury.com)

 

In last post we learnt how to use the System.Management.Automation.dll to add PowerShell script to get the digital signature certificate details. The said dll is of approx. 2MB in size, which will unnecessary increase the overall size of your application just to grab the digital signing status.

 

So, what to do to overcome this? If you just need to check whether the file is digitally signed or not, you can use the API 'X509Certificate.CreateFromSignedFile' from the 'System.Security.Cryptography.X509Certificates' namespace. If you are able to create the certificate for the specified file from code, the file contains a digital certificate. In other case, it will throw exception. Handle that exception and return to user confirming 'No Certificate' available for that file.

 

Here's the code for you to use:

 

 
private bool IsDigitallySigned(string filePath)
{
    try
    {
        var digitalCertificate = X509Certificate.CreateFromSignedFile(filePath);
        return true;
    }
    catch (CryptographicException)
    {
        return false;
    }
}

 

Please note that the above process is simple enough to check whether the assembly is signed or not, but to grab more details from the certificate, you will need to use the System.Management.Automation.dll assembly reference.

 

If you are looking to know the way to grab more details out of the certificate using C# code, checkout my next post. Do let me know, if you have any queries. Stay tuned for upcoming articles in this blog space. Have a great day!

 

 


If you have come this far, it means that you liked what you are reading. Why not reach little more and connect with me directly on Twitter, Facebook, Google+ and LinkedIn. I would love to hear your thoughts and opinions on my articles directly. Also, don't forget to share your views and/or feedback in the comment section below.

0 comments

 
© 2008-2017 Kunal-Chowdhury.com - Microsoft Technology Blog for developers and consumers | Designed by Kunal Chowdhury
Back to top