Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
gnupg解密python_Python_Python 3.x_Pickle_Gnupg - Fatal编程技术网

gnupg解密python

gnupg解密python,python,python-3.x,pickle,gnupg,Python,Python 3.x,Pickle,Gnupg,python和编程新手。目前正在编写一个小python 3代码来保存我的银行帐户登录详细信息等。我已经通过打开和关闭一个文件并为gnupg模块使用out='filename'实现了它,但是理想情况下,我希望它写入内存,因为如果可以避免的话,我不希望磁盘上的解密信息 该文件是由程序中的另一个函数创建的,该函数对字典进行pickle处理并将其加密为文件。但是,当我尝试用以下方法解密和打开它时: def OpenDictionary(): """ open the dictionary fil

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

该文件是由程序中的另一个函数创建的,该函数对字典进行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
我得到:

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
Traceback(最近一次调用last):文件“C:\Users\Josh Harney\Dropbox\Python pys\gpg banks.py”,第179行,在
main(sys.argv[1:])文件“C:\Users\Josh Harney\Dropbox\Python pys\gpg banks.py”,第173行,在main中
dictionary=OpenDictionary()文件“C:\Users\Josh Harney\Dropbox\Python pys\gpg banks.py”,第87行,在OpenDictionary中
字典=pickle.load(buf)EOFError
同样的结果在我的Linux机器上。我尝试了很多方法来实现这一目标,但到目前为止运气不佳。有人能提出更好的方法吗?基本上,我需要将gpg.decrypt_文件输出到缓冲区或变量,然后pickle.load将其读回字典

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

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