我正在尝试使用pycrypto创建一个简单的公钥 - 私钥加密算法:
from Crypto.PublicKey import RSA
from Crypto.Signature import PKCS1_v1_5
from Crypto.Hash import SHA256
from base64 import b64encode, b64decode
#Open a txt file in the host
f = open('to-drop-box.txt', 'rb')
#Save the contents of the file into a variable
message1 = f.read()
f.close()
data = message1
key = open("privateKey.der", "r").read()
rsakey = RSA.importKey(key)
signer = PKCS1_v1_5.new(rsakey)
digest = SHA256.new()
# It's being assumed the data is base64 encoded, so it's decoded before updating the digest
digest.update(b64decode(data))
sign = signer.sign(digest)
#return b64encode(sign)
signature = b64encode(sign)
但是我在 digest.update(b64decode(data))行中收到以下错误:
Traceback (most recent call last):
File "asymmetric-public-private-key-signature.py", line 33, in <module>
digest.update(b64decode(data))
File "/usr/lib/python2.7/base64.py", line 76, in b64decode
raise TypeError(msg)
TypeError: Incorrect padding
有人知道如何修复错误吗?
答案 0 :(得分:2)
to-drop-box.txt
不是base64编码的。 b64decode
抱怨填充(尾随=
),但错误通常表示字符串中出现非法字符。
答案 1 :(得分:1)
好的,我将行digest.update(b64decode(data))更改为digest.update(data),现在它可以工作了。