powershell 出口-Pfx证书:无法导出不可导出的私钥

z8dt9xmd  于 2023-08-05  发布在  Shell
关注(0)|答案(7)|浏览(447)

我试图导出我的自签名证书,以便将其导入到我的开发环境中的其他服务器(将使用“真实的”证书用于生产),但它引发以下错误:
出口-Pfx证书:无法导出不可导出的私钥
要求是我需要导出证书并“允许导出私钥”,但我很好奇我缺少了什么。我的PowerShell如下:

$pwd = ConvertTo-SecureString -String ‘1234’ -Force -AsPlainText
$path = 'cert:\localMachine\my\' + '1E7439053EE57AEE6EA0E1F3CDF5DB4234B6731E' 
Export-PfxCertificate -cert $path -FilePath c:\Certificates\cert.pfx -Password $pwd

字符串

mu0hgdu0

mu0hgdu01#

问题不在于powershell代码。证书有问题。
首次导入或创建证书时,必须将私钥标记为可导出,以便能够导出私钥。
您收到的错误消息表明,您尝试使用的证书上的私钥不可导出。
Example Issue

a5g8bdjr

a5g8bdjr2#

也许太晚了,但你有没有尝试以管理员身份运行PowerShell脚本?(如果您可以从mmc控制台导出私钥,Export-PfxCertificate也会导出私钥。)

r6l8ljro

r6l8ljro3#

我知道这是一个老问题,但我想张贴我的解决方案,因为我有这个同样的问题。我也得到了可怕的出口PfxCertificate:无法导出不可导出的私钥在尝试导出我的PFX文件时出错。在Windows计算机上加载代码签名证书后,问题开始出现。当我去导出它时,导出到PFX选项变灰了,没有进一步的解释。然后,我按照这里列出的许多说明进行操作,包括 Powershell Export-PfxCertificate。这些都不管用。我最后回到我的证书提供商GoDaddy,他们告诉我,在我的原始证书签名请求(CSR)中,我没有选中 * 使私钥可导出 *。GoDaddy慷慨地,免费地,允许我提交一个新的CSR(选中该选项)来“重新密钥”我现有的证书。几个小时后,我的新证书颁发了。我在我的机器上安装了它,并且能够直接从Windows MMC导出(不需要PowerShell)。我已经包括了在创建CSR时必须选中的框的这个屏幕截图(在不同的平台上可能看起来不同)。
x1c 0d1x的数据

ui7jx7zq

ui7jx7zq4#

我做了一个快速搜索,你可以使用certutil或更好的可能是从http://community.idera.com/powershell/powertips/b/tips/posts/exporting-certificate-with-private-key的解决方案。
该帖子的相关代码已粘贴在下面。100%归因于该页面的作者。

dir cert:\currentuser\my | 
Where-Object { $_.hasPrivateKey } | 
Foreach-Object { [system.IO.file]::WriteAllBytes(
"$home\$($_.thumbprint).pfx", 
($_.Export('PFX', 'secret')) ) }

字符串

vsmadaxz

vsmadaxz5#

将Import-PfxCertificate与参数-Exportable一起使用
Get-ChildItem -Path c:\mypfx\my.pfx| Import-PfxCertificate -CertStoreLocation证书:\CurrentUser\My -可导出

s4n0splo

s4n0splo6#

检查我的代码下面.

#Ask for the Name 
$name = Read-Host "Certificate Name "

# Check if the Path exists
$Path = "D:\Provisioning\certmgmt\$name.txt"
$TestPath = Test-Path $Path
if ($TestPath -ne "true")
{
    Write-Host "The Path $Path do not exist" -ForegroundColor Red
    Pause
    exit
}

# Import the certificate
$result = Import-Certificate -FilePath $Path -CertStoreLocation "Cert:\LocalMachine\My" 

# Get the serialnumber of the certificate
$Thumbprint = $result.Thumbprint

# Set the FriendlyName
(Get-ChildItem -Path Cert:\LocalMachine\My\$Thumbprint).FriendlyName = $name  

# Export the Certificate
$answer = Read-Host "Export Certificate? (Y/N)"

if ($answer -eq "N" -or $answer -eq "n")
{
    exit
}

    try
    {
       $mypwd = ConvertTo-SecureString -String "password" -Force -AsPlainText
       Get-ChildItem -Path cert:\localMachine\my\$Thumbprint | Export-PfxCertificate -FilePath C:\$name.pfx -Password $mypwd
    }
    catch
    {
      Write-Host $Error -ForegroundColor Red
      pause
      exit
    }

    Write-Host "Export the Certifikate was successful" -ForegroundColor Green

字符串

2uluyalo

2uluyalo7#

即使将证书标记为不可导出,仍可以从源服务器上的注册表导出证书,并将其重新导入目标服务器上的注册表。
首先,您需要证书的拇指指纹。(本题假设你有这个。)

#PowerShell5
$cert = Get-ChildItem -Path Cert:\ -Recurse | Where-Object Subject -Like '*example.com*'
$cert | Select-Object Subject, Thumbprint

字符串
假设指纹是1E7439053EE57AEE6EA0E1F3CDF5DB4234B6731E。现在导出此注册表项:

Computer\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\SystemCertificates\My\Certificates\1E7439053EE57AEE6EA0E1F3CDF5DB4234B6731E


并将其导入目标服务器。
来源:https://www.yuenx.com/2022/certificate-security-export-cert-with-non-exportable-private-key-marked-as-not-exportable-windows-pki/

相关问题