Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/322.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解密消息不起作用_Python_Python 3.x_Encryption_Fernet - Fatal编程技术网

Python 使用cryptography.fernet解密消息不起作用

Python 使用cryptography.fernet解密消息不起作用,python,python-3.x,encryption,fernet,Python,Python 3.x,Encryption,Fernet,我只是试着加密和解密数据。我首先生成一个密钥,然后用它加密数据并将其保存到XML文件中。现在该数据已被读取,应再次解密 但现在我得到了错误消息“cryptography.fernet.InvalidToken” 在元组“凭据”中有2个加密字符串 请帮助-已经尝试了所有更改格式的方法,但没有机会 编辑: 错误消息: Traceback (most recent call last): File "C:/Users/r/Documents/GitHub/ServiceEvaluatio

我只是试着加密和解密数据。我首先生成一个密钥,然后用它加密数据并将其保存到XML文件中。现在该数据已被读取,应再次解密

但现在我得到了错误消息“cryptography.fernet.InvalidToken”

在元组“凭据”中有2个加密字符串

请帮助-已经尝试了所有更改格式的方法,但没有机会

编辑:

错误消息:

Traceback (most recent call last):
  File "C:/Users/r/Documents/GitHub/ServiceEvaluationRK/source/main.py", line 27, in <module>
    login.loginToRoster(chrome)
  File "C:\Users\r\Documents\GitHub\ServiceEvaluationRK\source\login.py", line 106, in loginToRoster
    user = decryptMessage(credentials[0])
  File "C:\Users\r\Documents\GitHub\ServiceEvaluationRK\source\login.py", line 49, in decryptMessage
    decryptedMessage = decrypt_message(StringToDecrypt)
  File "C:\Users\r\Documents\GitHub\ServiceEvaluationRK\source\login.py", line 43, in decrypt_message
    decrypted_message = f.decrypt(message)
  File "C:\Users\r\Documents\GitHub\ServiceEvaluationRK\venv\lib\site-packages\cryptography\fernet.py", line 75, in decrypt
    timestamp, data = Fernet._get_unverified_token_data(token)
  File "C:\Users\r\Documents\GitHub\ServiceEvaluationRK\venv\lib\site-packages\cryptography\fernet.py", line 107, in _get_unverified_token_data
    raise InvalidToken
cryptography.fernet.InvalidToken
回溯(最近一次呼叫最后一次):
文件“C:/Users/r/Documents/GitHub/ServiceEvaluationRK/source/main.py”,第27行,在
login.logintoloster(chrome)
文件“C:\Users\r\Documents\GitHub\ServiceEvaluationRK\source\login.py”,第106行,登录名册
用户=解密消息(凭据[0])
解密消息中第49行的文件“C:\Users\r\Documents\GitHub\ServiceEvaluationRK\source\login.py”
decryptedMessage=decrypt_消息(StringToDecrypt)
解密消息中第43行的文件“C:\Users\r\Documents\GitHub\ServiceEvaluationRK\source\login.py”
解密消息=f.解密(消息)
文件“C:\Users\r\Documents\GitHub\ServiceEvaluationRK\venv\lib\site packages\cryptography\fernet.py”,第75行,在decrypt中
时间戳,data=Fernet.\u获取\u未验证\u令牌\u数据(令牌)
文件“C:\Users\r\Documents\GitHub\ServiceEvaluationRK\venv\lib\site packages\cryptography\fernet.py”,第107行,在\u get\u unverified\u token\u数据中
提出无效代币
cryptography.fernet.InvalidToken

您在解密消息函数中定义了
加载密钥()
。它不是一个方法,只是一个未定义的函数。您可能会收到该错误,因为密钥无效,因为您没有收到保存的密钥。

我找到了问题的答案:

我选择了
ASCII
而不是
utf-8
。我在函数“logintoloster”中为变量“user”和“pw”添加了一个
.decode('ASCII')

现在加密和解密工作正常

因此,“登录花名册”功能如下所示:

def loginToRoster(chrome):
    credentials = readXML()
    user = decryptMessage(credentials[0]).decode('ASCII')
    pw = decryptMessage(credentials[1]).decode('ASCII')

    userName = chrome.find_element_by_id('UserName')
    userName.send_keys(user)
    password = chrome.find_element_by_id('Password')
    password.send_keys(pw)

抱歉-我忘了用复制此函数。我现在修改了帖子。错误仍然存在。对于新手错误,很抱歉-我现在已经包含了完整的错误消息。我希望这有助于解决问题。
def loginToRoster(chrome):
    credentials = readXML()
    user = decryptMessage(credentials[0]).decode('ASCII')
    pw = decryptMessage(credentials[1]).decode('ASCII')

    userName = chrome.find_element_by_id('UserName')
    userName.send_keys(user)
    password = chrome.find_element_by_id('Password')
    password.send_keys(pw)