When using:
$con = mysql_connect('localhost','user','password') I have read about the benefits of storing the password part elsewhere (https://stackoverflow.com/a/3354457/1704651).
I'm fairly new to PHP and want to know if my method of storing MySQL passwords is secure:
- Step one, choose a lengthy MySQL Database Password (https://www.random.org/passwords/?num=1&len=24&format=html&rnd=new)
Step two, put this in /outsidewebroot/salt.php (my actual salt is longer)
<?php $salt = sdcjbdt8veADJbyuQxsfJtYeW7tC5; ?>Step three, encode password using a temporary PHP file (which is deleted after use):
$decrypted = "my decrypted MySQL password" $key = $salt; $encrypted = base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, md5($key), $decrypted, MCRYPT_MODE_CBC, md5(md5($key)))); var_dump($encrypted);Step four, put this code in .htaccess
SetEnv encrypted-password i3NOByNkztBtEbJ8LJMt2GbX9VjMzO2MTYtBXsxyYVI=Step five, put this code where you need to connect to your database:
require_once("../../outside-webroot/salt.php") $key = $salt $decrypted = rtrim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, md5($key), base64_decode($encrypted-password), MCRYPT_MODE_CBC, md5(md5($key))), "\0"); $con = mysql_connect('localhost','user',$decrypted)
The salt in /outsidewebroot/salt.php is the same one that I use for hashing passwords in my database so I only have to include it once for two uses.
The encrypt/decrypt method I saw here: http://ideone.com/yQIAX
I have also seen this method: https://stackoverflow.com/a/3354457/1704651 using a hash instead of the encrypt/decrypt method.
Thank you for your feedback on my method.