Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to encrypt emails in mysql database but still be able to query them?

I want to store the email addresses of users in a MySQL database using encryption to ensure that they won't be made public if the database gets compromised. I believe if I encrypt them with mysql's AES_ENCRYPT() function I can not create an index in an INNODB table because I have to use a BLOB datatype. If the table gets very large selects it will take a long time.

What is the best solution for securing email address but still being able to query them fast and preserve them as unique values in the column?

like image 744
bshack Avatar asked Jan 30 '11 02:01

bshack


2 Answers

When a user registers on your site, use AES_ENCRYPT() to encrypt the email.

INSERT into users (email) VALUES (AES_ENCRYPT('[email protected]', 'aeskey'));

When you query your database, you can call the AES_DECRYPT() function like this:

SELECT AES_DECRYPT(email, 'aeskey') from users;
like image 55
Cameron Tinker Avatar answered Sep 29 '22 12:09

Cameron Tinker


If you hash the addresses with SHA-256 or something similar, you can still index your tables, you can still do fast address lookups (when a user searches for [email protected], you'll just hash the input and select matching hashes in the tables).

ssh uses a very similar hashing trick. (Look for the -H option in that manpage for details.)

like image 29
sarnold Avatar answered Sep 29 '22 13:09

sarnold