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

发布于 2024-09-19 07:42:17 字数 696 浏览 8 评论 0原文

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

I'm signing a dot net exe using

signcode.exe with an spc/pvk combo

The file needs to read its own Public Key at runtime in order to verify some data. I've gone down a number of different avenues.

I've tried

X509Certificate executingCert = X509Certificate.CreateFromSignedFile(exe);

executingCert is then null. I'm guessing signcode isn't creating an X509 signed file, though if there's a switch to change that I'm happy to go that way.

edited
Turns out the above does work.... I had my null check backwards (!= != ==) :)

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

与君绝 2024-09-26 07:42:17

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

查看此相关的 StackOverflow 问题,其中有很多答案: WinVerifyTrust 检查具体签名?

SignCode (for .Net 1.0 and 1.1) uses Authenticode signing, which as far as I'm aware, lacks a .Net Framework managed interface. You will likely need to use P/Invoke to call routines in Win32 API such as those found in this KB article: How To Get Information from Authenticode Signed Executables. Likely you'll need to use CryptQueryObject which will get you the
certificate, which you will then likely have to find another routine to pull the public key from.

Check out this related StackOverflow question which has a lot of answers: WinVerifyTrust to check for a specific signature?

玩套路吗 2024-09-26 07:42:17

尝试类似的方法:

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

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

Try something like that:

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

I used GetEntryAssembly in that case, but of course you can call the method on any loaded assembly.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文