簡體   English   中英

如何使用 PowerShell 獲取證書模數?

[英]How to Get Certificate Modulus Using PowerShell?

給定一個*.cer文件,如何僅使用 PowerShell 以字節格式檢索證書公鑰的模數?

我能夠在 PowerShell 中執行以下操作來檢索簽名證書的公鑰模數。

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

這為我們提供了一個證書 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") };

這將從公鑰中檢索模數,並將結果 pipe 轉換為十六進制格式化程序。 您會注意到它還保證集合中的每個項目如果小於0x10 16,都將用前導零填充。

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

對於我的特定用例,我希望集合中的每個項目都有一個前導0x用於復制/粘貼目的,並且每 102 個字符后有一個換行符也用於復制/粘貼目的。 如果不需要,可以忽略最后一條命令。

暫無
暫無

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

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