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

时间:2010-09-15 23:26:24

标签: c# public-key x509 signtool

我正在使用

签署一个dot net exe
signcode.exe with an spc/pvk combo

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

我试过

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();
}

2 个答案:

答案 0 :(得分:3)

SignCode(用于.Net 1.0和1.1)使用Authenticode签名,据我所知,缺少.Net Framework托管接口。您可能需要使用P / Invoke来调用Win32 API中的例程,例如此KB article: How To Get Information from Authenticode Signed Executables中的例程。可能你需要使用CryptQueryObject才能获得 证书,您可能需要找到另一个从中提取公钥的例程。

查看这个有很多答案的StackOverflow问题:WinVerifyTrust to check for a specific signature?

答案 1 :(得分:0)

尝试类似的东西:

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

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