Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 929
Rafael Sedrakyan Avatar asked Nov 13 '15 21:11

Rafael Sedrakyan


1 Answers

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, i.e. in a BINARY / VARBINARY column or a BLOB.

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

like image 139
Ilmari Karonen Avatar answered Sep 28 '22 09:09

Ilmari Karonen