美文网首页
PowerShell 创建自签名证书, 对exe文件进行数字签名

PowerShell 创建自签名证书, 对exe文件进行数字签名

作者: 虚心的锄头 | 来源:发表于2021-05-21 15:13 被阅读0次

    使用 PowerShell 命令创建证书

    三条命令

    New-SelfSignedCertificate -Type Custom -Subject "CN=姓名, O=公司名称, C=CN, L=上海, S=上海" -KeyUsage DigitalSignature -FriendlyName "MailTool" -CertStoreLocation "Cert:\CurrentUser\My" -TextExtension @("2.5.29.37={text}1.3.6.1.5.5.7.3.3", "2.5.29.19={text}") -NotAfter (Get-Date).AddYears(10)
    
    # Thumbprint                                Subject              EnhancedKeyUsageList
    # ----------                                -------              --------------------
    # BA167F5AF3984064891DD8E70030F8B1175ABFF1  CN=姓名, O=公司名称… 代码签名
    
    $password = ConvertTo-SecureString -String 123456 -Force -AsPlainText
    
    Export-PfxCertificate -cert "Cert:\CurrentUser\My\BA167F5AF3984064891DD8E70030F8B1175ABFF1" -FilePath C:\Users\Administrator\Desktop\test.pfx -Password $password
    
    关键字说明

    https://docs.microsoft.com//powershell/module/pki/new-selfsignedcertificate?view=windowsserver2019-ps

    # New-SelfSignedCertificate
    CN 公用名称(Common Name) 简称:CN 字段,对于 SSL 证书,一般为网站域名;而对于代码签名证书则为申请单位名称;而对于客户端证书则为证书申请者的姓名
    O 单位名称 (Organization Name) 简称:O 字段,对于 SSL 证书,一般为网站域名;而对于代码签名证书则为申请单位名称;而对于客户端单位证书则为证书申请者所在单位名称
    C 字段,只能是国家字母缩写,如中国:CN 
    -KeyUsage DigitalSignature  指定证书的密钥使用扩展名中设置的密钥使用, DigitalSignature 数字证书
    -FriendlyNam 证书的友好名称, 在mmc 控制台 证书中可以看见
    -CertStoreLocation 指定存储新证书的证书位置, Cert:\CurrentUser\My 当前用户
    -TextExtension 指定一系列证书扩展名, 后面的值基本固定
    -NotAfter (Get-Date).AddYears(10) 证书在10年内到期
    
    # ConvertTo-SecureString
    -String 123456 设置密码
    
    # Export-PfxCertificate
    -cert "Cert:\CurrentUser\My\BA167F5AF3984064891DD8E70030F8B1175ABFF1"  这个是Thumbprint值 BA167F5AF3984064891DD8E70030F8B1175ABFF1
    -FilePath 保存证书路径
    

    使用 signtool.exe 对exe文件签名

    SignTool.exe (Sign Tool) | Microsoft Docs

    signtool.exe sign /f test.pfx /p123456 /t http://timestamp.digicert.com /v "uninst.exe"
    
    # /f 指定证书
    # /p 密码
    # /t 时间戳
    # /v 过程
    

    ps:

    代码签名, 没有免费的数字证书了, 勉强用下自建的😃

    相关文章

      网友评论

          本文标题:PowerShell 创建自签名证书, 对exe文件进行数字签名

          本文链接:https://www.haomeiwen.com/subject/eluwjltx.html