Search code examples
pythonencryptionpickle

Attempting to encrypt information before adding it to a pickled dictionary


I am currently attempting to save encrypted information in a dictionary in a pickle file, that can then be accessed, decrypted and searched through later on in the code.

I have created a function to encrypt, and then dump the information in a dictionary in my pickle file. But I am getting errors saying that the argument must be a string instead of bytes. I have tried to convert it, but I continue to get the error message regardless of what I have tried. Any help would be appreciated!

password_dictionary = {}
pickle.dump(password_dictionary, open("dictionary.pkl", 'wb'))
dictionary = "dictionary.pkl"

def encrypt_and_dump(username, password):
    f=Fernet(key)
    user_pass = (username + password)
    b_user_pass = user_pass.encode()
    encrypted_user_pass = f.encrypt(b_user_pass)
    d_encrypted_user_pass = str(encrypted_user_pass)
    with open(dictionary, 'a+') as f:
        a = {site:[d_encrypted_user_pass]}
        pickle.dump(a,f)

The error message

The error messages coming up at the moment are


Solution

  • You open the file in text mode "a+". Pickle wants to write bytes, but write() accepts str.

    So if you open the file with binary mode "ab+", everything should work:

    with open(dictionary, 'ab+') as f:
            a = {site:[d_encrypted_user_pass]}
            pickle.dump(a,f)