简体   繁体   English

如何使用 PowerShell 获取证书模数?

[英]How to Get Certificate Modulus Using PowerShell?

Given a *.cer file, how can I retrieve the modulus of the certificate's public key, in byte format, using only PowerShell?给定一个*.cer文件,如何仅使用 PowerShell 以字节格式检索证书公钥的模数?

I was able to perform the following actions in PowerShell to retrieve the public key modulus of a signing certificate.我能够在 PowerShell 中执行以下操作来检索签名证书的公钥模数。

# Load the cert into an object.
$cert = New-Object System.Security.Cryptography.X509Certificates.X509Certificate2("C:\Users\JaneDoe\Desktop\myCertificate.cer");

This gives us a certificate object that we can extract a lot of data from, such as the expiration date NotAfter , the thumbprint, and other info.这为我们提供了一个证书 object,我们可以从中提取大量数据,例如到期日期NotAfter 、指纹和其他信息。

# Calculate the public key modulus, and convert to hex format.
$modulus = ($cert.PublicKey.Key.ExportParameters($false)).Modulus | %{ ("{0:x}" -f $_).PadLeft(2, "0") };

This will retrieve the modulus from the public key, and pipe the results into a hex formatter.这将从公钥中检索模数,并将结果 pipe 转换为十六进制格式化程序。 You'll notice it also guarantees that each item in the collection will be padded with a leading zero if smaller than 0x10 16.您会注意到它还保证集合中的每个项目如果小于0x10 16,都将用前导零填充。

# Format display to look nice.
[Regex]::Replace(("0x00, 0x" + [string]::Join(", 0x", $modulus)), "(?<=^(.{102})+)", [Environment]::NewLine);

For my specific use case, I wanted each item in the collection to have a leading 0x for copy/paste purposes, and to have a newline after every 102 characters also for copy/paste purposes.对于我的特定用例,我希望集合中的每个项目都有一个前导0x用于复制/粘贴目的,并且每 102 个字符后有一个换行符也用于复制/粘贴目的。 You could ignore this last command if you don't need it.如果不需要,可以忽略最后一条命令。

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

相关问题 如何使用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 如何使用 Powershell 获取与服务主体关联的证书的指纹? - How to get thumbprint of the certificate associated with a service principal using Powershell? 如何使用Powershell获得证书的安全哈希算法 - How to get a security hash algorithm for a certificate using Powershell 如何使用Powershell导入证书 - How to import a certificate using powershell 如何使用Powershell 5创建具有2个密钥用法的证书 - How to create an certificate with 2 key usages using powershell 5 如何使用 PowerShell 申请可导出证书? - How to request a exportable certificate using PowerShell? 如何使用Powershell从PFX证书获取公钥? - How do I get the public key from a PFX certificate using Powershell? 微软图形; Powershell; 如何使用 invoke-restmethod 获取访问令牌:TenantID、ClientID 和 *certificate* - Microsoft Graph; Powershell; how to get access token using invoke-restmethod with: TenantID, ClientID and *certificate* 如何使用 PowerShell 获取域的最新 SSL 证书? - How can I get the most recent SSL certificate for a domain using PowerShell?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM