I'm working with pyCrpyto's RSA class:
from Crypto.Cipher import PKCS1_v1_5
from Crypto.PublicKey import RSA
message = 'To be encrypted'
key = RSA.generate(2048)
cipher = PKCS1_v1_5.new(key)
ciphertext = cipher.encrypt(message)
That code runs fine, and I'm able to decrypt the ciphertext. However, I need to be able to serialize these ciphers. I haven't had any problem pickle
-ing other pyCrypto ciphers, like AES, but when I try to pickle
an RSA cipher I run into the following error:
from Crypto.Cipher import PKCS1_v1_5
from Crypto.PublicKey import RSA
import pickle
message = 'To be encrypted'
key = RSA.generate(2048)
cipher = PKCS1_v1_5.new(key)
pickle.dump(cipher, open("cipher.temp", "wb"))
cipher = pickle.load(open("cipher.temp", "rb"))
ciphertext = cipher.encrypt(message)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/Crypto/Cipher/PKCS1_v1_5.py", line 119, in encrypt
randFunc = self._key._randfunc
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/Crypto/PublicKey/RSA.py", line 126, in __getattr__
raise AttributeError("%s object has no %r attribute" % (self.__class__.__name__, attrname,))
AttributeError: _RSAobj object has no '_randfunc' attribute
Is there anything I can do to get around this -- another serialization framework, a different construction method for the RSA object, etc., or is this just an un-pickle
-able object?