如何用python签署数字签名?

Posted

技术标签:

【中文标题】如何用python签署数字签名?【英文标题】:How to sign digital signature with python? 【发布时间】:2021-11-02 06:40:28 【问题描述】:

我正在使用 selenium 来自动化网站填充任务。但是,用户只能通过选择使用.p12证书登录网站。所以当我按下“使用数字签名登录”时,它会自动打开将.p12文件转换为XML并将数据发送到网站的软件。是否可以在不使用任何其他外部软件/应用程序的情况下完全在 python 中执行相同的过程?

【问题讨论】:

【参考方案1】:

如果您想尝试使用注册表编辑器并使用 ChromeDriver,请尝试:

1-) 按 Windwos + R 并输入“regedit”。

2-) Rigth 点击“Computer\HKEY_LOCAL_MACHINE\SOFTWARE\Policies”,选择新建 -> 密钥并输入名称“Google”,然后在此密钥中添加密钥“Chrome”并在此添加名为“ AutoSelectCertificateForUrls”。完整路径必须是“Computer\HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Google\Chrome\AutoSelectCertificateForUrls”。

3-) 右击 AutoSelectCertificateForUrls,新建 -> 字符串值,并输入名称“1”。

4-) 右击“1”,修改后复制粘贴这个 JSON “”pattern”:”https://urlFromTheSiteYouWantAccess”,”filter”:“ISSUER”:“CN”:“你的cn","SUBJECT":"CN":"你的证书 cn"".

我编辑答案是因为我犯了一个错误,在这里放了一个 Java 的例子。对不起那个 Uldana Duisenaly。 但现在我将输入一个代码用于 python 更改最终访问一个带有 python 和 selenium 的网站。 不要忘记以管理员身份运行应用程序。 进口。

from os import times
import winreg
from OpenSSL import crypto
from selenium import webdriver

开证方法:

def GetCertificate(pathOfCertificate, passwordOfCertifcate):
    pkcs12 = crypto.load_pkcs12(open(pathOfCertificate, 'rb').read(), passwordOfCertifcate)   
    return pkcs12.get_certificate()

更新注册表键“AutoSelectCertificateForUrls”中StringValue中Json的方法:

def UpdateStringValue(strigValueName,newValueOfStrinValue, stringValuePath):
  key = winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, stringValuePath, 0, winreg.KEY_ALL_ACCESS)
  winreg.SetValueEx(key, strigValueName, 0, winreg.REG_SZ, newValueOfStrinValue)
  winreg.CloseKey(key)

我为路径声明 2 个变量的主要部分是字符串值和字符串值的名称。在我的情况下是“1”:

pathOfstringValue = 'SOFTWARE\Policies\Google\Chrome\AutoSelectCertificateForUrls'
stringValueName = '1'

获取证书及 Issuer 和 Subject 信息:

certificate = GetCertificate("C:\yourCrtificatePath\\Certificate.pfx", 'certificatePassword')
subject = certificate.get_subject()
issuer = certificate.get_issuer()

第一个 URL 是发送证书的地方,另一个是我们有证书登录按钮的页面:

url_where_certificate_will_be_send = "https://notacarioca.rio.gov.br/"
url = 'https://notacarioca.rio.gov.br/senhaweb/login.aspx'

构建json,(对不起,处理字符串的方式很丑)并调用方法来编辑字符串值注册表:

json = '"pattern":"' + url_where_certificate_will_be_send + '","filter":"ISSUER":"CN":"' + issuer.CN + '","C":"' + issuer.C + '","O":"' + issuer.O + '","SUBJECT":"CN":"' + subject.CN + '","C":"' + subject.C + '","O":"' + subject.O + '"'
UpdateStringValue(stringValueName, json, pathOfstringValue)

最后是点击证书登录按钮的代码:

driver = webdriver.Chrome()

driver.get(url)

btn_certificate_login = driver.find_element_by_id('ctl00_cphCabMenu_imgLoginICP')
btn_certificate_login.click()
times.sleep(2.4)
driver.quit()

要下载代码,请访问我的 github here

【讨论】:

【参考方案2】:

一个字回答是不。

我想您正在尝试使用 selenium 进行网页抓取,并希望使用数字证书自动登录。浏览器在系统沙箱中运行,如果没有任何其他外部软件的帮助,则无法访问系统资源。

PKI Web Authentication using Digital Certificate and Browser Extension我开发的样本;还具有在引擎盖下运行的本机消息传递主机,它提供对系统证书存储(或智能卡)的访问。不过,它也可以在不打开证书选择弹出窗口(即外部组件)的情况下静默处理这些事情。需要浏览器扩展和本机主机。

【讨论】:

以上是关于如何用python签署数字签名?的主要内容,如果未能解决你的问题,请参考以下文章

如何用两个字符串生成一个唯一的数字

如何通过其他证书签署证书

如何为 Amazon CloudFront 签署 RTMP URL

编写驱动程序

如何为 Google Play 商店签署我的颤振应用程序?

如何用Python将十进制数字转为二进制,以及将二进制转为十六进制?