0

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

Rohit
  • 3,610
  • 7
  • 45
  • 76
  • did you try [GetPublicKey](https://msdn.microsoft.com/en-us/library/system.reflection.assemblyname.getpublickey%28v=vs.110%29.aspx)? – default Mar 03 '15 at 13:18
  • Does it need to be via code or would going through the windows GUI be ok? – Scott Chamberlain Mar 03 '15 at 13:19
  • using code as I want to do it runtime. I can extract for my use easily. – Rohit Mar 03 '15 at 13:27
  • @Default I understand that GetPublicKey gets strong name signature not autenticode signature – Rohit Mar 03 '15 at 14:07
  • I believe Authenticode is actually outside the CLR since it can be used for unmanaged code as well. Perhaps head down the road investigating the WinTrustVerify function. https://msdn.microsoft.com/en-us/library/windows/desktop/aa388208%28v=vs.85%29.aspx – Steve Mitcham Mar 03 '15 at 14:24

1 Answers1

7

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();
Yoh Deadfall
  • 2,711
  • 7
  • 28
  • 32