Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/295.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
无法在Python中排除cryptography.fernet.InvalidToken错误_Python_Python 3.x_Error Handling_Cryptography - Fatal编程技术网

无法在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有疑问。我怎么和你聊天?如果你有其他问题,最好是写另一个问题,谢谢!祝您有个美好的一天!