I'm trying to use RSA with PyCrypto to encrypt/decrypt a long string. I am quite confident that my code works for short strings (to about 200 characters), but for strings longer than that, I cannot successfully decrypt.
This is what I have:
random_generator = Random.new().read
def encrypt_RSA(message):
key = open('public_key.der', "r").read()
rsakey = RSA.importKey(key)
encrypted = rsakey.encrypt(message, random_generator)
return encrypted
def decrypt_RSA(package):
key = open('private_key.der', "r").read()
rsakey = RSA.importKey(key)
decrypted = rsakey.decrypt(package)
return decrypted
alphabet = 'abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz'
small_alphabet = 'abcdefghijklmnopqrstuvwxyz'
encrypted = encrypt_RSA(small_alphabet)
print base64.b64encode(encrypted[0])
print "========================"
print decrypt_RSA(encrypted[0])
This works for the string small_alphabet
, but not alphabet
. Why is that?
I'm using PyCrypto 2.6.1, Python 2.7.6.