gpt4 book ai didi

python - M2crypto 签名 "algorithm"

转载 作者:太空狗 更新时间:2023-10-30 01:17:18 24 4
gpt4 key购买 nike

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

代码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 - M2crypto 签名 "algorithm",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5663084/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com