PHP 搜索功能,比较加密值
我正在制作一个搜索功能,其中搜索值应与数据库表中的两列进行比较。一列只是一个名称,另一列是格式为“xxxxxx-xxxx”(仅限数字)的加密值。用户应该能够只搜索表中整个字符串的一部分。
对于名称比较,我使用 where name LIKE %search_value%
,但对于加密值,我不能使用这种方式。
关于如何进行比较的好方法有什么想法吗?
I am making a search feature where the search value should be compared with two columns in a db table. One column is just a name and the other column is en encrypted value in format "xxxxxx-xxxx" (only numbers). The user should be able to just search for part of the total string in the table.
For the name comparison I use where name LIKE %search_value%
, but for the encrypted value I can't use that way of doing it.
Any ideas to how a good way of doing the comparison would be?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您不能使用通配符搜索加密值,因为“a”的加密与“bac”的加密完全不同。没有实用的方法可以在加密字段中进行子字符串匹配。然而,简单的直接相等测试是可行的。如果您是像 mysql 的 aes_encrypt() 这样的数据库端函数,那么您可以执行
对于子字符串匹配,您必须首先解密该字段:
You couldn't use a wildcard search for crypted values, because the crypting of 'a' is ENTIRELY and UTTERLY different than the crypting of 'bac'. There's no practical method of doing sub-string matching within a crypted field. However, a simple direct equality test is doable. If you're a DB-side function like mysql's aes_encrypt(), then you could do
For substring matching, you'd have to decrypt the field first:
基本上,如果需要加密,系统的任何部分都不应能够搜索到它。如果它应该是可搜索的,那么它可能不需要加密。
否则你就违背了加密的目的。
basically, if it needs to be encrypted, no part of the system should be able to search it. if it should be searchable, then it probably doesn't need to be encrypted.
otherwise you are kind of defeating the purpose of encryption.
如果您正在寻找加密数据的全文搜索功能(数据库服务器无法解密消息),那么您就处于学术研究领域。
但是,如果您只需要对加密数据进行有限的搜索功能子集,则可以使用 盲索引 从明文构造,可在 SELECT 查询中使用。
因此,
您可以这样做:
然后向其传递三个截断的哈希函数输出,您将获得具有匹配密文的数据库记录。
这不仅仅是理论上的评论,您现在就可以使用这个开源库来实现这一点。
If you're looking for full-text search capability of encrypted data (without the database server being able to decrypt the messages), you're in academic research territory.
However, if you only need a limited subset of searching capabilities on encrypted data, you can use blind indexes constructed from the plaintext which can be used in SELECT queries.
So instead of:
You could do this:
And then pass it three truncated hash function outputs, and you'll get your database records with matching ciphertexts.
This isn't just a theoretical remark, you can actually do this today with this open source library.