Please excuse my poor english and spaghetti code;
I really want to make a python script that encrypts and decrypts files, but every time i try to run it (in vs code), there is one or another problem with the token.
Here is the code:
import os
from cryptography.fernet import Fernet
defKey = 'wLQz9Enb5veyv_JhXKsYMWybwAhm-JA1ytdyh4tGRI0='
def dec():
inpPass = input('Password:\n')
passKey = inpPass + defKey[len(inpPass):]
passToken = open('pass.txt', 'rb').read
Fernet(passKey.encode('utf-8')).decrypt(passToken)
print('no error wowie')
dec()
The exception with this code here is TypeError: token must be bytes.
Quite detailed explanation:
The pass.txt
file contains the word "check" encrypted with this key: amogusnb5veyv_JhXKsYMWybwAhm-JA1ytdyh4tGRI0=
. The script is prompting the user to enter a password. The password gets converted to a fernet key by replacing the first letters of defKey
. After that the program tries validating the key by decrypting the pass file.
I tried three notable things:
Making the
pass.txt
file plaintext in the script, and converting it to bytes. This worked, but i want the script to take in different passwords.Converting the file to string, then to bytes.
Fernet(passKey.encode('utf-8')).decrypt(bytes(str(encPass), 'utf-8'))
This gives this exception: InvalidToken: Incorrect padding.
- Doing what i did previously, but adding an equals sign to the string. This gives InvalidToken: no description. I thought about adding this because is solved somebody's problem with decoding base64.
Please help! I am really stuck, and for no good reason.