I did it by the tutorial and it's not working

Question:

I found some projects on youtube, and this is one of them.
I’m trying with the password manager program. Here’s the link: https://www.youtube.com/watch?v=DLn3jOsNRVE

And here’s my code:

from cryptography.fernet import Fernet

'''
def write_key():
    key = Fernet.generate_key()
    with open("key.key", "wb") as key_file:
        key_file.write(key)'''


def load_key():
    file = open("key.key", "rb")
    key = file.read()
    file.close()
    return key


key = load_key()
fer = Fernet(key)


def view():
    with open('passwords.txt', 'r') as f:
        for line in f.readlines():
            data = line.rstrip()
            user, passw = data.split("|")
            print("User:", user, "| Password:",
                  fer.decrypt(passw.encode()).decode())


def add():
    name = input('Account Name: ')
    pwd = input("Password: ")

    with open('passwords.txt', 'a') as f:
        f.write(name + "|" + fer.encrypt(pwd.encode()).decode() + "n")


while True:
    mode = input(
        "Would you like to add a new password or view existing ones (view, add), press q to quit? ").lower()
    if mode == "q":
        break

    if mode == "view":
        view()
    elif mode == "add":
        add()
    else:
        print("Invalid mode.")
        continue

#this is a module that will alow you to encrypt txt-s
# pass is used as a placeholder for future code
# rstrip removes any traling chars
#split will look for the char in the arg and it will split the string there
#a append w write r read r+ read and write
#with w mode, you completely overwrite the file so be careful
#with a mode you can add smthing to the end 

I followed the instructions precisely and I have no idea what could cause my problem. When I run it, I get an error message:
enter image description here

The guy even has his version of the code on github. I copy-pasted it and still doesn’t work

On the video, does the key.key file generate itself, or not?

Asked By: kristof

||

Answers:

The only possible code that could write to the key.key file is both:

  • commented out; and
  • not called even if it were not commented out,

So, no, it’s not correct to say that "the key.key file generate[s] itself".


Looking over the linked video, the presenter at some point (at 1:29:50, more precisely) had the code call that function to create the file, then removed that call and commented out that function.

Likely cause of your problem is that you made a mistake in the process somewhere(1), and this resulted in the file not being created (or being created somewhere other than where it’s expected). Suggest you go back to that point in the video and re-do it.

Or, you could just create the key.key file, containing the content (taken from the video):

Raq7IMZ4QkqK20j7lKT3bxJTgwxeJFYx4ADjTqVKdQY=

That may get you going faster than revisiting the steps that the presenter took.


(1) Re your comment that "[t]he guy even has his version of the code on github", it may be that you thought you could bypass the video and just go straight to the final code. If so, that was a mistake, as the final code expects you to have run the incomplete code in order to generate the key file.

If so, I would consider that a failing of the presenter. It would have been far better to leave the keyfile-creating code in and call it, for example, when you ran the code with python the_code.py --make-key-file.

Answered By: paxdiablo
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.