2016-08-23 50 views
0

我与pyCrpyto的RSA类工作:PyCrypto RSA和泡菜

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) 

该代码运行正常,而且我能够解密密文。但是,我需要能够序列化这些密码。我没有任何问题pickle -ing其他pyCrypto密码,如AES,但是当我尝试pickle的RSA密码我遇到了以下错误:

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 

有什么我可以做的来解决这个问题 - 另一个序列化框架,RSA对象的不同构造方法等等,还是仅仅是一个un-pickle -able对象?

+0

您不得不序列化是这样的对象背后的按键。 PyCrypto为您提供了导出密钥并导入密钥的功能。你尝试过那些吗? –

+0

@ArtjomB。我会尝试,但我希望能够将密码序列化为单个文件。你建议我只是序列化密钥(使用PyCrypto的输出,而不是pickle),然后通过导入密码来重建密码? – bkaiser

回答