jjf
jjf

Reputation: 115

"TypeError: Incorrect padding" in pycrypto-2.6.1(python 2.7.6) ubuntu14.04 LTS

I'm trying to make a simple public-private key encryption algorithm using 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?

Upvotes: 0

Views: 2514

Answers (2)

jjf
jjf

Reputation: 115

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

Upvotes: 1

Stefano Sanfilippo
Stefano Sanfilippo

Reputation: 33046

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.

Upvotes: 2

Related Questions