2012-12-08 46 views
1

python和編程新手。目前正在編寫一個小小的python 3代碼來保存我的銀行帳戶登錄信息等等。我已經通過打開和關閉文件並使用out ='filename'作爲gnupg模塊來工作,但理想情況下我希望它寫入一個內存,因爲我不想在磁盤上解密的信息,如果我可以避免它。gnupg解密python

該文件是由程序中的另一個函數創建的,該程序將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 

同樣的結果在我的Linux機器。我已經嘗試了一些東西來完成這項工作,至今沒有運氣。任何人都可以提出一個更好的方法來做到這一點基本上我需要得到gpg.decrypt_file輸出到緩衝區或變量,pickle.load將其讀回字典。

+1

正是基於文檔來'蟒蛇-gnupg'上,你應該使用['STR(decrypted_data)'](http://packages.python.org/python -gnupg /#解密)而不是'decrypted_data.data'。此外,您可以使用['pickle.loads'](http://docs.python.org/2/library/pickle.html#pickle.loads)將字符串直接轉換爲對象,而不是通過'BytesIO'對象。 (你可以在加密結束時使用['pickle.dumps'](http://docs.python.org/2/library/pickle.html#pickle.dumps))。希望複雜度的下降可以解決任何問題這裏正在發生。 – senderle

+0

感謝您的回覆。如果我這樣做,切換到stringIO爲緩衝區,和str(gpg.decrypt_file(f,passphrase = PASSWORD))我得到:TypeError:'_io.StringIO'不支持緩衝區接口。我認爲gpg.decrypt_file返回對象Crypt,因此可以使用屬性類型.ok,.status和.stderr以及.data。 – user1887962

+0

我想你已經誤解了。完全刪除所有僞文件(StringIO和BytesIO);你不需要它們。使用'pickle.dumps'來生成一個字符串;使用'gpg.encrypt'加密字符串,將加密的字符串保存到文件中。然後將加密文件加載到一個字符串中,解密爲一個字符串(使用'str(gpg.decrypt(data))')並將該字符串傳遞給'pickle.loads'。根本沒有涉及「StringIO」/「BytesIO」對象。 – senderle

回答

0
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=['[email protected]']) 
    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