Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL encrypted columns

Say each row in a table has data pertaining to one particular user. The user has a password to access the system.

How do I encrypt a column of data using InnoDB so that no one other than the user who's data it is can read the data ? I was thinking of something like using one of the MySQL encryption functions (say AES) with a key based on a hash calculated from the user's password.

Does any one have any pointers to how I could do this ? Am I on the right track ?

One of the answers below

The issue of modifying user's password involves re-encrypting the user key by means of the new password which is much more straight forward than re-encrypting the whole bunch of user's data that can be arbitrarily large. The user key remains the same accross the life of the user data in the system.

How does this help ? Say the password is pass1. And there are a bunch of records encrypted with a key generated from this. If the user now resets the password to pass2, I have no way of decrypting the data that was encrypted using pass1. In the case of a user forgetting the password entirely, all his encrypted data will be lost.

like image 927
alephnull Avatar asked Oct 26 '08 10:10

alephnull


People also ask

Can we encrypt data in MySQL?

To enable encryption for the mysql system tablespace, specify the tablespace name and the ENCRYPTION option in an ALTER TABLESPACE statement. mysql> ALTER TABLESPACE mysql ENCRYPTION = 'Y'; To disable encryption for the mysql system tablespace, set ENCRYPTION = 'N' using an ALTER TABLESPACE statement.

How does MySQL store encrypted data?

For storage of encrypted data, you could use a BLOB field, and use MySQL's built in encryption functions. Example: update mytable set myfield = AES_ENCRYPT('some value', SHA2('your secure secret key', 512)); If you prefer to do the encryption/decryption in the application code, take a look at PHP's Mcrypt functions.

How can I tell if a table is encrypted in MySQL?

For single tablespaces, verify the ENCRYPTION option using INFORMATION_SCHEMA. TABLES and the CREATE OPTIONS settings. A flag field in the INFORMATION_SCHEMA. INNODB_TABLESPACES has bit number 13 set if the tablespace is encrypted.

How do I encrypt a table in MySQL?

To encrypt data in an InnoDB file-per-table tablespace, run ALTER TABLE tbl_name ENCRYPTION = 'Y' . To encrypt a general tablespace or the mysql tablespace, run ALTER TABLESPACE tablespace_name ENCRYPTION = 'Y' . Encryption support for general tablespaces was introduced in MySQL 8.0.


2 Answers

I don't know if there is much sense in encrypting data with user's password hash, especially if you keep hash itself in the database. In that case anyone who can access the encrypted data can also access the password hash and decrypt the data.

Another approach would be to encrypt the data with the application-specific key salted with some user-specific data. However, then you face another problem: how to securely store the application key. To that question I do not know an easy answer, but keeping it in your source code is probably good enough if you fear that your database data can be compromised, but not the source code itself, e.g. if your database is stored off-site (think Amazon S3).

Salting the app key with the user's password helps if you keep only password's hash in the database, but can introduce another security flaw: you have to keep user's password in clear text in the applications session.

As for technical solution, it is quite simple and sample code is available. You could modify it as follows to encrypt the data with the application password salted with password hash:

INSERT INTO secure_table VALUES (
  1,
  AES_ENCRYPT(
    'plain text data',
    CONCAT(@application_password, @user_password))
);

In any case you would have to store your application password somewhere so I don't think that there is an easy approach that provides perfect security.

Another approach I can think of is to ask user for a short PIN which you could use as an encryption key. The PIN would not be stored in the database, but you would need to ask user for it every time you access their data.

And of course your have to think of is the feasibility of the encryption. You won't be able to index or to search it without decryption. It is probably required for a limited set of data (e.g. credit card number), but I wouldn't go to far with it.

like image 195
Damir Zekić Avatar answered Oct 04 '22 16:10

Damir Zekić


To clarify one of the answers mentioned in the question: "user/app key" is a randomly generated private key, which is used to encrypt the data. The private key never changes (unless it's compromised). You encrypt and store the private key with a password. Since the private key is much smaller than the data, it's much cheaper to change the password: you simply decrypt the private key with the old password and re-encrypt it with the new password.

like image 26
ididak Avatar answered Oct 04 '22 14:10

ididak