繁体   English   中英

如何对使用SharpDeveloper或内置csc.exe生成的.NET exe进行自签名?

[英]How to Self-Sign a .NET exe generated with SharpDeveloper or built-in csc.exe?

假设我使用Windows 10附带的csc.exe编译了我的c#应用程序,而没有安装Visual Studio,该如何自签名此应用程序? 我能想到的最好的方法是下面发布的这个疯狂的Powershell脚本...在我看来,对您的应用程序进行签名应该比这更容易。

# SCRIPT: signit.ps1
#
# Purpose: Sign a .NET Exe compiled by SharpDeveloper with a SelfSignedCertificate
#
# Usage:
#     Run signit.ps1 Script from an Administrator Powershell
#
#     PS>         Process-start -verb runas powershell
#     PS(ADMIN)>  Set-ExecutionPolicy -scope Process Unrestricted
#     Yes
#     PS(ADMIN)>  ./signit.ps1

# Sign EXE with PFX Certificate using SHA1
function SignIt {

    # Path to your Exe to sign
    $exe      = "$home\Desktop\tntrocketcar\bin\Debug\tntrocketcar.exe"

    # Name of your company
    $friendly_name = "ACME Software"
    $subject_cn    = "Wile E. Coyote Ventures"       #Common Name
    $subject_o     = "Roadrunner Foundation"         #Organization
    $subject_e     = "wile.e.coyote@mailinator.com"  #Email 
    $subject_c     = "US"                            #Country
    $subject_st    = "Arizona"                       #State

    # Path to signtool installed from "Windows SDK" download
    $signtool = "C:\Program Files (x86)\Windows Kits\10\bin\10.0.16299.0\x64\signtool.exe"

    $pfx      = "MySigniture.pfx"
    $location = "Cert:\LocalMachine\My"
    $tstamp   = "http://timestamp.verisign.com/scripts/timstamp.dll"

    try {
        Write-Host "SignIt: $pfx"

        if (![IO.File]::Exists($signtool)) {
            write-host "`nERROR: signtool tool not found. Install WIndows SDK and update signtool.exe path in script.`n"
            exit 1
        }

        $pwd         = get-location
        $pass1_sec   = $null    
        $pass1_bstr  = $null
        $pass1_text  = $null


        # Creates a SelfSigned PFX Certificate and save it to current directory
        if (![IO.File]::Exists("$pwd/MySigniture.pfx")) {
            Write-Host "`n!!! Creating New SelfSignedCertficate !!!`n"

            $pass1_sec   = read-host "Password: " -AsSecureString   
            $pass1_bstr  = [Runtime.InteropServices.Marshal]::SecureStringToBSTR($pass1_sec)
            $pass1_text  = [Runtime.InteropServices.Marshal]::PtrToStringAuto($pass1_bstr)

            $pass2_sec   = read-host "Re-Enter Password: " -AsSecureString  
            $pass2_bstr  = [Runtime.InteropServices.Marshal]::SecureStringToBSTR($pass2_sec)
            $pass2_text  = [Runtime.InteropServices.Marshal]::PtrToStringAuto($pass2_bstr)

            if ($pass1_text -ceq $pass2_text) {
                Write-Host "Passwords matched"
            } 
            else {
                Write-Host "Passwords differ. Aborting script."
                exit 1
            }   

            $subject="CN=${subject_cn},O=${subject_o},E=${subject_e},C=${subject_c},ST=${subject_st}"

            $cert = New-SelfSignedCertificate `
                -Type Custom `
                -Subject $subject `
                -KeyUsage DigitalSignature `
                -CertStoreLocation $location `
                -FriendlyName $friendly_name

            $ThumbPrint = $cert.ThumbPrint
            $provider   = "${location}\${ThumbPrint}"
            $tmp        = Export-PfxCertificate `
                             -cert $provider  `
                             -FilePath $pfx `
                             -Password $pass1_sec

            del $provider
        }

        if ($pass1_sec -eq $null) {
            $pass1_sec   = read-host "Password: " -AsSecureString   
            $pass1_bstr  = [Runtime.InteropServices.Marshal]::SecureStringToBSTR($pass1_sec)
            $pass1_text  = [Runtime.InteropServices.Marshal]::PtrToStringAuto($pass1_bstr)
        }

        & $signtool sign  `
            /a `
            /t http://timestamp.verisign.com/scripts/timstamp.dll `
            /f $pfx `
            /p $pass1_text `
            /v `
            $exe
    }
    catch {
        write-host "ERROR: Error Signing Exe."
        throw
    }   
}

SignIt

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM