在 Postgres 中搜索加密字段

Searching encrypted field in Postgres

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

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

然后在上面选择:

select * from person where first_name = pgp_sym_encrypt('test', 'password');

这 return 没有结果。

如果我将其更改为使用普通的 postgres 加密,它将 return table:

中的所有行
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');