简体   繁体   中英

gnupg decrypt python

New to python and programming. Currently writing a little python 3 code to keep my Bank Account login details etc. I've had it working by opening and closing a file and using the out='filename' for gnupg module, however ideally i'd like it to write to a memory as I don't want the decrypted info on the disk if I can avoid it.

The file is created by another function in the program, which pickles a dictionary and encrypts it to a file. However when I try to decrypt and open it with this:

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

I get:

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

Same result on my Linux box. I've tried a bunch of stuff to make this work, no luck so far. Can anyone suggest a better way to do this? Basically I need to get the gpg.decrypt_file to output to a buffer or variable, and pickle.load to read it back to dictionary.

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)

with thanks to @senderle

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM