I have a Python script that writes passwords to files these are two separate functions. The first encrypts the file ( After password writing is done ) and the second handles decryption on the file for reading/writing operations. encryption function works fine and encrypts the file however the decryption fails to execute the decryption on line Decrypted_Data = F.decrypt(Encrypted_Data). I have the code attached below for reference this function is called within a password generation function and works like this ( I have already tested the generation function and file writing without encryption and it works okay ) First a random ID is generated and then a random string is generated consisting of four lowercase and four uppercase letters and one punctuation and then a random.choice() is performed on each string and they are combined with that unique random ID this changes every function call. Executing the script results in InvalidToken. I don't know if this is important or not but the crypto library i am using is cryptography.fernet
# This code was taken from PythonCode with minor changes
with open("key.key", 'rb') as key_file:
Key = key_file.read()
F = fernet(Key)
with open("/tmp/Config.txt", 'rb') as File:
Encrypted_Data = File.read()
Decrypted_Data = F.decrypt(Encrypted_Data)
with open("/tmp/Config.txt", "wb") as File:
File.write(Decrypted_Data)