0

ok so I'm trying to code a password manager in python and I'm using cyrptography.fernet to crypt the emails and passwords and then store them into a local SQLite database the problem is that when I try to get for example the emails in the database they are in this format: (b'encypted-email-here'), (b'and-so-on) so I thought since theres the b before the quotes it's in bytes format and I do not need to do anything in order to decrypt them but when I actually try to decrypt them I get an error saying: "TypeError: token must be bytes" here is my code so you can take a look at it

    b_email = email.encode('utf-8')
    b_pwd = pwd.encode('utf-8')
    enc_email = f.encrypt(b_email)
    enc_pwd = f.encrypt(b_pwd)
    conn = sqlite3.connect('database.db')
    execute = conn.cursor()
    execute.execute('CREATE TABLE IF NOT EXISTS logins (website, email, password)')
    execute.execute('INSERT INTO logins VALUES (:website, :email, :password)', {'website': website, 'email': enc_email, 'password': enc_pwd})
    conn.commit()
    conn.close()


def view():
    con = sqlite3.connect('database.db')
    cur = con.cursor()
    iterable = cur.execute('SELECT email FROM logins')
    for email in iterable:
        dec_email = f.decrypt(email)
        print(dec_email)```

Zogra
  • 1
  • 1

1 Answers1

1

cur.execute() returns a sequence of rows, each of them is a tuple. In your case, a tuple of just one element, but still you need to extract the email from it. The most elegant way would be unpacking (notice the comma after email):

for email, in cur.execute('SELECT email FROM logins'):
    print(f.decrypt(email))
bereal
  • 32,519
  • 6
  • 58
  • 104