python和编程的新手。目前正在写一个小的python 3代码来保存我的银行账户登录详细信息等。我已经通过打开和关闭一个文件并使用out='filename‘作为gnupg模块,但是理想情况下我希望它写入内存,因为我不想在磁盘上解密的信息,如果我可以避免它。
该文件是由程序中的另一个函数创建的,该函数对字典进行pickles并将其加密到文件中。但是,当我尝试解密并使用以下命令打开它时:
def OpenDictionary():
""" open the dictionary file """
if os.path.isfile(SAVEFILE):
f = open(SAVEFILE, 'rb')
print('opening gpg file')
buf = io.BytesIO()
decrypted_data = gpg.decrypt_file(f, passphrase=PASSWORD)
buf.write(decrypted_data.data)
print ('ok: ', decrypted_data.ok)
print ('status: ', decrypted_data.status)
print ('stderr: ', decrypted_data.stderr)
f.close()
dictionary = pickle.load(buf)
buf.close()
return dictionary我得到了:
Traceback (most recent call last): File "C:\Users\Josh Harney\Dropbox\Python-pys\gpg-banks.py", line 179, in <module>
main(sys.argv[1:]) File "C:\Users\Josh Harney\Dropbox\Python-pys\gpg-banks.py", line 173, in main
dictionary = OpenDictionary() File "C:\Users\Josh Harney\Dropbox\Python-pys\gpg-banks.py", line 87, in OpenDictionary
dictionary = pickle.load(buf) EOFError在我的Linux机器上也有同样的结果。我已经尝试了很多东西来实现这个功能,但到目前为止还没有成功。有没有人能推荐一种更好的方法呢?基本上,我需要将gpg.decrypt_file输出到缓冲区或变量,并使用pickle.load将其读回字典。
发布于 2012-12-10 00:51:38
def OpenDictionary():
""" open the dictionary file """
try:
if os.path.isfile(SAVEFILE):
f = open(SAVEFILE, 'r')
enc_data = f.read()
print('opening gpg file')
decrypted_data = gpg.decrypt(enc_data, passphrase=PASSWORD)
print ('ok: ', decrypted_data.ok)
print ('status: ', decrypted_data.status)
print ('stderr: ', decrypted_data.stderr)
f.close()
dictionary = pickle.loads(decrypted_data.data)
return dictionary
except Exception as e:
print('Something Snaggy happened in the call OpenDictionary(), it looks like: ', e)
def SaveDictionary(dictionary):
savestr = pickle.dumps(dictionary)
status = gpg.encrypt(savestr, recipients=['my@email'])
if status.ok == True:
print('scrap buffer')
f = open(SAVEFILE, 'w')
f.write(str(status))
f.close()
else:
print('Something went wrong with the save!!!')
print ('ok: ', status.ok)
print ('status: ', status.status)
print ('stderr: ', status.stderr)感谢@senderle
https://stackoverflow.com/questions/13780830
复制相似问题