mcrypt characters causing mysql errors
-
26-10-2019 - |
Question
I'm having trouble getting php's mcrypt function to encrypt text in a way that can be stored in my mysql database.
Here's an example of a query that doesn't work:
UPDATE mydatabase.clients SET firstname='\'å»”é¶Q' WHERE id_client=65
But if I change it to be:
UPDATE mydatabase.clients SET firstname='Test' WHERE id_client=65
Then it works. So it must be those crazy mcrypt characters throwing things off.
What do I do to my mysql database so it accepts those characters?
Solution
The output of a cipher is a string of bytes, not characters. You shouldn't store cipher text directly as text. Use a "binary" data type, or transform the byte string to text with something like Base-64.
OTHER TIPS
Change the column type to binary instead, this may work.
Make sure you are escaping the encrypted string. It's an important part to doing this right!
I've been working with MySQL and Mcrypt and I store my encrypted data and initialization vectors as binary and I escape all of these strings before they get put in a query. Works like a charm.