在Postgres中搜索加密字段

Dal*_*001 3 sql security encryption postgresql

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

update person set first_name = pgp_sym_encrypt('test', 'password');
Run Code Online (Sandbox Code Playgroud)

然后选择:

select * from person where first_name = pgp_sym_encrypt('test', 'password');
Run Code Online (Sandbox Code Playgroud)

这将不返回任何结果。

如果我将其更改为使用常规的postgres加密,它将返回表中的所有行:

update person set first_name = encrypt('test', 'password', 'aes');
select * from person where first_name = encrypt('test', 'password', 'aes');
Run Code Online (Sandbox Code Playgroud)

我当前的postgres版本是:postgres(PostgreSQL)9.4.0。在这种情况下,first_name字段是一个bytea字段。

有谁知道为什么不能使用“ pgp_sym_encrypt”来工作?

谢谢!

Sim*_*stö 5

如果您查看PostgreSQL文档(附录F.25。pgcrypto-F.25.3。PGP加密函数):

使用String2Key(S2K)算法对给定的密码进行哈希处理。这与crypt()算法非常相似-故意降低速度并随机添加盐分 -但它会产生全长二进制密钥。

(强调我的。)

因此,每次运行时,以下结果都会不同:

select pgp_sym_encrypt('test', 'password');
Run Code Online (Sandbox Code Playgroud)

当测试使用密码时pgp_sym_decrypt,可以像这样进行测试:

select pgp_sym_decrypt(pgp_sym_encrypt('test', 'password'), 'password');
Run Code Online (Sandbox Code Playgroud)

  • 搜索会是这样吗?从'test'= pgp_sym_decrypt(first_name,'password')的人那里选择* (2认同)