如何使用Windows SDK中的工具创建用于代码签名的自签名证书?


当前回答

如回答中所述,为了使用不弃用的方式为自己的脚本签名,应该使用New-SelfSignedCertificate。

生成密钥:

New-SelfSignedCertificate -DnsName email@yourdomain.com -Type CodeSigning -CertStoreLocation cert:\CurrentUser\My

导出不带私钥的证书:

Export-Certificate -Cert (Get-ChildItem Cert:\CurrentUser\My -CodeSigningCert)[0] -FilePath code_signing.crt

[0]将使此工作用于当您有多个证书时……显然,要让索引与你想要使用的证书相匹配…或采用一种滤液方式(指纹或发胶)。

将其作为受信任的发布者导入

Import-Certificate -FilePath .\code_signing.crt -Cert Cert:\CurrentUser\TrustedPublisher

将其作为根证书颁发机构导入。

Import-Certificate -FilePath .\code_signing.crt -Cert Cert:\CurrentUser\Root

为脚本签名(假设这里的脚本名为script)。Ps1,修正相应的路径)。

Set-AuthenticodeSignature .\script.ps1 -Certificate (Get-ChildItem Cert:\CurrentUser\My -CodeSigningCert)

显然,一旦设置了密钥,就可以简单地用它为任何其他脚本签名。 您可以在本文中获得更详细的信息和一些故障排除帮助。

其他回答

在Powershell中使用New-SelfSignedCertificate命令非常简单。 打开powershell并运行这3条命令。

创建证书: $cert = New-SelfSignedCertificate -DnsName www.yourwebsite.com -Type CodeSigning -CertStoreLocation Cert:\CurrentUser\My 为它设置密码: $CertPassword = ConvertTo-SecureString -String " my_passsowrd " -Force - as明文 出口: Export-PfxCertificate -Cert "cert:\CurrentUser\My$($cert. thumbprint)" d: \ selfsigncert -FilePath”。pfx" -Password $CertPassword

您的证书selfsigncert。pfx将位于@ D:/


可选步骤:还需要向系统环境变量添加证书密码。在cmd中输入以下命令:setx CSC_KEY_PASSWORD "my_password"

如回答中所述,为了使用不弃用的方式为自己的脚本签名,应该使用New-SelfSignedCertificate。

生成密钥:

New-SelfSignedCertificate -DnsName email@yourdomain.com -Type CodeSigning -CertStoreLocation cert:\CurrentUser\My

导出不带私钥的证书:

Export-Certificate -Cert (Get-ChildItem Cert:\CurrentUser\My -CodeSigningCert)[0] -FilePath code_signing.crt

[0]将使此工作用于当您有多个证书时……显然,要让索引与你想要使用的证书相匹配…或采用一种滤液方式(指纹或发胶)。

将其作为受信任的发布者导入

Import-Certificate -FilePath .\code_signing.crt -Cert Cert:\CurrentUser\TrustedPublisher

将其作为根证书颁发机构导入。

Import-Certificate -FilePath .\code_signing.crt -Cert Cert:\CurrentUser\Root

为脚本签名(假设这里的脚本名为script)。Ps1,修正相应的路径)。

Set-AuthenticodeSignature .\script.ps1 -Certificate (Get-ChildItem Cert:\CurrentUser\My -CodeSigningCert)

显然,一旦设置了密钥,就可以简单地用它为任何其他脚本签名。 您可以在本文中获得更详细的信息和一些故障排除帮助。

这篇文章将只回答“如果你有证书,如何签署EXE文件”部分:

为了签署exe文件,我使用MS“signtool.exe”。为此,你需要下载庞大的微软Windows SDK,其容量高达1GB。幸运的是,您不必安装它。只需打开ISO并提取“Windows SDK Signing Tools-x86_en-us.msi”。它只有400 KB。

然后我创建了这个小脚本文件:

prompt $
echo off
cls

copy "my.exe" "my.bak.exe"

"c:\Program Files (x86)\Windows Kits\10\bin\10.0.22000.0\x64\signtool.exe" sign /fd SHA256 /f MyCertificate.pfx /p MyPassword My.exe

pause 

细节

__

你可以在Visual Studio 2019的项目属性中生成一个。在驱动程序签名部分,测试证书字段有一个下拉菜单。生成测试证书是其中一个选项。证书将位于扩展名为'cer'的文件中,通常与可执行文件或驱动程序位于相同的输出目录中。

从PowerShell 4.0 (Windows 8.1/Server 2012 R2)开始,不需要makecert.exe就可以在Windows中制作证书。

需要执行的命令为New-SelfSignedCertificate和Export-PfxCertificate。

说明请参见使用PowerShell创建自签名证书。