我使用 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;
}
最后我遇到了一个大问题。
我需要查表,需要用到MySQL的LIKE
函数。
我该怎么做?
我查找了文档,发现 MySQL 具有 AES 加密,但我完全迷失了。
这个用 PHP MySQL 写的函数是存在的,还是让它起作用?
最佳答案
当你将密文输入MySQL时,即
- 已在 MySQL 之外加密
- 不保留相似性(因为大多数加密不这样做)
您不能对其执行LIKE
搜索。
如果您将加密移动到数据库中,您可以执行 LIKE
搜索,但要准备好承受严重的计算性能损失:基本上,除了与LIKE
本身。
最好的选择是搜索一些重新设计的选项,让您根本不这样做。
关于php - 函数加密AES-256-CBC,搜索mysql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28197763/