I can't seem to be able to decrypt lines with the key used in encryption. and always get an "invalid token" decryption error'
I expect to add a password using the programme, encrypting it, and then write to file. If you open a file, all you'll see is encrypted text, and you can only see your passwords using the programme itself.
Here is my code:
from cryptography.fernet import Fernet
key = b'Sw42CeEyZCyRyct0Ly2loEga9D4iv6tF0WRiTLKVdDA='
fkey = Fernet(key)
def add():
app = input("enter app name ").upper()
name = input("enter account name ")
password = input("enter account password ")
with open("passwords2.txt", 'a') as f:
original = "{}: {} | {} ".format(app, name, password)
inbytes = original.encode()
originalencrypted = fkey.encrypt(inbytes)
f.write(str(originalencrypted) + "\n")
def read():
with open("passwords2.txt", 'r') as f:
for line in f:
print(fkey.decrypt(line.strip().encode()).decode())
while True:
operation = input("would you like to read, add, or quit (q) ")
if operation == "read":
read()
elif operation == "add":
add()
elif operation == "q" or operation == "quit":
quit()
else:
print("invalid input")
The problem is
str(originalencrypted)(It creates string like this"b'...'", rather than convert thebytestostrusing some defined encoding).You either want to convert
bytestostrproperly:originalencrypted.decode('utf-8').Or you want to write bytes to the file directly: