-->

How to extract public key from a .Net DLL in C#? [

2019-09-01 06:12发布

问题:

This question already has an answer here:

  • How do I read the public key from a signed C# exe 2 answers

I want to extract public key, not public key token, in C# from a autenticode signed .Net DLL?

回答1:

To get a public key from an Autenticode signed .Net library use the following code:

Assembly assembly = Assembly.LoadFrom("dll_file_name");
X509Certificate certificate = assembly.ManifestModule.GetSignerCertificate();

byte[] publicKey = certificate.GetPublicKey();

But this will work only if the certificate was installed into Trusted Root Certification Authorities. Otherwise, GetSignerCertificate() returns null.

The second way allows to get a certificate even if it isn't in Trusted Root Certification Authorities.

X509Certificate executingCert = X509Certificate.CreateFromSignedFile("dll_file_name");
byte[] publicKey = certificate.GetPublicKey();