[英]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()
EphemeralKeySet
標志; $X509KeyStorageFlag = 32
只是$X509KeyStorageFlag = [System.Security.Cryptography.X509Certificates.X509KeyStorageFlags]::EphemeralKeySet
的簡寫 X509KeyStorageFlags
枚舉器規范https://docs.microsoft.com/en-us/dotnet/api/system.security.cryptography.x509certificates.x509keystorageflags
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.