无法在Python中排除cryptography.fernet.InvalidToken错误
我试过这个:无法在Python中排除cryptography.fernet.InvalidToken错误,python,python-3.x,error-handling,cryptography,Python,Python 3.x,Error Handling,Cryptography,我试过这个: from cryptography import * try: #code here to create key from a password f=Fernet(key) token=f.decrypt(data) except cryptography.fernet.InvalidToken: print("wow") 但它仍然会引发错误。来自: 解密(令牌,ttl=None) 提出: cryptography.fernet.InvalidT
from cryptography import *
try:
#code here to create key from a password
f=Fernet(key)
token=f.decrypt(data)
except cryptography.fernet.InvalidToken:
print("wow")
但它仍然会引发错误。来自:
解密(令牌,ttl=None)
提出:
- cryptography.fernet.InvalidToken–如果令牌以任何方式无效,将引发此异常。令牌可能对某个
原因数量:它比ttl旧、格式不正确或
没有有效的签名 - TypeError–如果令牌不是字节,则引发此异常
from cryptography import *
import cryptography
try:
#code here to create key from a password
f=Fernet(key)
token=f.decrypt(data)
except (cryptography.fernet.InvalidToken, TypeError):
print("wow")
另外,键
应该是字节–URL安全的base64编码的32字节键
如果出现以下错误消息:
Error: Incorrect padding
这是因为在Fernet
类中,构造函数正在对键应用base64.decodestring
key = base64.urlsafe_b64decode(key)
要捕获错误,您可以使用:
from binascii import Error
from cryptography import *
import cryptography
try:
#code here to create key from a password
f=Fernet(key)
token=f.decrypt(data)
except (cryptography.fernet.InvalidToken, TypeError, Error):
print("wow")
上面写着NameError:name'cryptography'没有定义
@Mr.Dark,因为您没有导入cryptography
请立即尝试我的答案我对tkinter有疑问。我怎么和你聊天?如果你有其他问题,最好是写另一个问题,谢谢!祝您有个美好的一天!