我在做字段加密/解密。
我选择的是
select encrypt('123456789012345','1234','aes');
encrypt
------------------------------------
\x34591627f9c8eae417fc7cbbf458592c
(1 row)我对我的数据进行了加密,另一个字符串在解密后仍然存在,如下所示…
postgres=# select decrypt('\x34591627f9c8eae417fc7cbbf458592c','1234','aes');
decrypt
----------------------------------
\x313233343536373839303132333435
(1 row)我走错路了吗?(我知道这样的询问可能很愚蠢……)
我要做的就是以一种最简单的方式获得加密数据,而且加密的数据很小……
先谢谢你...
发布于 2012-09-26 17:49:41
decrypt函数返回的是一个字节字符串,而不是一个字符串,所以它以十六进制表示法显示。实际值是相同的\x31 = 1、\x32 =2等等。
您需要将返回值转换回文本。
例如:
select convert_from(decrypt('\x34591627f9c8eae417fc7cbbf458592c','1234','aes'),'SQL_ASCII');
convert_from
-----------------
123456789012345
(1 row)Postgresql string functions
发布于 2018-08-15 10:42:42
谢谢你,Gary!
此外,如果您在表查询中使用decrypt,则必须专门将该列转换为bytea类型。例如,如果您具有以下内容:
CREATE TABLE public.test_crypto
(
id bigint NOT NULL DEFAULT nextval('test_crypto_id_seq'::regclass),
plain_text text COLLATE pg_catalog."default",
crypted_text text COLLATE pg_catalog."default",
CONSTRAINT test_crypto_pkey PRIMARY KEY (id)
)
WITH (
OIDS = FALSE
)
TABLESPACE pg_default;您可以像这样加密数据:
insert into public.test_crypto (plain_text, crypted_text)
values ('plaintext', encrypt('plaintext', 'salty', 'aes'))然后像这样解密:
select id, plain_text,
convert_from(decrypt(crypted_text::bytea, 'salty', 'aes'), 'SQL_ASCII')
from test_crypto如果你不使用crypted_text::bytea,SQL解析器会因为找不到你所说的“解密”函数而大喊大叫。
https://stackoverflow.com/questions/12598382
复制相似问题