首页 > 技术文章 > KingbaseES 表空间加密-透明加密

kingbase 2021-06-26 14:50 原文

透明存储加密是指数据在写到磁盘上时对其进行加密,当授权用户重新读取数据时再对其进行解密。加密解密过程对用户都是透明的,无需对应用程序进行修改,授权用户甚至不会注意到数据已经在存储介质上加密。当前的透明加密下只支持表空间加密。

以下所有的用例都是基于KingbaseES V8R6版本进行过测试。

一、配置透明加密

V8R6数据库内置加密算法支持sm4 和rc4,数据库在初始化时通过 -e 选项指定默认透明加密算法。没指定情况下,默认是sm4。

initdb -D data -USYSTEM -e sm4

加载加密库文件

shared_preload_libraries = 'sysencrypt'

打开钱包:创建加密表空间,必须要求钱包打开状态。建表时,wallet 可以是关闭状态,不影响加密使用。Oracle 创建及访问加密表空间的表,要求wallet 必须是打开的。

test=# openup wallet with password "123456" ;
OPEN WALLET
test=# closeup wallet with password "123456" ;
CLOSE WALLET
test=# alter wallet with password "Kingbase";
ALTER WALLET
test=# openup wallet with password "Kingbase" ;
OPEN WALLET

创建加密表空间

test=# CREATE TABLESPACE ENC_TS LOCATION '/data/tbs01' with (encryption = true);
CREATE TABLESPACE
test=# select * from pg_tablespace;
  oid  |   spcname   | spcowner | spcacl |    spcoptions     
-------+-------------+----------+--------+-------------------
  1663 | sys_default |       10 |        | 
  1664 | sys_global  |       10 |        | 
  1986 | sysaudit    |       10 |        | 
 71438 | enc_ts      |       10 |        | {encryption=true}
(4 rows)

二、数据表加密结果验证

1、创建数据表

test=# create table t1_encrypt(id integer) tablespace ENC_TS;
CREATE TABLE
test=# insert into t1_encrypt select generate_series(1,100);
INSERT 0 100
test=# create table t1_noencrypt(id integer);
CREATE TABLE
test=# insert into t1_noencrypt select generate_series(1,100);
INSERT 0 100

test=# select tablename,tablespace from pg_tables where tablename in ('t1_encrypt','t1_noencrypt');
  tablename   | tablespace 
--------------+------------
 t1_encrypt   | enc_ts
 t1_noencrypt | 
(2 rows)

test=# select relname,reltablespace,relfilenode from pg_class where relname in ('t1_encrypt','t1_noencrypt');
relname | reltablespace | relfilenode
--------------+---------------+-------------
t1_encrypt | 71438 | 71439
t1_noencrypt | 0 | 71442
(2 rows)

2、验证加密结果

非加密表:

[kingbase@dbhost03 ~]$ hexdump -c /data/kbdata/base/61904/71442
0000000  \0  \0  \0  \0 030   )   - 224  \0  \0  \0  \0 250 001 200 023
0000010  \0     004      \0  \0  \0  \0 340 237   8  \0 300 237   8  \0
0000020 240 237   8  \0 200 237   8  \0   ` 237   8  \0   @ 237   8  \0
0000030     237   8  \0  \0 237   8  \0 340 236   8  \0 300 236   8  \0
0000040 240 236   8  \0 200 236   8  \0   ` 236   8  \0   @ 236   8  \0
0000050     236   8  \0  \0 236   8  \0 340 235   8  \0 300 235   8  \0
0000060 240 235   8  \0 200 235   8  \0   ` 235   8  \0   @ 235   8  \0
0000070     235   8  \0  \0 235   8  \0 340 234   8  \0 300 234   8  \0
0000080 240 234   8  \0 200 234   8  \0   ` 234   8  \0   @ 234   8  \0
0000090     234   8  \0  \0 234   8  \0 340 233   8  \0 300 233   8  \0
00000a0 240 233   8  \0 200 233   8  \0   ` 233   8  \0   @ 233   8  \0
00000b0     233   8  \0  \0 233   8  \0 340 232   8  \0 300 232   8  \0
00000c0 240 232   8  \0 200 232   8  \0   ` 232   8  \0   @ 232   8  \0
00000d0     232   8  \0  \0 232   8  \0 340 231   8  \0 300 231   8  \0
00000e0 240 231   8  \0 200 231   8  \0   ` 231   8  \0   @ 231   8  \0

加密表:

[kingbase@dbhost03 61904]$ hexdump -c 71439 
0000000  \0  \0  \0  \0   H 345   , 224  \0  \0  \0  \0 250 001 200 023
0000010  \0     004      \0  \0  \0  \0 340 237   8  \0 300 237   8  \0
0000020 030 035   ` 330 221   } 200   v   H   p 334 315 264   -   T    
0000030 321   "   ! 333 254 347 331   c 322 306   (   P 373 301 216   \
0000040   p   J   * 270 243 351 331 334 205 035 207 221 322 202   " 355
0000050 270 267   4 312   F   H 355  \n 273   C 004   d   > 267 307 271
0000060   i   B 276   ~   m   o 202 222 317  \b   b   K   1 310 317 213
0000070 250   n 252 277   !   * 322 213   >   X 323 246   V 261 177   ,
0000080 035 364 221   i 267 334 332 342 374   E   y 270   p 373 276   1
0000090 353   j 241 024 216 275 027 243   a 335   *   0 350 301 271 035
00000a0 241 335 250 337   v 305   d   W 262 305 365   W 270   '   3 206
00000b0 301 222   M   F   c   ( 360 035 216 347   `   g   ` 321 220   W
00000c0 243 345   ^ 321   V   : 252 331 274 030 274   ( 301   x 250  \v

结果:加密的表hexdump 结果是加密的。

三、指定表空间加密密钥

创建加密表空间时,可以指定加密密钥。enckey可以不指定,系统默认分配或者申请,若指定,就用这个密钥。

CREATE TABLESPACE ENC_TS LOCATION '/data/tbs01' with (encryption = true, enckey = 'kb123456');

Note:这个加密密钥对于用户来说是透明的,enckey 是系统内部使用的,实际上没有必要,不建议用户指定。

推荐阅读