【问题标题】:Getting private keys to work on windows 7 powershell version 2让私钥在 Windows 7 powershell 版本 2 上工作
【发布时间】:2023-03-29 08:40:02
【问题描述】:

首先,我知道有人提出了非常相似的问题,但是通读了解决方案并修改了我自己的工作流程并没有产生正确的结果。

我希望通过显示我正在使用的代码,有人可以指出我哪里出错了。我正在尝试将我的 Windows 10 机器上的私有密钥导入安装了 powershell 版本 2 的旧版 Windows 7。

我面临的错误是 $cert.hasprivatekey 返回 true 但 $cert.privatekey 总是返回 null。然而,这也发生在 Windows 10 机器上,所以生成密钥似乎是个问题,但我发现的每个指南都使用相同的语法!

这是创建密钥的代码:

        $store = "cert:\CurrentUser\My"

    $params = @{
    CertStoreLocation = $store
    Subject = "CN=Test1"    
    KeyLength = 2048
    KeyAlgorithm = "RSA" 
    KeyUsage = "DataEncipherment"
    Type = "DocumentEncryptionCert"
    KeyExportPolicy = 'Exportable'
    }

    # generate new certificate and add it to certificate store
    $cert = New-SelfSignedCertificate @params

    Get-ChildItem -path $store

    $pwd = ("P@ssword" | ConvertTo-SecureString -AsPlainText -Force)
    $privateKey = "$home\Documents\Test1.pfx"
    $publicKey = "$home\Documents\Test1.cer"

    # Export private key as PFX certificate, to use those Keys on different machine/user
    Export-PfxCertificate -FilePath $privateKey -Cert $cert -Password $pwd

    # Export Public key, to share with other users
    Export-Certificate -FilePath $publicKey -Cert $cert

我也试过了,没用:

       $TestCertificate = New-SelfSignedCertificate -Subject 'TestCertificate' -KeyExportPolicy 'Exportable'
        Export-PfxCertificate -Cert $TestCertificate -FilePath .\TestCertificate.pfx -Password (ConvertTo-SecureString 'TestPassword' -AsPlainText -Force)

然后我尝试使用以下方法在 Windows 7 上导入私有 pfx 证书:

     $PfxFilePath = "C:\Users\user\Desktop\Test1.pfx"
    $pwd = ("P@ssword" | ConvertTo-SecureString -AsPlainText -Force)
    $Password = $pwd

    $absolutePfxFilePath = Resolve-Path -Path $PfxFilePath
    Write-Output "Importing store certificate '$absolutePfxFilePath'..."

     Add-Type -AssemblyName System.Security
     $cert = New-Object   System.Security.Cryptography.X509Certificates.X509Certificate2
    $cert.Import($absolutePfxFilePath, $Password, [System.Security.Cryptography.X509Certificates.X509KeyStorageFlags]"PersistKeySet")
    $Store = New-Object System.Security.Cryptography.X509Certificates.X509Store(
        [System.Security.Cryptography.X509Certificates.StoreName]::My, 
        [System.Security.Cryptography.X509Certificates.StoreLocation]::LocalMachine)
    $store.Open([System.Security.Cryptography.X509Certificates.OpenFlags]::"ReadWrite")
    $store.Add($cert)
    $store.Close()

欢迎所有想法!

【问题讨论】:

    标签: powershell x509certificate private-key


    【解决方案1】:

    密钥存储在完全过时的PrivateKey 属性不支持的密钥存储提供程序中。相反,您需要使用更新的语法来获取私钥:

    $key = [System.Security.Cryptography.X509Certificates.RSACertificateExtensions]::GetRSAPrivateKey($cert)
    

    【讨论】:

    • 嗨 Crypt32,谢谢你的回答。我对如何将其与我的原始代码相匹配以使其正常工作有点困惑。麻烦你举个例子好吗?我想我知道 $cert.hasprivatekey 或 $cert.privatekey 已经过时,应该从我的代码中删除它作为检查。那是对的吗?谢谢
    • 我在您的代码中没有看到任何私钥的使用情况。
    • 你是对的,但我正在使用 If($Cert.HasPrivateKey -eq $False -or $Cert.PrivateKey -eq $null){Write 测试我的设置脚本中是否有私钥-错误“错误}
    • 我应该在哪里使用您提供的 $key 变量来帮助完成这项工作?
    • 只检查HasPrivateKey 值就足以确定证书是否具有关联的私钥。
    猜你喜欢
    • 2017-02-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-17
    • 1970-01-01
    • 2012-10-11
    相关资源
    最近更新 更多