-2

我需要使用 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"
    }
}
4

1 回答 1

0

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

[System.Security.Cryptography.X509Certificates.X509KeyStorageFlags]"Exportable, PersistKeySet"
于 2016-10-02T16:02:48.057 回答