首页 > 解决方案 > 如何搜索由 libSodium 加密的数据库列?

问题描述

我正在使用 SODIUM 加密存储在数据库中的个人数据。我可以愉快地对存储的数据进行加密和解密。存储在数据库中时,我正在加密名字和姓氏、电话号码、电子邮件地址等。

但是我不知道如何搜索加密数据。任何人都可以提供加密数据然后能够搜索它的指针吗?

例如,我需要按名字、姓氏等进行搜索,但这是加密的。

我正在使用此代码来搜索并“愚蠢地”考虑加密名称,但当然会重新加密它,然后它与实际记录不同。

public function searchStaff($string) {
  $this->db->query('SELECT * FROM staff WHERE lastName IN (:unEncrypted, :encrypted)');
  $this->db->bind(':unEncrypted', $string);
  $this->db->bind(':encrypted', $string);
  $results = $this->db->resultSet();
  return $results;
}

我什至不知道该怎么做,到目前为止我唯一的想法是解密每一行,检查并返回,但这是一种明显有缺陷的看待它的方式,尤其是当表变大时!

我正在使用下面的代码在列中创建加密条目。我目前唯一的想法是将 $nonce 存储在数据库行中并使用它依次解密每一行?但这会产生巨大的开销?

人们如何确保个人数据的安全?

//create random number
$nonce = random_bytes(SODIUM_CRYPTO_SECRETBOX_NONCEBYTES);

//encrypt the input
//to encrypt the value we pass it to sodium_crypto_secretbox() with our key
//and a $nonce. The nonce is generated using random_bytes(), because the 
//same nonce should never be reused.

$cipher = sodium_crypto_secretbox($data, $nonce, CRYPTOKEY);

//This presents a problem because we need the nonce to decrypt the value 
//later.
//Luckily, nonces don’t have to be kept secret so we can prepend it to our 
//$ciphertext then base64_encode() the value before saving it to the 
//database.

$encoded = base64_encode($nonce . $cipher);

sodium_memzero($data);

return $encoded;

标签: phpencryptionlibsodium

解决方案


从根本上说......如果数据被加密,你就无法搜索它。解密每条记录以查看它是否包含特定值是非常难以管理的。

在这种情况下,我认为加密是不必要的。诸如信用卡号之类的真正机密信息可能需要加密,例如以满足“PCI 合规性”标准,但这些数据永远不会被“搜索”。我认为加密姓名和地址等内容没有任何价值。只需确保访问控制规则适合您的数据库。


推荐阅读