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對象?
您不得不序列化是這樣的對象背後的按鍵。 PyCrypto爲您提供了導出密鑰並導入密鑰的功能。你嘗試過那些嗎? –
@ArtjomB。我會嘗試,但我希望能夠將密碼序列化爲單個文件。你建議我只是序列化密鑰(使用PyCrypto的輸出,而不是pickle),然後通過導入密碼來重建密碼? – bkaiser