我正在 PHP 中加密和解密我的 MySQL 联系人表。这一切都很好,但我正在制作数据网格,以便它有一个搜索框,可以对所有列进行全文搜索。现在的问题是搜索是针对加密数据而不是原始数据进行的。
所以我被迫用 PHP 进行全文搜索。这需要我迭代每一行,但无论如何我都会这样做,因为我需要解密它。
我关心的是我的代码进行全文搜索的速度,我想知道是否有更好、更快的方法来做到这一点?
$result = Some PDO query that does a PDO::FETCH_ASSOC, query has about 8 columns
$searchPhrase = strtolower($searchPhrase);
$aes = new AES(DATABASE_KEY, DATABASE_IV);
$count = count($result);
for($ii = 0; $ii <$count; $ii++ )
{
$result[$ii]["ContactName"] = $aes->Decrypt($result[$ii]["ContactName"]);
$result[$ii]["ContactDescription"] = $aes->Decrypt($result[$ii]["ContactDescription"]);
/*More encrypted columns*/
if (!empty($searchPhrase)) //Only do search when search phrase not empty
{
$filterRow = array_slice($result[$ii], 3); // Do not take first 3 columns
$data = strtolower(implode("", $filterRow)); //Flaten row as one string, all lowercase
if (strpos($data, $searchPhrase) === FALSE) //Now doing elimentation from array if not match
unset($result[$ii]);
}
}
最佳答案
唯一secure and efficient way to search encrypted data in PHP + MySQL是通过精确匹配,使用一种称为盲索引的技术。
- 存储
E(M, K1) || H(E(M, K1), K2)
(例如,用defuse/php-encryption
加密的消息)在一列中 - 将
H(M, K3)
存储在第二列中
伪代码
$cipher = Crypto::encrypt($message, $encryptionKey); // takes care of K1 and K2 for you
$blind_idx = hash_hmac('sha384', $message, $unrelatedKey); // K3
关于PHP 关联数组上的全文搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34632637/