powershell - 使用 PowerShell 从证书中检索 KeySpec 值
问题描述
我正在尝试验证机器商店中的证书已KeySpec
设置为AT_KEYEXCHANGE
. Usingcertutil.exe
确实提供了此信息,但需要进行字符串解析。我宁愿避免字符串解析,以避免对certutil.exe
我不知道的输出假设在不同版本的 Windows 中总是正确的。
我查看了 和 的属性和System.Security.Cryptography.X509Certificates.X509Certificate2
方法System.Security.Cryptography.X509Certificates.RSACertificateExtensions
。
如何从证书存储中的证书中检索 KeySpec?
解决方案
在此处和此处的帮助下,我能够找到 KeySpec 。CspKeyContainerInfo 类包含一个名为 KeyNumber 的属性,这就是 certutil 所指的 KeySpec。
我找到了两种方法。一种仅适用于 PowerShell 5,另一种适用于 PowerShell 5 和 7。
仅限 PowerShell 5
$Cert = (Get-ChildItem -Path Cert:\LocalMachine\My)[1]
$Cert.PrivateKey.CspKeyContainerInfo.KeyNumber
PowerShell 5 和 7
$Cert = (Get-ChildItem -Path Cert:\LocalMachine\My)[1]
$PrivateKey = [System.Security.Cryptography.X509Certificates.RSACertificateExtensions]::GetRSAPrivateKey($Cert)
$CngProvider = [System.Security.Cryptography.CngProvider]::new($PrivateKey.Key.Provider)
$CngKey = [System.Security.Cryptography.CngKey]::Open($PrivateKey.Key.KeyName, $CngProvider, [System.Security.Cryptography.CngKeyOpenOptions]::MachineKey)
$CspParameters = [System.Security.Cryptography.CspParameters]::New(1, $CngKey.Provider, $CngKey.KeyName)
$CspParameters.Flags = [System.Security.Cryptography.CspProviderFlags]::UseMachineKeyStore
$CspKeyContainerInfo = [System.Security.Cryptography.CspKeyContainerInfo]::New($CspParameters)
$CspKeyContainerInfo.KeyNumber
推荐阅读
- c# - 为什么这个 WaitLock 卡住了?
- python - 在我的寡妇 7 专业机器上安装 python 时遇到问题
- python - 必须以管理员-python 运行
- node.js - 从 MongoDB 聚合函数接收的数组不可迭代
- python - python中的双重调用
- javascript - 如何使用 JS/CSS 将数字设置为 PRE
- sql - 当没有相关记录时,无法使用日期过滤器显示记录
- java - 是否可以根据实际类中的局部变量为方法调用返回模拟对象?
- python-3.x - Frozen Issue while opening an existing file
- python - BrowserMob Proxy with Python - The Browsermob-Proxy server process failed to start - Seeing Operation not permitted in server.log