简体   繁体   English

如何使用Powershell从PFX证书获取公钥?

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

I am trying to extract the public key from a certificate using Powershell. 我试图使用Powershell从证书中提取公钥。 However, whenever I use the Powershell command Get-PfxCertificate it only outputs the Thumbprint of the certificate and not the public key. 但是,每当我使用Powershell命令Get-PfxCertificate ,它只输出证书的指纹而不是公钥。 How can I get it to output the public key? 如何让它输出公钥?

To retrieve the public key from a PFX certificate using Powershell, use the following command: 要使用Powershell从PFX证书检索公钥,请使用以下命令:

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

To convert the public key to a hex string without hyphens you can use this command: 要将公钥转换为不带连字符的十六进制字符串,可以使用以下命令:

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

Be aware, that Get-PfxCertificate temporarily stores your private key at %ProgramData%\\Microsoft\\Crypto\\RSA\\MachineKeys , where everyone could read it. 请注意, Get-PfxCertificate将您的私钥临时存储在%ProgramData%\\Microsoft\\Crypto\\RSA\\MachineKeys每个人都可以阅读它。

If that is not a desirable behavior, than you should probably use either import method or constructor of the X509Certificate2 .NET object with EphemeralKeySet as a key storage flag, which indicates that private key should be created in memory and not persisted on disk when importing a certificate, eg: 如果这不是一个理想的行为,那么你应该使用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()

Notes 笔记

  • EphemeralKeySet flag supported by .NET Framework 4.7.2 , .NET Core 2.0 and above; .NET Framework 4.7.2.NET Core 2.0及更高版本支持的EphemeralKeySet标志;
  • $X509KeyStorageFlag = 32 is just a shorthand for the $X509KeyStorageFlag = [System.Security.Cryptography.X509Certificates.X509KeyStorageFlags]::EphemeralKeySet $X509KeyStorageFlag = 32只是$X509KeyStorageFlag = [System.Security.Cryptography.X509Certificates.X509KeyStorageFlags]::EphemeralKeySet的简写

Further reading 进一步阅读

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

相关问题 PowerShell - 使用公钥读取证书颁发者 - PowerShell - Read Certificate Issuer using public key PowerShell 使用密码 PFX 文件获取证书指纹 - PowerShell Get Certificate Thumbprint with Password PFX File 如何从天青自动化的证书中获取公钥 - how to get public key from certificate on azure automation Azure - Key Vault 中的应用服务证书为空白 - 无法使用 powershell / az cli 下载 pfx - Azure - App Service Certificate in Key Vault is blank - Unable to download the pfx using powershell / az cli 如何使用Powershell和证书身份验证获取Azure Key Vault机密? - How to get Azure Key Vault secret using powershell with certificate auth? 如何使用 powershell 获取 Azure 密钥保管库证书激活日期 - How to Get the Azure key vault certificate activation date using powershell 如何在 windows powershell 中获取 base64 编码 pfx 证书的指纹 - How to get the fingerprint of a base64 encoded pfx certificate in windows powershell 如何从使用 PowerShell 自签名的 web 服务器下载证书? - How do I download the certificate from a web server that is self signed using PowerShell? 从 pfx 文件或证书存储中提取私钥,而无需在 Windows 上使用 OpenSSL - Extract private key from pfx file or certificate store WITHOUT using OpenSSL on Windows 如何使用Powershell 5创建具有2个密钥用法的证书 - How to create an certificate with 2 key usages using powershell 5
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM