0

So im storing passwords and usernames in separate files, and i want to decrypt them and check them, but whenever i decrypt the words, it adds characters to the original words:

def checkLogin(usernameInput, passwordInput, key, Usr):
    f = Fernet(key)
    with open("username.csv", "rb") as file:
                encryptedUsername = file.read()
    decryptedUsername = f.decrypt(encryptedUsername)
    with open("password.csv", "rb") as file:
                encryptedPassword = file.read()
    decryptedPassword = f.decrypt(encryptedPassword)
    print(decryptedPassword)
    print(decryptedUsername)
    usernameInput = usernameInput.get()
    passwordInput = passwordInput.get()

the original Username and passwords were: "Admin" and "SecurePassword" when i print the decrypted strings it returns: "b'SecurePassword\r\n'"and "b'Admin'"

Zenot1c
  • 25
  • 5
  • Fernet is returning bytes, not a string. See [this answer](https://stackoverflow.com/a/6273618/2378643) for more details, but simply, you probably want to do something like `print(decryptedPassword.decode("utf-8")`. More than that, though, you really shouldn't be storing encrypted password. Look for a good password hashing algorithm. You shouldn't be able to get the original password. – Anon Coward Jan 06 '21 at 17:14

1 Answers1

0

You haven't really showed examples of the files to provide an idea of why additional characters are getting added, so I'll just provide a quick fix solution in Python. You can remove the "b'", the "\r", and the "\n" by using strip() after converting usernameInput and passwordInput to a string:

usernameInput = usernameInput.get().decode("utf-8").strip("\n").strip("\r")
passwordInput = passwordInput.get().decode("utf-8").strip("\n").strip("\r")

strip() basically removes the parameter provided at the beginning and end of a string, and decode() converts the variables to strings.

jianmin-chen
  • 603
  • 7
  • 9
  • it was the decrypted passwords that were the issue: I changed the Variables to them: decryptedPassword = decryptedPassword.strip("b'").strip("\r").strip("\n") decryptedUsername = decryptedUsername.strip("b'").strip("\r").strip("\n") and it returns this: TypeError: a bytes-like object is required, not 'str' – Zenot1c Jan 06 '21 at 18:11
  • @Zenot1c My mistake. I retook a look at your question and it seems like you're opening the files with rb, meaning you're reading the file in bytes. Retake a look at my edited answer - I added decode() which should convert the bytes to strings. However, Anon Coward is right - it's probably not safe to store passwords that can be decoded so easily, unless you're writing the code for practice. – jianmin-chen Jan 06 '21 at 19:05
  • Security isn't a massive issue, as its for a school project, which the encryption isn't necessary, just nice to have there! Thanks everyone – Zenot1c Jan 07 '21 at 21:52