'Limiting the Number of Characters in Cryptography Encryption Output

I am working on a password manager project in Python. I'm using the cryptography library to create Fernet objects for the keys to encrypt my password data. I'm storing the encrypted passwords into a MySQL database along with user information.

My problem is:

  • The encrypted passwords exceed the charset of my database column.
  • I know I could potentially change the column to a larger charset to fit the data, but there is no guarantee the encrypted password length will not exceed the charset later.

My goal is:

  • I want to limit the number of characters in the encrypted password output in order to prevent bugs down the line and use less memory space.

I tried searching information regarding this, but didn't find anything answering this particular question.

Screenshot of code and error message

user = d.get_user()
raw_pwd = d.get_pwd()
site = d.get_site()
email = d.get_email()
bkup_email = d.get_bkup_em()

# generate new key 
key = Fernet.generate_key()
k = Fernet(key)

# change password plaintext into bytes
raw_pwd = raw_pwd.encode()

# encrypt password before pushing to database
encrypted_pwd = k.encrypt(raw_pwd)

# insert collected attributes into table
table_query = f'''INSERT INTO {TABLE} (username, pass_word, website, email, backup_email) 
VALUES (%s, %s, %s, %s, %s);'''
record_data = (user, encrypted_pwd, site, email, bkup_email)
cursor.execute(table_query, record_data)
connection.commit()
print('New data added!')

Error Message: Exception has occurred: DataError 1406 (22001): Data too long for column 'pass_word' at row 1



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source