Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
94 views
in Technique[技术] by (71.8m points)

Python decrypting using cryptography adds random characters

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 '"and "b'Admin'"

question from:https://stackoverflow.com/questions/65599574/python-decrypting-using-cryptography-adds-random-characters

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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 " ", and the " " by using strip() after converting usernameInput and passwordInput to a string:

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

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...