[英]How to use a client certificate from the Windows certificate store in python?
[英]How to Export Certificate on Windows in Python
我正在編寫一個Python程序,該程序需要從Windows上的證書存儲中導出證書。 我已經嘗試搜索可以做到這一點的代碼片段,但是我很難找到一個能做到這一點的代碼。 這里重要的是,我需要從屬於計算機和當前用戶的證書存儲中導出帶有私鑰的證書。
=編輯以添加更多上下文=
我的目標是使用證書對Azure Key Vault進行身份驗證。 根據接受的答案,無法從Windows上的證書庫中檢索證書。 相反,我決定編寫一個C#應用程序以對Azure Key Vault進行身份驗證並將機密傳遞給Python應用程序。
您可以向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()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.