python-3.x - 令牌必须是字节,'List' 不可调用?P3.7
问题描述
我正在尝试使用 MySQLdb 中的密码(加密)验证用户的密码输入:Working with mysqlconnector, python 3.7
encryptor.py : 从 cryptography.fernet 导入 Fernet
#---key-----
key = b'Ys_G_ziOS1AFs6X-jD3rtgsh77b3HpF7A-yiGpH-5Fg='
cipher_suite = Fernet(key)
cipher_text = cipher_suite.encrypt(b"password") # Convert in byte
print(cipher_text)
key1 = b'Ys_G_ziOS1AFs6X-jD3rtgsh77b3HpF7A-yiGpH-5Fg='
cipher_suite1 = Fernet(key1)
chiper_text1 = b'gAAAAABcHA2-58GwFxKzMzZRJsNV269_Nc-xvabFJcXV8yf0BuKI2XtD7211Vusf_hMXNrvK0glamHuoJhwPegP9iFRjXpANcg=='
unciphered_text = (cipher_suite1.decrypt(chiper_text1))
print(unciphered_text)
现在表格很简单<form method='POST' action='cgi-bin/file.py'>
使用用户名,名称 = 用户名,密码,名称 = 密码
import mysql.connector
from config import *
from encryptor import *
import cgi, cgitb
import os
#----user/pasw params
form = cgi.FieldStorage()
username1 = form.getvalue('username')
password1 = form.getvalue('password')
print(f"Username: {username1} Password: {password1}")
#Search password in DB
mycursor = mydb.cursor()
mycursor.execute("USE continental")
sql_password = "SELECT password FROM users WHERE username ='root'"
#assword23 = "SELECT password FROM users WHERE user = %s", (username1,)
mycursor.execute(sql_password)
find_password = mycursor.fetchall() # ????
unciphered_text1 = (cipher_suite1.decrypt(find_password))#error
if password1==unciphered_text1:
print("ok")
else:
print(unciphered_text1)
unciphered_text1 = (cipher_suite1.decrypt(find_password))
File "/Users/jhon/prova/lib/python3.7/site-packages/cryptography/fernet.py", line 74, in decrypt
timestamp, data = Fernet._get_unverified_token_data(token)
File "/Users/jhon/prova/lib/python3.7/site-packages/cryptography/fernet.py", line 86, in _get_unverified_token_data
raise TypeError("token must be bytes.")
TypeError: token must be bytes.
token must be bytes.
我尝试转换但没有成功,想法是将输入密码与来自数据库的解密密码对抗,因为加密方法每次都会生成不同的哈希。我不知道如何解决我无法更正错误,fetchall() 应该返回一个我应该以字节为单位的列表,否则解密的方法不能以这种方式工作,或者可能只读取以字节为单位的字符串(cipher_text ), 我无法理解。谢谢大家。
解决方案
推荐阅读
- java - @Transactional 注释不适用于方法
- python - 如何在python中只有边框的图像上显示对象的叠加?
- javascript - 如何在 Google Place AutoComplete API 上添加会话令牌
- puppet - 我可以根据 puppet 的 hiera.yaml 文件中的名称选择层次结构吗?
- webpack - 将 Webpack 更新到 4.35.3 并将 Laravel Mix 更新到 4.1.2 后模块构建失败
- python - 我正在尝试使用 os.rename 一次重命名多个文件
- r - ggplot sf_geom 在多个区域之间创建自定义边框
- r - 使用 R-plotly 包中的 plot_ly 进行动态绘图
- javascript - 对象文字中的键、值映射
- c# - 动态类型不调用 AppDomain.TypeResolve