0

I'm trying to use the PHP function openssl_encrypt() to store a few short strings of data in a MySQL database.

When I test it with encrypt then decrypt in a PHP file it works fine, but if I save the data to the DB table and then SELECT it and openssl_decrypt() it, I just get gobbledygook like: ^� �Б4�

So, my guess is that the data format in DB table is corrupting the encrypted data - currently, I am doing everything in UTF-8 and saving it to a TEXT field type in the DB with a collation of utf8mb4_general_ci.

Should I change the field type in the DB table or do I need to do something to the encrypted data before inserting it? Or something else ??

Thanks for any thoughts you may have on this!

HugoScott
  • 164
  • 6
  • 1
    Use a BLOB field. – Sammitch Oct 26 '21 at 16:46
  • I use `blob` with `AES_ENCRYPT`. – user3783243 Oct 26 '21 at 16:47
  • 1
    For storage of encrypted data, you could use a BLOB field, and use MySQL's built in encryption functions. see: [How can I store sensitive data securely in a MySQL database?](https://stackoverflow.com/a/15059949/724039) – Luuk Oct 26 '21 at 16:47
  • 1
    `openssl_encrypt` returns binary data, so you will either need to change your db column type to binary (preferred method), e.g. `BLOB` or `VARBINARY`, or encode the results of `openssl_encrypt` to text before storing, and decode it from text back to binary before using `openssl_decrypt`. – Alan Oct 26 '21 at 16:50
  • Fantastic! I will go with the BLOB type, then! Thank you :) – HugoScott Oct 26 '21 at 17:01

0 Answers0