我尝试将RSA与PyCrypto一起使用来加密/解密长字符串。我非常有信心我的代码适用于短字符串(大约200个字符),但对于比这更长的字符串,我无法成功解密。
这就是我所拥有的:
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])
这适用于字符串small_alphabet
,但不适用于alphabet
。那是为什么?
我正在使用PyCrypto 2.6.1,Python 2.7.6。