@Sir Kill A Lotの回答で提供されているのと同じ方法ですが、PowerShell スクリプト ( pfx2snk.ps1 ) に変換されています。
Param(
[Parameter(Mandatory=$True,Position=1)]
[string] $pfxFilePath,
[string] $pfxPassword
)
# The path to the snk file we're creating
[string] $snkFilePath = [IO.Path]::GetFileNameWithoutExtension($pfxFilePath) + ".snk";
# Read in the bytes of the pfx file
[byte[]] $pfxBytes = Get-Content $pfxFilePath -Encoding Byte;
# Get a cert object from the pfx bytes with the private key marked as exportable
$cert = New-Object System.Security.Cryptography.X509Certificates.X509Certificate2(
$pfxBytes,
$pfxPassword,
[Security.Cryptography.X509Certificates.X509KeyStorageFlags]::Exportable);
# Export a CSP blob from the cert (which is the same format as an SNK file)
[byte[]] $snkBytes = ([Security.Cryptography.RSACryptoServiceProvider]$cert.PrivateKey).ExportCspBlob($true);
# Write the CSP blob/SNK bytes to the snk file
[IO.File]::WriteAllBytes($snkFilePath, $snkBytes);
pfx ファイルのパスとパスワードを指定してそのスクリプトを実行するだけで、pfx ファイルと同じディレクトリに (拡張子以外は同じ名前で) snk ファイルが作成されます。
powershell.exe -File pfx2snk.ps1 -pfxFilePath cert.pfx -pfxPassword "pfx password"
または、pfx にパスワードがない場合 (恥、恥):
powershell.exe -File pfx2snk.ps1 cert.pfx
また、残念ながら PowerShell スクリプトの実行が許可されていない環境 (つまり、対話型の PowerShell セッションのみ) で作業している場合は、標準の cmd.exe コマンド ラインからこの醜いワンライナーを実行できます (必要に応じてファイル パスと pfx パスワードを置き換えます)。
powershell.exe -Command "[IO.File]::WriteAllBytes('SnkFilePath.snk', ([Security.Cryptography.RSACryptoServiceProvider](New-Object System.Security.Cryptography.X509Certificates.X509Certificate2((Get-Content 'PfxFilePath.pfx' -Encoding Byte), 'PfxPassword', [Security.Cryptography.X509Certificates.X509KeyStorageFlags]::Exportable)).PrivateKey).ExportCspBlob($true));"
私は実際に、このワンライナーを Visual Studio ビルド前プロセスの標準部分として使用して、Authenticode 署名証明書 (pfx ファイル) から同じキーを使用して厳密な名前の署名を行うプロセスを自動化しています。それは要件ではありませんが、それらが同じであることは私には理にかなっているように思え、それは私の強迫性障害の傾向を助長します.
(@ punkcoderが彼の回答で言及した強力な名前署名にpfxファイルを使用した「バグのある」経験があったため、元のpfxではなくsnkファイルを使用します)
また、興味がある場合は、Visual Studio でのビルド後のプロセスの一部として、次のようなものを用意して、Authenticode 署名をプロジェクト出力に追加します (「リリース」プロジェクト構成で)。
powershell.exe -Command "Set-AuthenticodeSignature -FilePath '$(TargetPath)' -Certificate '$(SolutionDir)MyCert.pfx' -TimestampServer http://timestamp.verisign.com/scripts/timstamp.dll -HashAlgorithm sha256;"