首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >gnupg解密python

gnupg解密python
EN

Stack Overflow用户
提问于 2012-12-09 03:01:45
回答 1查看 2.6K关注 0票数 1

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

该文件是由程序中的另一个函数创建的,该函数对字典进行pickles并将其加密到文件中。但是,当我尝试解密并使用以下命令打开它时:

代码语言:javascript
复制
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

我得到了:

代码语言:javascript
复制
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将其读回字典。

EN

回答 1

Stack Overflow用户

发布于 2012-12-10 00:51:38

代码语言:javascript
复制
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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13780830

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档