Getting this error when trying to run this:
File "Test Files.py", line 502, in decryptdefault decrypted = fernet.decrypt(d) File "/usr/lib/python3/dist-packages/cryptography/fernet.py", line 74, in decrypt timestamp, data = Fernet._get_unverified_token_data(token) File "/usr/lib/python3/dist-packages/cryptography/fernet.py", line 92, in _get_unverified_token_data raise InvalidToken cryptography.fernet.InvalidToken
FYI dk variable is defined with key (default key)
dk = 'niwaXsYbDiAxmLiqRiFbDa_8gHio15sNQ6ZO-sQ0nR4=' # Decrypts the file with default key def decryptdefault(inclufile): Key = dk fernet = Fernet(Key) readfile = open(inclufile, 'rb') d = readfile.read() readfile.close() # Decrypts and puts it into the text if readfile != "": decrypted = fernet.decrypt(d) decrypted = str(decrypted).replace('b'', '', 1) decrypted = decrypted[:-3] return str(decrypted)
Edit: I added the key for those who asked
Advertisement
Answer
I have found out, through trial and error with the same project later down the line, that you need to turn your key into something like this key = b'niwaXsYbDiAxmLiqRiFbDa_8gHio15sNQ6ZO-sQ0nR4='
The main difference being the key is encoded in a utf-8 format and is now readable by Fernet and doesn’t return that error. Here is a function that uses Tkinter, Fernet, and os to actually decrypt my file.
# Propriatary method of encrypting files def decrypt(self, file): with open(file, 'rb') as readfile: contents = readfile.read() self.title(os.path.basename(file) + ' - SecureNote') # self.textbox is a variable inside of the class I am using for my window self.textbox.delete(1.0, tk.END) if contents != "": # getword retur Key = bytes(getword('Key:', 1), encoding="utf-8") fernet = Fernet(Key) decrypted = fernet.decrypt(contents).decode('utf-8') self.textbox.insert(1.0, str(decrypted)) del Key del fernet else: pass