2

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.

Zozo
  • 81
  • 2
  • 8

1 Answers1

4

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'
subhanshu kumar
  • 372
  • 3
  • 10