【问题标题】:How to Export Certificate on Windows in Python如何用 Python 在 Windows 上导出证书
【发布时间】:2019-04-12 01:26:17
【问题描述】:

我正在编写一个 Python 程序,该程序需要从 Windows 上的证书存储中导出证书。我已经尝试搜索执行此操作的代码 sn-p,但我无法找到执行此操作的代码。这里重要的是我需要从属于机器和当前用户的证书存储中导出带有私钥的证书。

我的目标是使用证书对 Azure Key Vault 进行身份验证。根据接受的答案,无法从 Windows 上的证书存储中检索证书。相反,我决定编写一个 C# 应用程序来对 Azure Key Vault 进行身份验证并将机密传递给 Python 应用程序。

【问题讨论】:

    标签: python x509 certificate-store


    【解决方案1】:

    您可以向 powershell 发送子进程调用以从证书存储中导出证书。此脚本提示输入用户密码,然后将具有私钥的用户和本地计算机证书存储中的证书导出为 .pfx 文件。

    import subprocess
    import getpass
    
    pw = getpass.getpass()
    
    proc = subprocess.Popen(
        [
            'powershell.exe',
            '-c',
            f'&{{ $pw = ConvertTo-SecureString -String "{pw}" -Force -AsPlainText;',
            'gci Cert:\\*\\My\\* |',
            '?{ $_.HasPrivateKey } |',
            '%{ Export-PfxCertificate -cert $_.PSPath',
            '-FilePath $env:USERPROFILE\\$($_.thumbprint).pfx -Password $pw}',
            '}'
        ],
        stdout = subprocess.PIPE,
        stderr = subprocess.PIPE,
        shell = True
    )
    
    out, err = proc.communicate()
    

    【讨论】:

    • 我猜没有办法在 Python 中本地导出证书。我喜欢你的回答,一个建议是将其重写为一个接受指纹和密码的函数。此外,数组中的第三个元素需要第一个打开的大括号进行转义。
    猜你喜欢
    • 2013-03-29
    • 2014-11-14
    • 2010-09-22
    • 2011-03-14
    • 2019-01-24
    • 2019-10-11
    • 1970-01-01
    • 2014-11-10
    • 1970-01-01
    相关资源
    最近更新 更多