简体   繁体   中英

How to store AES encrypted information in MySQL database

I have a piece of information which is encoded using aes-256-cbc encryption. How should I store it in the database? Currently I'm using VARCHAR(255) utf8_bin. Is this OK or should I use other field type like VARBINARY(255)? Is there a possibility of losing some data using VARCHAR in this case? Thanks.

The possible (in)appropriateness of storing encrypted (as opposed to hashed) passwords in a database notwithstanding, AES ciphertext is binary data, and therefore should be stored as such , ie in a BINARY / VARBINARY column or a BLOB .

It's also possible to encode the ciphertext eg as base64 , and then store it in a text (ie CHAR / VARCHAR / TEXT ) column. This is less space-efficient, but it may sometimes be more convenient, eg when inspecting the data visually or passing it between programs that may have trouble dealing with fields containing arbitrary binary data.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM