【发布时间】:2020-02-09 16:57:52
【问题描述】:
我正在使用 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;
【问题讨论】:
-
您现在将 nonce 存储在哪里?每一行的随机数不是不同吗?
-
我将 nonce 附加到加密字符串并将其存储在数据库中。我目前唯一的想法是存储一系列随机数,并在搜索时依次使用每个随机数进行加密,进行搜索,如果匹配项返回,则尝试下一个随机数,依此类推。
-
所以过程将是... ~ 使用随机随机数(来自列表)和密钥加密名称 ~ 存储在数据库中 ~ 在搜索时,使用来自列表检查匹配〜如果没有匹配移动到下一个随机数等。我想知道这会产生多少开销以及它有多安全
-
这听起来和您的其他错误选择一样糟糕。我认为这里最大的问题是您无法在数据库中即时解密,因为 libsodium 不在 Mysql 中。不幸的是,我不认为使用 libsodium 可以优雅地解决您的问题。 This question was seeking something similar (ordering) 并建议切换到
CipherSweet。 -
是的,一直在挖掘并遇到了那个。
标签: php encryption libsodium