编程语言
首页 > 编程语言> > python – M2crypto签名“算法”

python – M2crypto签名“算法”

作者:互联网

这两个代码提供相同的签名,这是预期的:

代码1:

from M2Crypto import RSA, EVP
import base64, hashlib

text = "some text"

pkey = EVP.load_key("mykey.pem")  #"mykey.pem" was generated as: openssl genrsa -des3 -out mykey.pem 2048
pkey.sign_init()
pkey.sign_update(text)
signature = pkey.sign_final()
print base64.b64encode(signature)

码2:

pkey = RSA.load_key("mykey.pem")
signature = pkey.sign(hashlib.sha1(text).digest())
print base64.b64encode(signature)

但是,如果我想“模仿”签名算法,即用私钥加密摘要,我得到一个不同的签名,即:

pkey = RSA.load_key("mykey.pem")
signature = pkey.private_encrypt(hashlib.sha1(text).digest(), RSA.pkcs1_padding)
print base64.b64encode(signature)  #different from the two above

你能提供一些解释吗?后一种签署方式有什么问题?

解决方法:

我认为不同之处在于RSA_sign将摘要PKCS1 algorithmIdentifier与摘要数据一起签名,其中RSA_private_encrypt仅对摘要数据进行签名.

RSA_private_encrypt手册页:

RSA_PKCS1_PADDING
    PKCS #1 v1.5 padding. This function does not handle the
    algorithmIdentifier specified in PKCS #1. When generating or
    verifying PKCS #1 signatures, RSA_sign(3) and RSA_verify(3) should
    be used.

标签:python,digital-signature,m2crypto
来源: https://codeday.me/bug/20190630/1339442.html