gpt4 book ai didi

sql - 在 Postgres 中搜索加密字段

转载 作者:行者123 更新时间:2023-12-04 12:48:13 25 4
gpt4 key购买 nike

我正在尝试使用“pgp_sym_encrypt”在 postgres 中查询加密字段。我通过将表中的所有名字设置为加密值来运行我的测试:

update person set first_name = pgp_sym_encrypt('test', 'password');

然后选择它:
select * from person where first_name = pgp_sym_encrypt('test', 'password');

这不返回任何结果。

如果我将其更改为使用普通的 postgres 加密,它将返回表中的所有行:
update person set first_name = encrypt('test', 'password', 'aes');
select * from person where first_name = encrypt('test', 'password', 'aes');

我当前的 postgres 版本是:postgres (PostgreSQL) 9.4.0。
本例中的 first_name 字段是一个 bytea 字段。

有谁知道为什么使用“pgp_sym_encrypt”不起作用?

谢谢!

最佳答案

如果您查看 PostgreSQL 文档 ( Appendix F.25. pgcrypto - F.25.3. PGP Encryption Functions ):

The given password is hashed using a String2Key (S2K) algorithm. This is rather similar to crypt() algorithms — purposefully slow and with random salt — but it produces a full-length binary key.



(强调我的。)

因此,每次运行时,以下结果都会给出不同的结果:
select pgp_sym_encrypt('test', 'password');

测试密码时使用 pgp_sym_decrypt相反,它可以像这样测试:
select pgp_sym_decrypt(pgp_sym_encrypt('test', 'password'), 'password');

关于sql - 在 Postgres 中搜索加密字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29122667/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com