2017-12-11 5 views
0

Visual Studio를 설치하지 않고 Windows 10과 함께 배포 된 csc.exe를 사용하여 C# 응용 프로그램을 컴파일한다고 가정 해 봅시다.이 응용 프로그램에 어떻게 자체 서명합니까? 내가 뽑을 수있는 최선의 방법은 아래에 게시 된이 미친 파워 쉘 스크립트입니다 ... 저보다 그 어플리케이션에 서명하기가 더 쉬워 보입니다.SharpDeveloper 또는 내장 csc.exe로 생성 된 .NET exe에 자체 서명하는 방법?

+0

서명이 쉽고, 'sn.exe'를 사용하십시오. 문제는 인증서에 대한 것입니다. – Crowcoder

답변

1
# 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  = "[email protected]" #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 
관련 문제