【发布时间】:2014-01-25 06:36:33
【问题描述】:
我在安全方面没有太多经验,但现在我必须在 python 中实现一个签名程序。
我有一个证书somename.cer。我有一个c# 实现示例,说明如何使用该字符串对我的字符串进行签名,如下所示:
CertColl 是证书的集合,相关代码在前几行中找到带有Thumbprint 的相关证书并返回证书列表。
X509Certificate2 cert = certColl[0]
RSACryptoServiceProvider rsa = (RSACryptoServiceProvider)cert.PrivateKey;
return Convert.ToBase64String(rsa.SignData(Encoding.GetEncoding(1251).GetBytes(my_string), new SHA1CryptoServiceProvider()));
my_string是要在代码中签名和构造的字符串,但我不需要在此处添加这些步骤
所以我试图在this previous Q&A 的帮助下在 Python 中实现这一点
from Crypto.Util.asn1 import DerSequence
from Crypto.PublicKey import RSA
from binascii import a2b_base64
pem = open("some-path/somefile.cer") # I have a certificate with `cer` extension
lines = pem.replace(" ",'').split()
der = a2b_base64(''.join(lines[1:-1]))
cert = DerSequence()
cert.decode(der)
tbsCertificate = DerSequence()
tbsCertificate.decode(cert[0])
subjectPublicKeyInfo = tbsCertificate[6]
rsa_key = RSA.importKey(subjectPublicKeyInfo)
正如我所料,现在我可以用这个签名my_string。
rsa_key.sign("Hello World", "")
但我收到以下错误:
TypeError:此对象中的私钥不可用
我是不是做错了什么,比如用错误的方法在 python 中模仿rsa.SignData?
【问题讨论】:
标签: c# python encryption x509certificate signing