im rather new to python and even newer to cryptography, using the cryptography.fernet library on vs code. im working on a password manager-type software for fun, and have recently ran into this issue (raise InvalidToken cryptography.fernet.InvalidToken) while working with the cryptography library. Here is a snippet of my code: (please excuse the terribly low-level code)
userdata = ' '.encode() # Linked to username textbox
passdata = ''.encode() # Liked to password textbox
file10 = open('a4.txt', "w")
file10.write(userdata)
file10.close()
file11 = open("a5.txt", "w")
file11.write(passdata)
file11.close()
file12 = open("a2.txt", "w")
file12.write("b") #indicates that a username and password has been selected
file12.close()
encryptedusername = fernet.encrypt(userdata)
with open ('a4.txt', 'wb') as file2:
file2.write(encryptedusername)
file2.close()
encryptedpassword = fernet.encrypt(passdata)
with open ('a5.txt', 'wb') as file4:
file4.write(encryptedpassword)
file4.close()
#^ encrypt and write username and password data to file
#v Decrypt username and password data
with open("a5.txt", "rb") as f:
encmsg_1 = f.read()
f.close()
decmsg_1 = fernet.decrypt(token = encmsg_1)
with open("a5.txt", "rb") as f2:
encmsg_2 = f2.read()
f2.close()
decmsg_2 = fernet.decrypt(token = encmsg_2)
if anyone has any possible soluition or edit to my code, that would be very welcome!
i tried switching this exact code to the other template i used for encrypting your saved passwords, and it still returned the error "raise InvalidToken cryptography.fernet.InvalidToken." this was strange because the other piece of code encrypted and decrypted fine, if anyone was wondering; the code snippet that worked specifically for that part of the code is below.
input2 = tb1.get("1.0", END) # linked to a text area widget
file = open("voice.txt","w")
file.write(input2)
file.close()
file2 = open("voice.txt","r")
x = file2.read()
file2.close
file3 = open("voice.txt", "wb")
message=str(x)
encMessage = fernet.encrypt(message.encode())
file3.write(encMessage)
file3.close()
# encrypt saved text: ^ |
# decrypt and show saved text: v
frame3 = Frame(window, height= 2000, width = 2000)
frame3.place(x = 0, y = 0)
file11 = open("voice.txt", "ab")
file11.write(" ".encode())
file11.close()
file10 = open("voice.txt","r")
x = (file10.read()).encode()
file10.close()
global decMessage
decMessage = fernet.decrypt(x)