簡體   English   中英

如何使用Powershell從PFX證書獲取公鑰?

[英]How do I get the public key from a PFX certificate using Powershell?

我試圖使用Powershell從證書中提取公鑰。 但是,每當我使用Powershell命令Get-PfxCertificate ,它只輸出證書的指紋而不是公鑰。 如何讓它輸出公鑰?

要使用Powershell從PFX證書檢索公鑰,請使用以下命令:

(Get-PfxCertificate -FilePath mycert.pfx).GetPublicKey()

要將公鑰轉換為不帶連字符的十六進制字符串,可以使用以下命令:

[System.BitConverter]::ToString((Get-PfxCertificate -FilePath mycert.pfx).GetPublicKey()).Replace("-", "")

請注意, Get-PfxCertificate將您的私鑰臨時存儲在%ProgramData%\\Microsoft\\Crypto\\RSA\\MachineKeys每個人都可以閱讀它。

如果這不是一個理想的行為,那么你應該使用X509Certificate2 .NET對象的import方法或構造函數與EphemeralKeySet作為密鑰存儲標志,這表明私鑰應該在內存中創建,而不是在導入時保留在磁盤上證書,例如:

$Cert = New-Object -TypeName System.Security.Cryptography.X509Certificates.X509Certificate2
$FullPathToCert = Resolve-Path -Path .\cert.pfx
$Password = Read-Host 'Password' -AsSecureString
$X509KeyStorageFlag = 32
$Cert.Import($FullPathToCert, $Password, $X509KeyStorageFlag)
$Cert.GetPublicKey()

筆記

  • .NET Framework 4.7.2.NET Core 2.0及更高版本支持的EphemeralKeySet標志;
  • $X509KeyStorageFlag = 32只是$X509KeyStorageFlag = [System.Security.Cryptography.X509Certificates.X509KeyStorageFlags]::EphemeralKeySet的簡寫

進一步閱讀

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM