python - 通过 Tkinter GUI 使用加密技术

标签 python python-3.x tkinter cryptography

所以我一直在做一些关于创建预约系统的类(class),目前我正在研究员工登录。我希望能够对员工的密码进行加密以便存储和比较。

我的问题是我必须从他们的输入框中获取密码,但结果我不知道如何将数据转换为字节以便对其进行加密。这是我目前所拥有的:

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()

最佳答案

您可以在将字符串传递给加密之前将其编码为 utf-8:

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

关于python - 通过 Tkinter GUI 使用加密技术,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49491730/

相关文章:

javascript - Selenium 如何批量处理多个 isElementDisplayed 调用?

python - 使用 python-pelican 生成网站时出现错误 "can' t 比较原始偏移和偏移感知日期时间”

python - 无法弄清楚 python 查询。

python - 如何使用 Pandas 在 pyqt 中将图表类型更改为面积图或条形图?

python - 从 URL 获取协议(protocol)和域(没有子域)

python - CPython:为什么字符串的+ =会更改字符串变量的ID

python - 如何在 Tkinter 列表框中插入时添加自动滚动?

python - 如何使用 python 提取 exe-archive 的内容?

python - 无效的 float 。消息错误Python

python - 如何更改 Python 中 TkTable 中事件单元格的文本颜色?