【问题标题】:Searching encrypted field in Postgres在 Postgres 中搜索加密字段
【发布时间】:2015-03-18 12:55:15
【问题描述】:

我正在尝试使用“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”不起作用?

谢谢!

【问题讨论】:

    标签: sql postgresql security encryption


    【解决方案1】:

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

    使用 String2Key (S2K) 算法对给定密码进行哈希处理。这与 crypt() 算法非常相似——故意缓慢且使用随机盐——但它会生成一个完整的二进制密钥。

    (强调我的。)

    因此,每次运行时,以下内容都会给出不同的结果:

    select pgp_sym_encrypt('test', 'password');
    

    测试密码时使用pgp_sym_decrypt,可以这样测试:

    select pgp_sym_decrypt(pgp_sym_encrypt('test', 'password'), 'password');
    

    【讨论】:

    • 那么,如果我想对使用 S2K 算法加密的数据进行搜索,我唯一的选择是在该数据的安全哈希上设置一个单独的索引吗?
    • 搜索会是这样的吗? select * from person where 'test' = pgp_sym_decrypt(first_name, 'password');
    猜你喜欢
    • 2014-09-02
    • 1970-01-01
    • 1970-01-01
    • 2012-08-28
    • 2015-10-06
    • 2020-05-03
    • 2015-08-20
    • 2010-11-16
    • 1970-01-01
    相关资源
    最近更新 更多