構造を次のように変更します。
`database`
id | domain | db_user | db_pass | pass_iv | db_name
------------------------------------------------------
1 | domainone.com | root | root | ....... | domain_one
// MORE DATABASES HERE
追加の列には、その特定のパスワードを暗号化するために使用されるIVpass_iv
が保持されます。
以下は、MySQL のAES_ENCRYPT関数を使用するよりも安全な方法で暗号化と復号化を行うサンプル PHP コードです。
<?php
$mysqlPass = '12345thispassword$'; // this is the password you want to store
$key = 'this is a secure secret key...'; // a secure secret key for encryption
$cipher = MCRYPT_RIJNDAEL_256;
$mode = MCRYPT_MODE_CBC;
$keysize = mcrypt_get_key_size($cipher, $mode);
$ivsize = mcrypt_get_iv_size($cipher, $mode);
$iv = mcrypt_create_iv($ivsize); // create random IV for encryption
$encrypted = mcrypt_encrypt($cipher, $key, $mysqlPass, $mode, $iv); // encrypt pass
この時点で、パスワードを ( columnn に)暗号化するために使用されるパスワード$encrypted
と共にデータベースに挿入します。$iv
pass_iv
復号化するには:
$key = 'this is a secure secret key...'; // a secure secret key for encryption
$cipher = MCRYPT_RIJNDAEL_256;
$mode = MCRYPT_MODE_CBC;
$keysize = mcrypt_get_key_size($cipher, $mode);
// read password info from db...this includes the encrypted pass and pass_iv
// put encrypted password into $encrypted, put pass_iv into $iv
$decrypted = mcrypt_decrypt($cipher, $key, $encrypted, $mode, $iv);
$decrypted = rtrim($decrypted, "\0");
// decrypted is now the plain-text database password.
それが役立つことを願っています。