Search the database with encrypted data

Asked

Viewed 272 times

1

I own a database where the entered data is encrypted through a function written in PHP, this function encrypts and decrypts. I need to search this table.

For example, I search for "BRAZIL", but in the table "BRAZIL" is encrypted as: cdlddzfqm3b3szyxvvk2u3n6lzb1ut09

Use PHP and Mysql, and to make the searches use LIKE % %

1 answer

3


It was not given many details but by the described way I believe that is only encrypt what you want to look for, then the comparison will be made of the data encrypted with another data likewise encrypted.

$query = "select * from tabela where campo = " . crypt("BRASIL");

I put in the Github for future reference.

This may not work depending on some factors such as how encryption is done, database configuration for comparison (collation), etc. But I believe you are using a simple way and this way will work.

With LIKE under normal conditions is not possible, at least I do not know a form. It may be possible with some additional module to Mysql but I don’t know any that do this. I don’t really know if it’s a good idea to do this.

It may be possible to create a function for Mysql (possibly in C) that can be used in place of or with LIKE to do this search. nor will I speculate too much on this because it does not seem to be the best of ideas.

A horrible possible solution would be to take all the encrypted data, decrypt it in PHP and make the selection. It’s easier to do than the previous solution but terrifying to use.

I found that solution in the OS for cryptography builtin Mysql. I wouldn’t know to say all the limitations except the performance already mentioned there.

select * from tabela where aes_decrypt(campo, salt) like '%BRASIL%'

I put in the Github for future reference.

Obviously this only works if the data is recorded in the same way.

Behold other encryption functions builtin mysql.

  • Poise, however I need to use the LIKE and would not work this way.

  • Only you didn’t put it in the question.

  • Sorry, I omitted details, already arranged. I will read your reply.

  • That way it would only be possible with this kind of correct encryption?

  • Could be with any kind of encryption builtin.

  • I use a php function that encrypts with Base64 and keys

  • As I said, it would complicate a lot, I would have to use extremely complex solutions. Besides, Base64 doesn’t encrypt anything.

  • See, AES-256-CBC is the method the function uses

  • But it’s not the job builtin mysql. At least according to what you said.

  • Hi, I made my adaptations and came to a doubt, I am inserting now using mysql AES function, and then using TO_BASE64 to "fit" the code, I tried to use now select * from tabela where FROM_BASE64(aes_decrypt(campo, salt)) like '%BRASIL%' but it doesn’t work, because?

  • I cannot say, there is not enough information http://answall.com/help/mcve. Note that this is already a different problem than you originally posted in the question.

Show 6 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.