如何从签名的C#exe中读取公钥

我正在签署一个dot net exe

signcode.exe with an spc/pvk combo 

该文件需要在运行时读取自己的公钥以validation某些数据。 我走了很多不同的途径。

我试过了

 X509Certificate executingCert = X509Certificate.CreateFromSignedFile(exe); 

那么executionCert就是null。 我猜测signcode没有创建一个X509签名文件,但如果有一个改变的转换,我很高兴这样做。

编辑结果以上工作….我向后检查了我的空检查(!=!= ==):)

 Assembly asm = Assembly.GetExecutingAssembly(); string exe = asm.Location; X509Certificate executingCert = X509Certificate.CreateFromSignedFile(exe); if (executingCert != null) { Console.WriteLine("Assembly is signed"); byte[] assemblyKey = executingCert.GetPublicKey(); } 

SignCode(用于.Net 1.0和1.1)使用Authenticode签名,据我所知,缺少.Net Framework托管接口。 您可能需要使用P / Invoke来调用Win32 API中的例程,例如此知识库文章中的例程:如何从Authenticode签名的可执行文件中获取信息 。 可能你需要使用CryptQueryObject来获取证书,然后你可能需要找到另一个从中提取公钥的例程。

看看这个有很多答案的StackOverflow问题: WinVerifyTrust来检查特定的签名?

试试这样的事情:

 Assembly.GetEntryAssembly().GetName().GetPublicKey() 

在这种情况下我使用了GetEntryAssembly,但当然你可以在任何加载的程序集上调用该方法。