pgcrypto 模块
加密后返回二进制的数据
-
加密解密函数
encrypt(data bytea, key bytea, type text) returns bytea
decrypt(data bytea, key bytea, type text) returns bytea
例子:
encrypt(data, 'fooz', 'bf-cbc/pad:pkcs')
encrypt('this is a message', 'key', 'aes-ecb')
decrypt('\x39c3c665757a0ff973b83fb98cc3d63f', 'key', 'aes-ecb')
-
类型转换
pgcrypto模块加密出来的二进制数据,转换成base64后与PHP加密的结果。
encode(data bytea, type text)
decode(string text, type text)
aes解密是自动识别 aes128, aes192, aes256,aes加密默认是aes128,暂时没发现怎么指定位数
1
2
3
4
5
6
7
8
9
10-- PostgreSQL加密
select encrypt('123456','haha', 'aes-ecb');
encrypt
------------------------------------
\xd6d01c6ac3ff0353ff1ccf9204f149c5
select encode('\xd6d01c6ac3ff0353ff1ccf9204f149c5', 'base64');
encode
--------------------------
1tAcasP/A1P/HM+SBPFJxQ==1
2
3-- PHP
echo openssl_encrypt('123456', 'AES-128-ECB', 'haha');
1tAcasP/A1P/HM+SBPFJxQ==1
2
3
4
5
6
7
8
9
10
11
12
13
14-- PostgreSQL解密
select decrypt('\xd6d01c6ac3ff0353ff1ccf9204f149c5','haha', 'aes-ecb');
decrypt
----------------
\x313233343536
-- 转换为utf8
select convert_from('\x313233343536', 'SQL_ASCII');
convert_from
--------------
123456
-- bytea转字符串:
select encode('\x00','escape');
php 加密解密函数
以指定的方式和 key 加密数据,返回原始或 base64 编码后的字符串。
-
1
string openssl_encrypt ( string $data , string $method , string $key [, int $options = 0 [, string $iv = "" [, string &$tag = NULL [, string $aad = "" [, int $tag_length = 16 ]]]]] )
-
1
string openssl_decrypt ( string $data , string $method , string $key [, int $options = 0 [, string $iv = "" [, string $tag = "" [, string $aad = "" ]]]] )
例子:
openssl_encrypt($data, 'AES-256-ECB', $key);
openssl_decrypt($data, 'AES-256-ECB', $key);
1 | -- 解密函数 |