【问题标题】:How do I read the public key from a signed C# exe如何从签名的 C# exe 中读取公钥
【发布时间】:2011-04-12 23:08:39
【问题描述】:

我正在使用

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

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

我试过了

X509Certificate executingCert = X509Certificate.CreateFromSignedFile(exe);

executingCert 然后为空。我猜 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();
}

【问题讨论】:

    标签: c# public-key x509 signtool


    【解决方案1】:

    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?

    【讨论】:

    • 谢谢,如果我能完成所有的 p/invoking,那应该可行
    【解决方案2】:

    试试这样的:

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

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

    【讨论】:

    • 这是读取强名称的公钥,而不是基于证书的数字签名!
    猜你喜欢
    • 2017-10-09
    • 2022-10-20
    • 2023-03-10
    • 1970-01-01
    • 1970-01-01
    • 2014-09-17
    • 2013-08-17
    • 2021-08-23
    • 2016-03-08
    相关资源
    最近更新 更多