Search code examples
pythonpython-3.xencryptionfernet

Decrypt message with cryptography.fernet do not work


I just tried my hand at encrypting and decrypting data. I first generated a key, then encrypted data with it and saved it to an XML file. Now this data is read and should be decrypted again.

But now I get the error message "cryptography.fernet.InvalidToken".

import xml.etree.cElementTree as ET
from cryptography.fernet import Fernet
from pathlib import Path

def load_key():
    """
    Load the previously generated key
    """
    return open("../login/secret.key", "rb").read()

def generate_key():
    """
    Generates a key and save it into a file
    """
    key = Fernet.generate_key()
    with open("../login/secret.key", "wb") as key_file:
        key_file.write(key)

def decrypt_message(encrypted_message):
    """
    Decrypts an encrypted message
    """
    key = load_key()
    f = Fernet(key)
    message = encrypted_message.encode('utf-8')
    decrypted_message = f.decrypt(message)

    return(decrypted_message)

def decryptMessage(StringToDecrypt):
    decryptedMessage = decrypt_message(StringToDecrypt)
    return decryptedMessage

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

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

In the tuple "credentials" there are 2 encrypted strings.

Please help - have already tried everything to change the formats, but no chance.

Edit:

Errormessage:

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

Solution

  • I found an answer to my problem:

    I took ASCII instead of utf-8. And I added a .decode('ASCII') at the function "loginToRoster" to both variables 'user' and 'pw'

    Now the encryption and decryption works fine.

    So, the 'loginToRoster' functions looks like:

    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)