sql - pgp_sym_encrypt 和解密 postgresql 中的整个列

标签 sql postgresql encryption pgcrypto

我想使用 aes 256 加密技术对我的 postgreSQL 数据库中的 area_code 列进行加密。 这是发生的事情。我能够轻松地对其进行加密,但我无法对其进行解密。

这是运行良好的加密查询:

update mytable t1 set area_code = t2.area_code from (select pgp_sym_encrypt(area_code,'Password', 'compress-algo=1, cipher-algo=aes256') as area_code,area_name from mytable) t2 where t1.area_name = t2.area_name;

但是,如果我给出类似的东西,解密查询似乎不起作用

update mytable t1 set area_code = t2.area_code from (select pgp_sym_decrypt(area_code,'Password') as area_code,area_name from mytable) t2 where t1.area_name = t2.area_name;

甚至当我尝试查看解密的 area_code 时

select pgp_sym_decrypt((select area_code from ci), 'Password') ;

唯一可行的是当我使用单个记录并直接输入加密文本时。

select pgp_sym_decrypt('aes.encrypted.string.given.as.input', 'Password') ;

最佳答案

您应该将加密数据存储到 bytea 列中,而不是文本列中。您可以将其从文本转换为 bytea:

pgp_sym_decrypt(area_code::bytea,'Password')

为什么不直接更新表,而是在更新时进行退化的自连接?

关于sql - pgp_sym_encrypt 和解密 postgresql 中的整个列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26656910/

相关文章:

iOS:存储在安全区域中的 key 不支持解密

java - 如何在不更改对称 key 的情况下更改非对称 key 对以及使用 Bouncy CaSTLe 的 CMS 中的加密内容

sql - DB2 查询检索给定模式的所有表名

sql - 为不同的查询在单个表上添加多个索引

sql - 替换部分/行 Postgresql

json - 具有字符串 unicode 值的 Postgres 列

postgresql - PostgreSQL 何时舍入 double 列类型

PHP电子邮件加密

SQL 根据 IN 条件合并表行

mysql - 查询不返回所有预期的行