I use this technique to crypt some field of my database:
How to use AES_ENCRYPT and AES_DECRYPT in mysql
It works very well but I am facing a problem.
Now that the contents of the field are encrypted, I can not do LIKE in a classic way!
I try to do the like on the decrypt field but sql does not recognize the field !!
Here is the structure (very simple):
CREATE TABLE `messages` (
`id` int(11) NOT NULL,
`message` varchar(250) NOT NULL,
`crypt_key` varchar(50) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
ALTER TABLE `messages`
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=1;
Insert:
INSERT into messages (message) VALUES (AES_ENCRYPT('Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. ', '123456'));
Simple select:
SELECT
CAST(AES_DECRYPT(message, '123456') AS CHAR(50)) decrypt
FROM messages
Perfect, all work!
Now, let's imagine that I want to search with a LIKE in the 'message' field:
SELECT
CAST(AES_DECRYPT(message, '123456') AS CHAR(50)) decrypt
FROM messages WHERE decrypt LIKE '%Lorem%'
I get this error:
Unknown 'decrypt' field in where clause
And same error for this query:
SELECT
CAST(AES_DECRYPT(message, '123456') AS CHAR(50)) AS decrypt
FROM messages WHERE decrypt.message LIKE '%Lorem%'
In principle, I have the message encrypt, the decryption key and the algorithm for decryption! It should be possible to search by sql in the decrypt field but I can not find a solution.
With a request stack? but this is not very optimize...
I am taker of any solution and any opinion!