函数加密AES-256-CBC,搜索mysql
Function encryption AES-256-CBC, search mysql
我使用 PHP 和 MySQL,并使用此函数在 PHP 中以加密形式输入数据:
function encrypt_decrypt($action, $string) {
$output = false;
$encrypt_method = "AES-256-CBC";
$secret_key = 'This is my secret key';
$secret_iv = 'This is my secret iv';
// hash
$key = hash('sha256', $secret_key);
// iv - encrypt method AES-256-CBC expects 16 bytes - else you will get a warning
$iv = substr(hash('sha256', $secret_iv), 0, 16);
if( $action == 'encrypt' ) {
$output = openssl_encrypt($string, $encrypt_method, $key, 0, $iv);
$output = base64_encode($output);
}
else if( $action == 'decrypt' ){
$output = openssl_decrypt(base64_decode($string), $encrypt_method, $key, 0, $iv);
}
return $output;
}
我最终 运行 遇到了一个大问题。
我需要搜索table,需要使用MySQL LIKE
功能。
我该怎么做?
我查找了文档并发现 MySQL 具有 AES 加密,但我完全迷失了。
PHPMySQL中写的这个函数存在,还是让它起作用?
当您将密文输入 MySQL 时,
- 已在 MySQL
之外加密
- 不保留相似性(因为大多数加密不这样做)
您无法对其执行 LIKE
搜索。
如果将加密移动到数据库中,您可以执行 LIKE
搜索,但要准备好承受严重的计算性能损失:基本上 MySQL 除了解密整个列之外LIKE
本身涉及的成本。
最好的选择是搜索一些重新设计的选项,这样您就不会那样做。
我使用 PHP 和 MySQL,并使用此函数在 PHP 中以加密形式输入数据:
function encrypt_decrypt($action, $string) {
$output = false;
$encrypt_method = "AES-256-CBC";
$secret_key = 'This is my secret key';
$secret_iv = 'This is my secret iv';
// hash
$key = hash('sha256', $secret_key);
// iv - encrypt method AES-256-CBC expects 16 bytes - else you will get a warning
$iv = substr(hash('sha256', $secret_iv), 0, 16);
if( $action == 'encrypt' ) {
$output = openssl_encrypt($string, $encrypt_method, $key, 0, $iv);
$output = base64_encode($output);
}
else if( $action == 'decrypt' ){
$output = openssl_decrypt(base64_decode($string), $encrypt_method, $key, 0, $iv);
}
return $output;
}
我最终 运行 遇到了一个大问题。
我需要搜索table,需要使用MySQL LIKE
功能。
我该怎么做?
我查找了文档并发现 MySQL 具有 AES 加密,但我完全迷失了。
PHPMySQL中写的这个函数存在,还是让它起作用?
当您将密文输入 MySQL 时,
- 已在 MySQL 之外加密
- 不保留相似性(因为大多数加密不这样做)
您无法对其执行 LIKE
搜索。
如果将加密移动到数据库中,您可以执行 LIKE
搜索,但要准备好承受严重的计算性能损失:基本上 MySQL 除了解密整个列之外LIKE
本身涉及的成本。
最好的选择是搜索一些重新设计的选项,这样您就不会那样做。