【问题标题】:New-SelfSignedCertificate does not generate an exportable keyNew-SelfSignedCertificate 不生成可导出密钥
【发布时间】:2020-09-01 13:25:52
【问题描述】:

我正在尝试使用 New-SelfSignedCertificate cmdlet 生成可导出密钥,但我不能。

这是我的代码:

$certFilePath = "C:\certs"
$certStoreLocation = "Cert:\LocalMachine\My"
$pwd = "p@ssw0rd"

$cert = New-SelfSignedCertificate `
    -KeyFriendlyName "Development Cert" `
    -KeyDescription "Development Cert" `
    -KeyAlgorithm "RSA" `
    -DnsName @("*.dev.local", "localhost") `
    -NotBefore (Get-Date).AddYears(-1) `
    -NotAfter (Get-Date).AddYears(50) `
    -KeyUsage CertSign, CRLSign, DataEncipherment, DigitalSignature, NonRepudiation `
    -KeyUsageProperty All `
    -KeyLength 2048 `
    -KeyLocation $certFilePath `
    -CertStoreLocation $certStoreLocation `
    -KeyExportPolicy Exportable `
    -KeyProtection None `
    -Type Custom

$certThumb = $cert.Thumbprint
$certPath = "$certStoreLocation\$certThumb"
$cert | Export-PfxCertificate -FilePath "$certFilePath\$certThumb.pfx" -Password (ConvertTo-SecureString -String $pwd -AsPlainText -Force)
Export-PfxCertificate : Cannot export non-exportable private key.
At line:24 char:9
+ $cert | Export-PfxCertificate -FilePath "$certFilePath\$certThumb.pfx ...
+         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Export-PfxCertificate], Win32Exception
    + FullyQualifiedErrorId : System.ComponentModel.Win32Exception,Microsoft.CertificateServices.Commands.ExportPfxCertificate

请注意,我专门告诉它生成一个可导出的密钥,但它没有。

有什么想法吗?

【问题讨论】:

  • 从您的代码中删除 -KeyLocation 标志似乎可以解决您的问题?那里可能有什么问题?
  • @ITM 确实如此。似乎这个论点抵消了其他一些论点。

标签: powershell ssl-certificate pfx


【解决方案1】:

删除上面写着的部分:

-KeyLocation $certFilePath `

...解决问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-02-25
    • 2011-02-10
    • 2017-11-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-22
    相关资源
    最近更新 更多