天天看點

資料庫 PGP 加密算法、mode、PAD的選擇 - PG與Oracle, MySQL的差異(安全性)

PostgreSQL , Greenplum , crypt , pgcrypt , mode , padding , 算法 , aes , bf , cbc , ecb , openssl , enc , linux

PostgreSQL, Greenplum的資料加密插件pgcrypto,用于加密資料。

其中用于PGP對稱加密的函數例子:

Introduction of PGP encryption, usage of raw encryption functions is discouraged.

Encrypt/decrypt data using the cipher method specified by type.

The syntax of the type string is:

where algorithm is one of:

and mode is one of:

and padding is one of:

So, for example, these are equivalent:

In encrypt_iv and decrypt_iv, the iv parameter is the initial value for the CBC mode;

it is ignored for ECB.

It is clipped or padded with zeroes if not exactly block size.

It defaults to all zeroes in the functions without this parameter.

注意,在實際使用時,通常不需要寫mode和pad方法。這樣就帶來了一定的差異。

例如Oracle, MySQL預設的mode是ecb,并不安全。

PostgreSQL預設的mode是cbc,更加安全。

aes是基于資料塊的加密方式,也就是說,每次處理的資料時一塊(16位元組),當資料不是16位元組的倍數時填充,這就是所謂的分組密碼(差別于基于比特位的流密碼),16位元組是分組長度

分組加密的幾種模式:

ECB:是一種基礎的加密方式,密文被分割成分組長度相等的塊(不足補齊),然後單獨一個個加密,一個個輸出組成密文。

CBC:是一種循環模式,前一個分組的密文和目前分組的明文異或或操作後再加密,這樣做的目的是增強破解難度。這是PostgreSQL pgcrypto的預設MODE。

CFB/OFB:實際上是一種回報模式,目的也是增強破解的難度。

FCB和CBC的加密結果是不一樣的,兩者的模式不同,而且CBC會在第一個密碼塊運算時加入一個初始化向量。

選擇不一樣的mode,對同一串字元加密,得到的加密結果是不一樣的

1、mode=cbc

2、mode=ecb

解密時,如果使用ecb加密的串,用cbc來解密,顯然得到的結果是不對的,反之亦然

如果你發現同樣用到了AES算法加密,但是得到的加密串不一樣,那麼請注意兩者用的mode, padding是否一緻,如果不一緻,結果肯定是不一樣的。

解密與加密的算法、MODE、PADDING都需要一樣,才能保證正常的解密。

如果你的ORACLE或MYSQL用了ecb mode,那麼在PostgreSQL, Greenplum中解密時,請務必使用同樣的mode來解密。

<a href="https://www.postgresql.org/docs/10/static/pgcrypto.html">https://www.postgresql.org/docs/10/static/pgcrypto.html</a>

<a href="https://github.com/digoal/blog/blob/master/201710/20171012_01.md">《PostgreSQL 和 Greenplum pgcrypto 加解密bytea處理差異》</a>

<a href="https://github.com/digoal/blog/blob/master/201607/20160727_02.md">《固若金湯 - PostgreSQL pgcrypto加密插件》</a>

<a href="https://github.com/digoal/blog/blob/master/201305/20130522_01.md">《PostgreSQL 如何實作網絡壓縮傳輸或加密傳輸(openssl)》</a>

man openssl

man enc