Alex Rivera | Logout

What's the best method to use / store encryption keys in MySQL

Asked 2008-10-24T01:43:14.633
12

I plan on using MySQL and it's built-in encryption functionality to encrypt / decrypt certain columns in certain tables. The concern I have is that I need to store the key somewhere. I could certainly store the key in a file and control the permissions of that file and the permissions of the application that accesses it, but is that enough? I could also create a web service to get the key or something.

I am in a small shop where I would be the only one (possibly one other person) that would have access to the machine that the application was on. Edit: I should add that there is a web facing part of this application that would need to decrypt the data unless I added a tier.

I have looked ad nauseum, but no one seems to have a bulletproof answer.

Is this one of those problems where you have to settle for good enough? Given that I am using MySQL and PHP (possibly Python) is there a better way to approach this?

Edit
Report

1 Answer

4

I'd probably store it in a file that is in a non-web-accessible directory and locked down with file system permissions as much as possible.

Your web-facing script should not open any file system files using variables, especially user-provided ones. Don't even give them the option of slipping something passed your input filter (you are filtering your user-provided data right?) and possibly giving up the contents of the key file. Keep file paths to hard-coded strings and define()'s only. Since most of your data is stored in MySQL, this shouldn't be a problem.

When doing the decryption, read the key into a cleanly initialized variable, do the decryption, then overwrite the key variable (say with a string of x's) and unset the variable. This probably all sounds a bit paranoid but if you minimize the time the key is in the clear in memory and isolate it from all the other variables flying around your PHP script it can't make you any less secure.

If you and the one other person are the only ones that have physical access to the machine, this is probably just fine. If someone breaks in and steals the box, well, they've got all your data anyway so game over.

answered 2008-10-24T08:56:25.913

Your Answer