【问题标题】:Export-PfxCertificate : Cannot export non-exportable private keyExport-PfxCertificate:无法导出不可导出的私钥
【发布时间】:2018-02-21 20:56:40
【问题描述】:

我正在尝试导出我的自签名证书,以便将其导入我的开发环境中的其他服务器(将使用“真实”证书进行生产),但它会引发以下错误:

Export-PfxCertificate : 无法导出不可导出的私钥

要求是我需要导出证书并“允许导出私钥”,但很好奇我缺少什么。我的PowerShell如下:

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

【问题讨论】:

标签: powershell ssl-certificate


【解决方案1】:

问题不在于 powershell 代码。问题出在证书上。

首次导入或创建证书时,必须将私钥标记为可导出才能导出私钥。

您收到的错误消息表明您尝试使用的证书上的私钥不可导出。

Example Issue

【讨论】:

  • 示例链接已损坏。
【解决方案2】:

也许为时已晚,但您是否尝试过以管理员身份运行 PowerShell 脚本? (如果您可以从 mmc 控制台导出私钥,Export-PfxCertificate 也会将其导出。)

【讨论】:

  • 您无需成为管理员即可导出您的密钥,但您需要有可用的密钥才能导出它们。
  • 如果我以用户身份启动脚本,我只能从 \LocalMachine\My 导出公钥。此外,Export-PfxCertificate 返回错误“无法导出不可导出的私钥”。但是,如果我从提升的命令提示符启动 PS,则不会出现错误,并且 PFX 文件包含 pub 和 priv 密钥。
  • 如果你可以在certlm.msc中手动导出证书,因为它被标记为可导出,但是你在PowerShell中仍然出现这个错误,那么这就是解决方案。例如,如果您未提升权限,则无法导出 IIS 证书。
【解决方案3】:

我知道这是一个较老的问题,但我想发布我的解决方案,因为我遇到了同样的问题。 在尝试导出我的 PFX 文件时,我也遇到了可怕的 Export-PfxCertificate : Cannot export non-exportable private key 错误。 在我的 Windows 机器上加载我的代码签名证书后,问题就开始了。 当我去导出它时,导出到 PFX 选项是灰色的,没有进一步的解释。然后我按照此处列出的许多说明进行操作,包括 Powershell Export-PfxCertificate。这些都不起作用。 我终于回到了我的证书提供商 GoDaddy,他们告诉我,在我的原始证书签名请求 (CSR) 中,我没有选中 Make Private Key Exportable 框。 GoDaddy 慷慨且免费地允许我提交新的 CSR(选中该选项)以“重新加密”我现有的证书。几个小时后,我的新证书就颁发了。我将它安装在我的机器上,并且能够直接从 Windows MMC 导出(无需 PowerShell。) 我已经包含了创建 CSR 时必须选中的框的屏幕截图(在不同平台上可能看起来不同。)

【讨论】:

    【解决方案4】:

    我快速搜索了一下,你可以使用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')) ) }
    

    【讨论】:

      【解决方案5】:

      使用Import-PfxCertificate with parameter -Exportable

      Get-ChildItem -路径 c:\mypfx\my.pfx | Import-PfxCertificate -CertStoreLocation Cert:\CurrentUser\My -Exportable

      【讨论】:

        【解决方案6】:

        在下面查看我的代码。

        #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
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2011-04-24
          • 1970-01-01
          • 2019-01-09
          • 1970-01-01
          • 1970-01-01
          • 2011-07-19
          • 2012-02-18
          • 1970-01-01
          相关资源
          最近更新 更多