【发布时间】:2019-01-05 05:11:09
【问题描述】:
我需要使用 RSA 公钥和私钥对消息进行签名和验证。接收方的 if verifier.verify(h, signature) 部分,每次都返回“Signature notauthentic”错误。尽管一切都是正确的。我究竟做错了什么?这个问题最可能的原因是什么?
我使用以下代码生成了密钥
from Crypto.Signature import PKCS1_v1_5
from Crypto.PublicKey import RSA
key = RSA.generate(1024)
private_key=key.exportKey()
public_key=key.publickey().exportKey()
在发件人处,
from Crypto.Signature import PKCS1_v1_5
from Crypto.Hash import SHA256
from Crypto.PublicKey import RSA
private_key="""Private key here
-----END RSA PRIVATE KEY-----"""
message = 'To be signed'
priv_key = RSA.importKey(private_key)
h = SHA256.new(message)
signature = PKCS1_v1_5.new(priv_key).sign(h)
f=open('sign.txt','w')
f.write(signature)
在接收器处,
from Crypto.Signature import PKCS1_v1_5
from Crypto.Hash import SHA256
from Crypto.PublicKey import RSA
from base64 import b64decode
public_key="""public key here"""
pub_key = RSA.importKey(public_key)
message = 'To be signed'
f=open('sign.txt')
sig=f.readlines()
signature=sig[0]
h = SHA256.new(message)
verifier = PKCS1_v1_5.new(pub_key)
if verifier.verify(h, signature):
print "The signature is authentic."
else:
print "The signature is not authentic."
我是 python 新手。任何帮助将不胜感激。谢谢
【问题讨论】:
标签: python-2.7 rsa verification public-key dsa