首页 > 解决方案 > 令牌必须是字节,'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 ), 我无法理解。谢谢大家。

标签: python-3.xencryptionloginmysql-connector

解决方案


推荐阅读