0

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)
Roland Smith
  • 42,427
  • 3
  • 64
  • 94
Donald
  • 1
  • 1
  • Please post a [mcve] – ForceBru Apr 21 '20 at 10:12
  • Added the code to my post – Donald Apr 21 '20 at 10:20
  • 1
    Currently, your description is hard to read due to not including a single period. Can you fix this? – SuperStormer Apr 21 '20 at 13:14
  • The code is there but yes i will edit the post – Donald Apr 21 '20 at 13:18
  • Yes sorry if i came off as rude but i edited the post but the previous formatted code thanks to ForceBru is all jumbled up again and i am too stupid to get it formatted correctly again – Donald Apr 21 '20 at 13:35
  • But if you are still not sure basically my problem is encryption works fine but decryption fails with InvalidToken the provided code is not the generator nor is it the encryption function because both work the attached code is the decryption function sorry for my poor English – Donald Apr 21 '20 at 13:38
  • Any help is appreciated i have worked on this problem for days with no luck and yes i have looked at the code and no it does not generate a different key it opens the key.key file in the current directory which i have verified is there and is indeed not empty and reads from it and then uses the output as the Fernet key so i know it is not being changed or modified and like i have said the key is there so the file is not empty so i am out of ideas also a strange piece of information is when testing it sometimes works but sometimes does not work and returns the the same error on that same line – Donald Apr 21 '20 at 13:42

0 Answers0