cryptography in python: Fernet key must be 32 url-safe base64-encoded bytes

47 views Asked by At

I am trying to make a login interface in a command prompt,

def login():
    key_file_path = "etc/keys/key.key"

    # Read the key from the file
    with open(key_file_path, "rb") as key_file:
        key = key_file.read()

    cipher_suite = Fernet(key)

    username = input("Enter your username: ")
    password = input("Enter your password: ")

    user_file_path = "etc/users/" + username + ".usr"

    # Check if the file exists for the provided username
    if os.path.exists(user_file_path):
        with open(user_file_path, "rb") as file:
            # Decrypt the stored password
            enc = file.read()
            stored_password = cipher_suite.decrypt(enc).decode()

            # Check if the decrypted password matches the user input
            if stored_password == password:
                print("Login successful!")
                user_terminal(username)
            else:
                print("Incorrect password. Please try again.")
    else:
        print("Error: User not found.")

problem is, cryptography does not like this...

ValueError: Fernet key must be 32 url-safe base64-encoded bytes.

The key is: b'ckUS_DFMD3P_t14204IndtLhHokAaQrFT48aY4TF2JU='

reinstalled cryptography, regenerated the key

0

There are 0 answers