开发者

Where should I store an encryption key for php?

开发者 https://www.devze.com 2023-03-22 06:03 出处:网络
I\'m writing a php application that accepts sensitive customer data, and so I need to encrypt it before storing it in a mysql database. I\'m going to 开发者_如何学Gouse mysql\'s built-in AES functiona

I'm writing a php application that accepts sensitive customer data, and so I need to encrypt it before storing it in a mysql database. I'm going to 开发者_如何学Gouse mysql's built-in AES functionality to do column-level encryption.

I want to avoid storing the encryption key on the server, and so i'm going to provide a web-page for an administrator to log-in, and enter the encryption key. I want to store this key in memory while the application is running, but never permanently to disk.

What is the best way to do this?

Can I modify the $_SERVER array to store information between requests? Can I store the key with apache in some way? Maybe shared memory?


Rather than rely on MySQL AES for encryption, why not use PHP's native openssl encryption scheme (a PECL extension). This requires a private and public key, public to encrypt, private to decrypt, and the keys can be kept in separate places.


I ended up storing the encryption key in an in-memory table. All access to the database is done through a set of stored procedures; The stored procedures include the ability to do key management (i.e. insert key to memory-table, change keys, check if a key has been entered etc.), as well as store/retrieve application data.

With this design, the database credentials left on the application server only have the authorization to query through the set of defined procedures, and have no way to read the encryption key directly.

I liked this approach because:

  1. The key isn't stored on the file system - preventing issues with hardware disposal at end-of-life, and from prying eyes of system administrators
  2. The application code can't get access to the key (other than while entering it), so the only place it will ever reside is within the database process.
  3. All logic for encryption/decryption is embedded within the SQL queries, so don't need to worry about application code doing it correctly - Nice for maintenance.


One possibility is to create a RAM disk and store the key there.


The safest place to store any kind of encryption key is on the server NOT in the database, and make sure it is owned by root and not readable by others.


Write a php config file and store it in your home directory. Allow only php to have access to it.

$cryptKey = "aac1ebadcfabdef72376acd" ;

Include at the top of every php page that uses the encryption key using an absolute path to the home folder. This folder is not accessible to the end user.

0

精彩评论

暂无评论...
验证码 换一张
取 消