所以我一直在做一些关于创建预约系统的课程,我目前正在调查员工登录。我希望能够加密员工的密码以进行存储和比较。
我的问题是我必须从他们的Entry框中获取密码,但结果我不知道如何将数据转换为字节以便可以加密。这就是我现在所拥有的:
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()
答案 0 :(得分:2)
您可以在将字符串传递给加密之前将其编码为utf-8:
password = passwordEntry.get().encode("utf-8")
cipher_text = cipher_suite.encrypt(password)