“TypeError:错误填充”在pycrypto-2.6.1(python 2.7.6) ubuntu14.04 LTS中。

时间:2022-05-30 18:31:05

I'm trying to make a simple public-private key encryption algorithm using pycrypto:

我尝试用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) 

But I get the following error at the line 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

Does someone know how to fix the error?

有人知道如何修正错误吗?

2 个解决方案

#1


2  

to-drop-box.txt is not base64 encoded. b64decode is complaining about the padding (those trailing =) but the error usually signifies that illegal characters appear in the string.

to-drop-box。txt不是base64编码的。b64decode正在抱怨填充(那些尾随=),但是错误通常意味着非法字符出现在字符串中。

#2


1  

Ok, I changed the line digest.update(b64decode(data)) to digest.update(data) and now it works.

好的,我修改了行摘要。更新(b64decode(data))来消化。更新(数据),现在它起作用了。

#1


2  

to-drop-box.txt is not base64 encoded. b64decode is complaining about the padding (those trailing =) but the error usually signifies that illegal characters appear in the string.

to-drop-box。txt不是base64编码的。b64decode正在抱怨填充(那些尾随=),但是错误通常意味着非法字符出现在字符串中。

#2


1  

Ok, I changed the line digest.update(b64decode(data)) to digest.update(data) and now it works.

好的,我修改了行摘要。更新(b64decode(data))来消化。更新(数据),现在它起作用了。