python - 我不断收到 base64 和令牌错误这是什么意思?
问题描述
更新:我只知道令牌错误,我不明白当我使用相同的密钥加密和解密时发生了什么?手头的错误:
Exception in Tkinter callback #Don't understand this callback as this is to do with the cryptography module I am using
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/tkinter/__init__.py", line 1883, in __call__
return self.func(*args)
File "/Users/James/Documents/Programming/PasswordManager/main.py", line 190, in see
decrypt = f.decrypt(item[1].encode('utf-8'))
File "/Users/James/Documents/Programming/venv/lib/python3.8/site-packages/cryptography/fernet.py", line 75, in decrypt
timestamp, data = Fernet._get_unverified_token_data(token)
File "/Users/James/Documents/Programming/venv/lib/python3.8/site-packages/cryptography/fernet.py", line 107, in _get_unverified_token_data
raise InvalidToken
cryptography.fernet.InvalidToken
b'gAAAAABgY3KXoeeegS8RPHdvXTH6_GQ_EPfgdqZqIwP4XL-hIyEk3BcxV3y0o_quNPFyHeTdkv7Pk9MmnEIL1XeXlEfuUQNJ_e_dIsAr2ZvLGpDT1Y6I6Qvzat5aAf7Z0624O4BeAFNf
原始问题
我正在做一个密码管理器,我从使用文件和一些建议的 sqlite3 开始,所以我已经开始使用它,所以我可以添加重写密码的功能,但我不断收到两个让我很困惑的错误,
表sqlite表:
c.execute("""CREATE TABLE passwords (passwordTitle text,generatedPassword text)""")
我使用此功能加密密码,这有效并插入数据库:
def submited():
# Call the password generator
setPassword()
# Get user input from the entry
usrinput = userInput.get()
# Set a splitter for writing to the file
# If passwordenc.txt exists:
if os.path.isfile('data.db'):
# Open passwordenc.txt and set it as password_file
# Encrypt the combo
genPasswordEnc = f.encrypt(generatePassword.encode('utf-8'))
c.execute("INSERT INTO passwords VALUES(?,?)", (usrinput, genPasswordEnc))
conn.commit()
# If passwordenc.txt does not exist:
我使用这个函数解密它:
def see():
menu.destroy()
seeWin = Tk()
seeWin.geometry('1250x500')
# Same size will be defined in variable for center screen in Tk_Width and Tk_height
Tk_Width = 1250
Tk_Height = 500
# calculate coordination of screen and window form
x_Left = int(seeWin.winfo_screenwidth() / 2 - Tk_Width / 2)
y_Top = int(seeWin.winfo_screenheight() / 2 - Tk_Height / 2)
# Write following format for center screen
seeWin.geometry("+{}+{}".format(x_Left, y_Top))
seeWin.title('See Password')
seeFrame = tk.Frame()
listbox = tk.Listbox(seeFrame, height='30', width='135')
listbox.pack(side=LEFT, fill=BOTH)
scrollbar = tk.Scrollbar(seeFrame, )
scrollbar.pack(side=RIGHT, fill=Y)
listbox.config(yscrollcommand=scrollbar.set)
scrollbar.config(command=listbox.yview)
if os.path.isfile('data.db'):
c.execute('SELECT * FROM passwords')
password_list = c.fetchall()
for item in password_list:
item = str(item)
itemSplit = item.split(',')
leng = len(itemSplit[1])-2
itemCut = itemSplit[1][:leng].encode('utf-8')
print(itemCut)
decrypt = f.decrypt(itemCut)
print(decrypt)
listbox.insert(END, str(decrypt))
print("Decrypted pass: ", decrypt)
但我不断收到此错误:
Exception in Tkinter callback
Traceback (most recent call last):
File "/Users/James/Documents/Programming/venv/lib/python3.8/site-packages/cryptography/fernet.py", line 102, in _get_unverified_token_data
data = base64.urlsafe_b64decode(token)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/base64.py", line 133, in urlsafe_b64decode
return b64decode(s)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/base64.py", line 87, in b64decode
return binascii.a2b_base64(s)
binascii.Error: Invalid base64-encoded string: number of data characters (141) cannot be 1 more than a multiple of 4
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/tkinter/__init__.py", line 1883, in __call__
return self.func(*args)
File "/Users/James/Documents/Programming/PasswordManager/main.py", line 190, in see
decrypt = f.decrypt(itemCut)
File "/Users/James/Documents/Programming/venv/lib/python3.8/site-packages/cryptography/fernet.py", line 75, in decrypt
timestamp, data = Fernet._get_unverified_token_data(token)
File "/Users/James/Documents/Programming/venv/lib/python3.8/site-packages/cryptography/fernet.py", line 104, in _get_unverified_token_data
raise InvalidToken
cryptography.fernet.InvalidToken
而且我不明白在使用 .txt 文件时引发此错误的原因是什么
解决方案
推荐阅读
- mysql - SQL count + join 抛出语法错误,但没有 count 可以正常工作
- php - 从两个表中回显内容
- python - 如何识别图像中的三种对象?
- ios - RxSwift 仅在添加新元素时观察数组变化
- c# - 如何改进 C# 中的推送数据管道以匹配 F# 的性能
- javascript - 如何让多个 XMLHttpRequests 串行发生?
- r - 为 ggplot2 中的点指定形状
- android - 如何将 pdf 文档显示到 Webview 中?
- javascript - Fabric js文本框换行一个长字
- linux - Shell Scripting ssh 执行命令