【问题标题】:Import p12 collection certificate with both PersistKeySet and Exportable options使用 PersistKeySet 和 Exportable 选项导入 p12 集合证书
【发布时间】:2016-10-02 14:47:58
【问题描述】:

我需要使用 PersistKeySet 和 Exportable 选项导入 p12 证书集合。获得只使用一个选项。 [System.Security.Cryptography.X509Certificates.X509KeyStorageFlags]::"Exportable, PersistKeySet") 不起作用。如何正确操作?

我的功能:

function ImportEASCert($strCertPath, $strCertPass)
{
    $fOk = Test-Path "$strCertPath"
    if ($fOk)
    {
        $bytes = [System.IO.File]::ReadAllBytes($strCertPath)
        $cert  = New-Object system.security.cryptography.x509certificates.X509Certificate2Collection
        $store = New-Object system.security.cryptography.X509Certificates.X509Store "My", "CurrentUser"

        try
        {
            $cert.Import($bytes, $strCertPass, [System.Security.Cryptography.X509Certificates.X509KeyStorageFlags]::MachineKeySet -bor [System.Security.Cryptography.X509Certificates.X509KeyStorageFlags]::PersistKeySet)
            $store.Open([System.Security.Cryptography.X509Certificates.OpenFlags]::ReadWrite)

            foreach ($D in $cert)
            {
                $store.Add($d)
                $d.SerialNumber
            }

            $store.Close()
        }
        catch
        {
            return "0"
        }
    }
    else
    {
        return "0"
    }
}

【问题讨论】:

  • 您的问题的答案已经在您的代码中。请仔细看看MachineKeySetPersistKeySet 标志是如何组合的。

标签: powershell x509certificate p12


【解决方案1】:

只需删除双冒号并在引号中输入标志:

[System.Security.Cryptography.X509Certificates.X509KeyStorageFlags]"Exportable, PersistKeySet"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-04-04
    • 2016-08-20
    • 1970-01-01
    • 2015-11-15
    • 2019-12-09
    • 2015-04-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多