0
from cryptography.fernet import Fernet
import base64


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

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


master_pwd=input("Enter master password: ")

key=load_key() + master_pwd.encode()
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("Username:", user, " |Password:",
                  fer.decrypt(passw))
                 # base64.decode(fer.decrypt(passw, encoding="ascii")))
           


def create():
    name=input("username: ")
    pwd=input("password: ")

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


while True:
    mode=input("Create a new password or view existing ones (view, create), press x to quit? ").lower()
    if mode=="x":
        break

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




PS C:\Users\RAINMAKER\Desktop\Python Projects II> & C:/Users/RAINMAKER/AppData/Local/Programs/Python/Python311/python.exe "c:/Users/RAINMAKER/Desktop/Python Projects II??/password manager.py"
Enter master password: h
Create a new password or view existing ones (view, create), press x to quit? create
username: chief chef
password: 12345
Create a new password or view existing ones (view, create), press x to quit? view
Traceback (most recent call last):
  File "c:\Users\RAINMAKER\Desktop\Python Projects II\password manager.py", line 47, in <module>    
    view()
  File "c:\Users\RAINMAKER\Desktop\Python Projects II\password manager.py", line 29, in view        
    fer.decrypt(passw))
    ^^^^^^^^^^^^^^^^^^
  File "C:\Users\RAINMAKER\AppData\Local\Programs\Python\Python311\Lib\site-packages\cryptography\fernet.py", line 86, in decrypt
    timestamp, data = Fernet._get_unverified_token_data(token)
                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\RAINMAKER\AppData\Local\Programs\Python\Python311\Lib\site-packages\cryptography\fernet.py", line 122, in _get_unverified_token_data
    raise InvalidToken
cryptography.fernet.InvalidToken


I was creating a password manager to restrict unauthorised access.

I am trying to write a code for encryption in Python and decryption in Python but I am getting an error.

I am using cryptography.fernet in python to encrypt a file and when I use Fernet Python for decryption it shows an error.

Above is my python code:

victor
  • 1
  • 1

1 Answers1

0

Firstly I don't know what value has the key.key file, so I just made an empty file and just set as my master key this: 12345678901234567890123456789012.

Keep in mind that in Fernet docs they say that key should be a URL-safe base64-encoded 32-byte key.

So you should update your code where you set Fernet key like this:

key=base64.b64encode(load_key() + master_pwd.encode())
fer=Fernet(key)

And make sure that it's 32-byte key length.

Secondly you are not decrypting the passwords right. You should encode them before decrypt them.

print("Username:", user, " |Password:", fer.decrypt(passw.encode()).decode()))

These fixes should do the job.

But I have some other comments to do to your code:

1. Never store `key.key` in plaintext
2. If you want to make a basic password database, it's not very good idea to split the rows like this a|b. You should read about JSON format.
3. In cryptography I don't recommend to use decode() nowhere. It's safer for you to use always encoded texts at files reading/writing actions and use decode() at user's outputs only if you are sure that there are no bytes that cannot be decoded.