将您的结构更改为:
`database`
id | domain | db_user | db_pass | pass_iv | db_name
------------------------------------------------------
1 | domainone.com | root | root | ....... | domain_one
// MORE DATABASES HERE
附加列pass_iv
将保存用于加密该特定密码的IV 。
这是一些示例 PHP 代码,它以比使用 MySQL 的AES_ENCRYPT函数更安全的方式进行加密和解密。
<?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
此时,将密码与用于加密密码的密码$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.
希望有帮助。