Zozo
Zozo

Reputation: 81

Python Fernet TypeError: token must be bytes

Here i try to insert new row into a table in a database.

def insert_record():
    values = input('Enter values ').replace(' ', '').split(sep=',')
    values[-1] = f"{f.encrypt(values[-1].encode())}"
    c.execute("INSERT INTO credentials VALUES (?, ?, ?)", values)
    conn.commit()
    conn.close()

I encrypt the last argument values[-1] because it will be a password. when i try to decrypt it

    for item in values:
    print(f.decrypt(item[-1]))

i get:

Traceback (most recent call last):
TypeError: token must be bytes

I tried so many things and i've been stuck for hours. I can't get rid of the error.

Upvotes: 2

Views: 11185

Answers (1)

subhanshu kumar
subhanshu kumar

Reputation: 392

please convert the type from string to byte by adding b in the string. Take a look at the below example:

>>> from cryptography.fernet import Fernet
>>> key = Fernet.generate_key()
>>> f = Fernet(key)
>>> token = f.encrypt(b"my deep dark secret")
>>> token
b'...'
>>> f.decrypt(token)
b'my deep dark secret'

Upvotes: 4

Related Questions