gnupg解密python

2024-05-17 10:12:14 发布

您现在位置:Python中文网/ 问答频道 /正文

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

该文件是由程序中的另一个函数创建的,该函数将词典进行pickle并将其加密到一个文件中。但是当我试图解密并打开它时:

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

我得到:

^{pr2}$

同样的结果在我的Linux机器上。我已经试了很多方法来完成这个任务,但到目前为止还没有成功。有谁能提出更好的方法吗?基本上我需要gpg.decrypt_文件输出到缓冲区或变量,以及酸洗。装载把它读回字典。在


Tags: 文件函数datadictionarystatusokopengpg
1条回答
网友
1楼 · 发布于 2024-05-17 10:12:14
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

相关问题 更多 >