【问题标题】:gnupg decrypt pythongnupg解密python
【发布时间】:2012-12-08 19:01:45
【问题描述】:

python 和编程新手。目前正在编写一些 python 3 代码来保存我的银行帐户登录详细信息等。我已经通过打开和关闭一个文件并使用 out='filename' 来实现它的工作 gnupg 模块,但理想情况下我希望它写入记忆,因为如果我可以避免的话,我不希望磁盘上的解密信息。

该文件是由程序中的另一个函数创建的,该函数提取字典并将其加密为文件。但是,当我尝试用这个解密并打开它时:

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 将其读回字典。

【问题讨论】:

  • 仅根据python-gnupg 的文档,您应该使用str(decrypted_data) 而不是decrypted_data.data。此外,您可以直接使用pickle.loads 将字符串转换为对象,而不是通过BytesIO 对象。 (您可以在加密端使用pickle.dumps。)希望复杂性的降低能够解决这里发生的任何问题。
  • 感谢您的回复。如果我这样做,则切换到 stringIO 作为缓冲区,并且 str(gpg.decrypt_file(f, passphrase=PASSWORD)) 我得到:TypeError: '_io.StringIO' 不支持缓冲区接口。我认为 gpg.decrypt_file 返回对象 Crypt,因此能够使用属性类型 .ok、.status 和 .stderr 以及 .data。
  • 我想你误会了。完全删除所有伪文件(StringIO 和 BytesIO);你不需要它们。使用pickle.dumps 生成字符串;使用gpg.encrypt 加密字符串,将加密的字符串保存到文件中。然后将加密文件加载到字符串中,将其解密为字符串(使用str(gpg.decrypt(data)))并将该字符串传递给pickle.loads。完全不涉及StringIO/BytesIO 对象。
  • 如果你只是想玩 Python,无论如何,继续;但是,如果您确实这样做是为了方便保存和检索银行账户信息等私人数据,我建议您寻找已经存在的 gpg 前端。你可能会喜欢pyrite
  • @ryran,是的,我现在只是在玩 python!不过我会看看黄铁矿,它看起来很有趣。我已经重写了字符串的两个函数,仍然没有运气。

标签: python python-3.x pickle gnupg


【解决方案1】:
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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-08-10
    • 1970-01-01
    • 2014-05-11
    • 1970-01-01
    • 2018-11-28
    • 2014-09-19
    • 1970-01-01
    相关资源
    最近更新 更多