简体   繁体   中英

Using Cryptography with Tkinter GUI

So I have been doing some coursework on creating an appointment booking system and I am currently looking into the employee logins. I want to be able to encrypt the passwords of employees for storage and comparisons.

My problem is that I have to get the password from their Entry box but as a result I don't know how to convert the data to bytes so that it can be encrypted. Here is what I have at the moment:

import tkinter as tk
from cryptography.fernet import Fernet

key = Fernet.generate_key()
cipher_suite = Fernet(key)

MEDIUM_FONT = ("Berlin Sans FB", 12)
LARGE_FONT = ("Berlin Sans FB", 16)

#validate function
def validateStylist(window):
    password = bstr(passwordEntry.get())
    cipher_text = cipher_suite.encrypt(password)
    plain_text = cipher_suite.decrypt(cipher_text)
    print(plain_text)

window = tk.Tk()

titleLabel = tk.Label(window, text="Register Stylist", font=LARGE_FONT, bg="#FFC0CB")
titleLabel.grid(columnspan = 4)

#Password
passwordLabel = tk.Label(window, text="Password:", font=MEDIUM_FONT, bg="#FFC0CB")
passwordLabel.grid(row=1,column=3)
passwordVar = tk.StringVar(window)
passwordEntry = tk.Entry(window, textvariable=passwordVar)
passwordEntry.grid(row=2,column=3)

finishButton = tk.Button(window, text="Finish",
                          command=validateStylist(window))
finishButton.grid(row=4, column=3, sticky="ew")

window.mainloop()

You can encode the string to utf-8 before passing it to encrypt:

password = passwordEntry.get().encode("utf-8")
cipher_text = cipher_suite.encrypt(password)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM