Logo
加解密

加解密 #

语法描述类型
CREATE ENCRYPT RULE encryptRuleDefinition [, encryptRuleDefinition] …创建加密规则RDL
ALTER ENCRYPT RULE encryptRuleDefinition [, encryptRuleDefinition] …修改加密规则RDL
DROP ENCRYPT RULE tableName [, tableName] …删除加密规则RDL
SHOW ENCRYPT RULES [FROM databaseName]
SHOW ENCRYPT TABLE RULE tableName [FROM databaseName]
查看加密规则RQL
COUNT ENCRYPT RULE统计加密规则的数量,在后续版本中将废弃此语法RQL
CREATE ENCRYPT KEY MANAGER keyManagerName keyManagerDefinition创建密钥管理规则RDL
ALTER ENCRYPT KEY MANAGER keyManagerName keyManagerDefinition修改密钥管理规则RDL
DROP ENCRYPT KEY MANAGER ifExists? keyManagerName删除密钥管理规则RDL
select [,string] … [,] decrypt({tableName}, {encryptColumn}, {encryptValue}), [,string] …查询解密结果RQL

1. 创建加密规则 #

CreateEncryptRule ::=
  'CREATE' 'ENCRYPT' 'RULE' ifNotExists? encryptDefinition (',' encryptDefinition)*

ifNotExists ::=
  'IF' 'NOT' 'EXISTS'

encryptDefinition ::=
  ruleName '(' 'COLUMNS' '(' columnDefinition (',' columnDefinition)*  ')' (',' 'QUERY_WITH_CIPHER_COLUMN' '=' ('TRUE' | 'FALSE'))? ')'

columnDefinition ::=
  '(' 'NAME' '=' columnName (',' 'DATA_TYPE' '=' dataType)? (',' 'PLAIN' '=' plainColumnName (',' 'PLAIN_DATA_TYPE' '=' dataType)?)? ',' 'CIPHER' '=' cipherColumnName (',' 'CIPHER_DATA_TYPE' '=' dataType)? (',' 'ASSISTED_QUERY_COLUMN' '=' assistedQueryColumnName (',' 'ASSISTED_QUERY_DATA_TYPE' '=' dataType)?)? (',' 'LIKE_QUERY_COLUMN' '=' likeQueryColumnName (',' 'LIKE_QUERY_DATA_TYPE' '=' dataType)?)? ',' encryptAlgorithmDefinition (',' assistedQueryAlgorithmDefinition)? (',' likeQueryAlgorithmDefinition)? ')' 

encryptAlgorithmDefinition ::=
  'ENCRYPT_ALGORITHM' '(' 'TYPE' '(' 'NAME' '=' encryptAlgorithmType (',' propertiesDefinition)? ')'

assistedQueryAlgorithmDefinition ::=
  'ASSISTED_QUERY_ALGORITHM' '(' 'TYPE' '(' 'NAME' '=' encryptAlgorithmType (',' propertiesDefinition)? ')'

likeQueryAlgorithmDefinition ::=
  'LIKE_QUERY_ALGORITHM' '(' 'TYPE' '(' 'NAME' '=' encryptAlgorithmType (',' propertiesDefinition)? ')'

propertiesDefinition ::=
  'PROPERTIES' '(' key '=' value (',' key '=' value)* ')'

tableName ::=
  identifier

columnName ::=
  identifier

dataType ::=
  string

plainColumnName ::=
  identifier

cipherColumnName ::=
  identifier

assistedQueryColumnName ::=
  identifier

likeQueryColumnName ::=
  identifier

encryptAlgorithmType ::=
  string

key ::=
  string

value ::=
  literal  

参数说明

参数解释 #

名称数据类型说明
tableNameIDENTIFIER表名称
columnNameIDENTIFIER逻辑列名称
DATA_TYPEIDENTIFIER逻辑列数据类型
PLAINIDENTIFIER明文列名称
PLAIN_DATA_TYPEIDENTIFIER明文列数据类型
CIPHERIDENTIFIER加密列名称
CIPHER_DATA_TYPEIDENTIFIER加密列数据类型
ASSISTED_QUERY_COLUMNIDENTIFIER辅助查询列
ASSISTED_QUERY_DATA_TYPEIDENTIFIER辅助查询列数据类型
LIKE_QUERY_COLUMNIDENTIFIER模糊查询列
LIKE_QUERY_DATA_TYPEIDENTIFIER模糊查询列数据类型
ENCRYPT_ALGORITHMSTRING加密算法名称
ASSISTED_QUERY_ALGORITHMSTRING辅助查询算法
LIKE_QUERY_ALGORITHMSTRING模糊查询算法
  • PLAIN 指定明文数据列,CIPHER 指定密文数据列;
  • encryptAlgorithmType 指定加密算法类型,请参考 加密算法
  • 重复的 tableName 将无法被创建;
  • QUERY_WITH_CIPHER_COLUMN 支持大写或小写的 true 或 false,支持表级别和列级别的设置;
  • ASSISTED_QUERY_COLUMN 辅助查询列:在用户的 CIPHER (加密字段)以及对应的加密算法无法支持查询时,可以配置该字段用来辅助查询。例如用户配置的加密算法对于同一个值进行多次加密的结果不同,那么就无法使用 CIPHER (加密字段)进行查询,这时候就需要使用辅助查询列进行查询。辅助查询列对应的算法一般可以使用不可逆的算法,但是对于同一个值进行多次加密的结果需要相同。
  • LIKE_QUERY_COLUMN 模糊查询列:用于帮助用户进行 like 查询时使用的列。一般 CIPHER (加密字段) 配置的算法无法支持 like 查询,所以当用户需要使用 like查询时,需要配置模糊查询列以及对应的模糊查询算法。
  • ASSISTED_QUERY_ALGORITHM 辅助查询算法:即辅助查询列对应的算法。一般是不可逆,并且多次加密结果一致的算法。
  • LIKE_QUERY_ALGORITHM 模糊查询算法:即模糊查询列对应的算法,可以支持 like 查询的算法。

示例

创建数据加密规则

CREATE ENCRYPT RULE t_encrypt (
COLUMNS(
(NAME=user_id,PLAIN=user_plain,CIPHER=user_cipher,ENCRYPT_ALGORITHM(TYPE(NAME='AES',PROPERTIES('aes-key-value'='123456abc')))),
(NAME=order_id, CIPHER =order_cipher,ENCRYPT_ALGORITHM(TYPE(NAME='MD5')))
),QUERY_WITH_CIPHER_COLUMN=true),
t_encrypt_2 (
COLUMNS(
(NAME=user_id,DATA_TYPE='int(11)',PLAIN=user_plain,PLAIN_DATA_TYPE='int(11)',CIPHER=user_cipher,CIPHER_DATA_TYPE='varchar(255)',ENCRYPT_ALGORITHM(TYPE(NAME='AES',PROPERTIES('aes-key-value'='123456abc')))),
(NAME=order_id, CIPHER=order_cipher,ENCRYPT_ALGORITHM(TYPE(NAME='MD5')))
), QUERY_WITH_CIPHER_COLUMN=FALSE);

使用 ifNotExists 子句创建数据加密规则

CREATE ENCRYPT RULE IF NOT EXISTS t_encrypt (
COLUMNS(
(NAME=user_id,PLAIN=user_plain,CIPHER=user_cipher,ENCRYPT_ALGORITHM(TYPE(NAME='AES',PROPERTIES('aes-key-value'='123456abc')))),
(NAME=order_id, CIPHER =order_cipher,ENCRYPT_ALGORITHM(TYPE(NAME='MD5')))
),QUERY_WITH_CIPHER_COLUMN=true),
t_encrypt_2 (
COLUMNS(
(NAME=user_id,DATA_TYPE='int(11)',PLAIN=user_plain,PLAIN_DATA_TYPE='int(11)',CIPHER=user_cipher,CIPHER_DATA_TYPE='varchar(255)',ENCRYPT_ALGORITHM(TYPE(NAME='AES',PROPERTIES('aes-key-value'='123456abc')))),
(NAME=order_id, CIPHER=order_cipher,ENCRYPT_ALGORITHM(TYPE(NAME='MD5')))
), QUERY_WITH_CIPHER_COLUMN=FALSE);

2. 修改加密规则 #

ALTEREncryptRule ::=
  'ALTER' 'ENCRYPT' 'RULE' encryptDefinition (',' encryptDefinition)*

encryptDefinition ::=
  ruleName '(' 'COLUMNS' '(' columnDefinition (',' columnDefinition)*  ')' (',' 'QUERY_WITH_CIPHER_COLUMN' '=' ('TRUE' | 'FALSE'))? ')'

columnDefinition ::=
  '(' 'NAME' '=' columnName (',' 'DATA_TYPE' '=' dataType)? (',' 'PLAIN' '=' plainColumnName (',' 'PLAIN_DATA_TYPE' '=' dataType)?)? ',' 'CIPHER' '=' cipherColumnName (',' 'CIPHER_DATA_TYPE' '=' dataType)? (',' 'ASSISTED_QUERY_COLUMN' '=' assistedQueryColumnName (',' 'ASSISTED_QUERY_DATA_TYPE' '=' dataType)?)? (',' 'LIKE_QUERY_COLUMN' '=' likeQueryColumnName (',' 'LIKE_QUERY_DATA_TYPE' '=' dataType)?)? ',' encryptAlgorithmDefinition (',' assistedQueryAlgorithmDefinition)? (',' likeQueryAlgorithmDefinition)? ')' 

encryptAlgorithmDefinition ::=
  'ENCRYPT_ALGORITHM' '(' 'TYPE' '(' 'NAME' '=' encryptAlgorithmType (',' propertiesDefinition)? ')'

assistedQueryAlgorithmDefinition ::=
  'ASSISTED_QUERY_ALGORITHM' '(' 'TYPE' '(' 'NAME' '=' encryptAlgorithmType (',' propertiesDefinition)? ')'

likeQueryAlgorithmDefinition ::=
  'LIKE_QUERY_ALGORITHM' '(' 'TYPE' '(' 'NAME' '=' encryptAlgorithmType (',' propertiesDefinition)? ')'

propertiesDefinition ::=
  'PROPERTIES' '(' key '=' value (',' key '=' value)* ')'

tableName ::=
  identifier

columnName ::=
  identifier

dataType ::=
  string

plainColumnName ::=
  identifier

cipherColumnName ::=
  identifier

assistedQueryColumnName ::=
  identifier

likeQueryColumnName ::=
  identifier

encryptAlgorithmType ::=
  string

key ::=
  string

value ::=
  literal

参数说明

名称数据类型说明
tableNameIDENTIFIER表名称
columnNameIDENTIFIER逻辑列名称
DATA_TYPEIDENTIFIER逻辑列数据类型
PLAINIDENTIFIER明文列名称
PLAIN_DATA_TYPEIDENTIFIER明文列数据类型
CIPHERIDENTIFIER加密列名称
CIPHER_DATA_TYPEIDENTIFIER加密列数据类型
ASSISTED_QUERY_COLUMNIDENTIFIER辅助查询列
ASSISTED_QUERY_DATA_TYPEIDENTIFIER辅助查询列数据类型
LIKE_QUERY_COLUMNIDENTIFIER模糊查询列
LIKE_QUERY_DATA_TYPEIDENTIFIER模糊查询列数据类型
ENCRYPT_ALGORITHMSTRING加密算法名称
ASSISTED_QUERY_ALGORITHMSTRING辅助查询算法
LIKE_QUERY_ALGORITHMSTRING模糊查询算法
  • PLAIN 指定明文数据列,CIPHER 指定密文数据列;
  • encryptAlgorithmType 指定加密算法类型,请参考 加密算法
  • 重复的 tableName 将无法被创建;
  • QUERY_WITH_CIPHER_COLUMN 支持大写或小写的 true 或 false,支持表级别和列级别的设置;
  • ASSISTED_QUERY_COLUMN 辅助查询列:在用户的 CIPHER (加密字段)以及对应的加密算法无法支持查询时,可以配置该字段用来辅助查询。例如用户配置的加密算法对于同一个值进行多次加密的结果不同,那么就无法使用 CIPHER (加密字段)进行查询,这时候就需要使用辅助查询列进行查询。辅助查询列对应的算法一般可以使用不可逆的算法,但是对于同一个值进行多次加密的结果需要相同。
  • LIKE_QUERY_COLUMN 模糊查询列:用于帮助用户进行 like 查询时使用的列。一般 CIPHER (加密字段) 配置的算法无法支持 like 查询,所以当用户需要使用 like查询时,需要配置模糊查询列以及对应的模糊查询算法。
  • ASSISTED_QUERY_ALGORITHM 辅助查询算法:即辅助查询列对应的算法。一般是不可逆,并且多次加密结果一致的算法。
  • LIKE_QUERY_ALGORITHM 模糊查询算法:即模糊查询列对应的算法,可以支持 like 查询的算法。

示例

ALTER ENCRYPT RULE t_encrypt (
COLUMNS(
(NAME=user_id,PLAIN=user_plain,CIPHER=user_cipher,ENCRYPT_ALGORITHM(TYPE(NAME='AES',PROPERTIES('aes-key-value'='123456abc')))),
(NAME=order_id,CIPHER=order_cipher,CIPHER_DATA_TYPE='varchar(255)',ENCRYPT_ALGORITHM(TYPE(NAME='MD5')))
), QUERY_WITH_CIPHER_COLUMN=TRUE);

3. 删除加密规则 #

示例

删除加密规则

DROP ENCRYPT RULE t_encrypt, t_encrypt_2;

使用 ifExists 删除加密规则

DROP ENCRYPT RULE IF EXISTS t_encrypt, t_encrypt_2;

4. 查看加密规则 #

查询所有的数据加密规则

SHOW ENCRYPT RULES [FROM databaseName]

示例

mysql> SHOW ENCRYPT RULES FROM encrypt_db;
+-------------+--------------+-----------------+---------------+------------------+--------------+-----------------+-----------------------+--------------------------+----------------+-------------------------+--------------------------+
| table       | logic_column | logic_data_type | cipher_column | cipher_data_type | plain_column | plain_data_type | assisted_query_column | assisted_query_data_type | encryptor_type | encryptor_props         | query_with_cipher_column |
+-------------+--------------+-----------------+---------------+------------------+--------------+-----------------+-----------------------+--------------------------+----------------+-------------------------+--------------------------+
| t_encrypt   | user_id      |                 | user_cipher   |                  | user_plain   |                 |                       |                          | AES            | aes-key-value=123456abc | true                     |
| t_encrypt   | order_id     |                 | order_cipher  |                  |              |                 |                       |                          | MD5            |                         | true                     |
| t_encrypt_2 | user_id      |                 | user_cipher   |                  | user_plain   |                 |                       |                          | AES            | aes-key-value=123456abc | false                    |
| t_encrypt_2 | order_id     |                 | order_cipher  |                  |              |                 |                       |                          | MD5            |                         | false                    |
+-------------+--------------+-----------------+---------------+------------------+--------------+-----------------+-----------------------+--------------------------+----------------+-------------------------+--------------------------+
4 rows in set (0.78 sec)

输出说明

说明
table逻辑表名
logic_column逻辑列名
logic_data_type逻辑列数据类型
cipher_column密文列名
cipher_data_type密文列数据类型
plain_column明文列名
plain_data_type明文列数据类型
assisted_query_column辅助查询列名
assisted_query_data_type辅助查询列数据类型
encryptor_type加密算法类型
encryptor_props加密算法参数
query_with_cipher_column是否使用加密列进行查询

5. 查询所有的数据加密规则 #

SHOW ENCRYPT RULES [FROM databaseName]

示例

mysql> SHOW ENCRYPT RULES FROM encrypt_db;
+-------------+--------------+-----------------+---------------+------------------+--------------+-----------------+-----------------------+--------------------------+----------------+-------------------------+--------------------------+
| table       | logic_column | logic_data_type | cipher_column | cipher_data_type | plain_column | plain_data_type | assisted_query_column | assisted_query_data_type | encryptor_type | encryptor_props         | query_with_cipher_column |
+-------------+--------------+-----------------+---------------+------------------+--------------+-----------------+-----------------------+--------------------------+----------------+-------------------------+--------------------------+
| t_encrypt   | user_id      |                 | user_cipher   |                  | user_plain   |                 |                       |                          | AES            | aes-key-value=123456abc | true                     |
| t_encrypt   | order_id     |                 | order_cipher  |                  |              |                 |                       |                          | MD5            |                         | true                     |
| t_encrypt_2 | user_id      |                 | user_cipher   |                  | user_plain   |                 |                       |                          | AES            | aes-key-value=123456abc | false                    |
| t_encrypt_2 | order_id     |                 | order_cipher  |                  |              |                 |                       |                          | MD5            |                         | false                    |
+-------------+--------------+-----------------+---------------+------------------+--------------+-----------------+-----------------------+--------------------------+----------------+-------------------------+--------------------------+
4 rows in set (0.78 sec)

输出说明

说明
table逻辑表名
logic_column逻辑列名
logic_data_type逻辑列数据类型
cipher_column密文列名
cipher_data_type密文列数据类型
plain_column明文列名
plain_data_type明文列数据类型
assisted_query_column辅助查询列名
assisted_query_data_type辅助查询列数据类型
encryptor_type加密算法类型
encryptor_props加密算法参数
query_with_cipher_column是否使用加密列进行查询

6. 查询指定加密规则 #

示例

mysql> SHOW ENCRYPT TABLE RULE t_encrypt;
+-------------+--------------+-----------------+---------------+------------------+--------------+-----------------+-----------------------+--------------------------+----------------+-------------------------+--------------------------+
| table       | logic_column | logic_data_type | cipher_column | cipher_data_type | plain_column | plain_data_type | assisted_query_column | assisted_query_data_type | encryptor_type | encryptor_props         | query_with_cipher_column |
+-------------+--------------+-----------------+---------------+------------------+--------------+-----------------+-----------------------+--------------------------+----------------+-------------------------+--------------------------+
| t_encrypt   | user_id      |                 | user_cipher   |                  | user_plain   |                 |                       |                          | AES            | aes-key-value=123456abc | true                     |
| t_encrypt   | order_id     |                 | order_cipher  |                  |              |                 |                       |                          | MD5            |                         | true                     |
+-------------+--------------+-----------------+---------------+------------------+--------------+-----------------+-----------------------+--------------------------+----------------+-------------------------+--------------------------+
2 rows in set (0.01 sec)

mysql> SHOW ENCRYPT TABLE RULE t_encrypt FROM encrypt_db;
+-------------+--------------+-----------------+---------------+------------------+--------------+-----------------+-----------------------+--------------------------+----------------+-------------------------+--------------------------+
| table       | logic_column | logic_data_type | cipher_column | cipher_data_type | plain_column | plain_data_type | assisted_query_column | assisted_query_data_type | encryptor_type | encryptor_props         | query_with_cipher_column |
+-------------+--------------+-----------------+---------------+------------------+--------------+-----------------+-----------------------+--------------------------+----------------+-------------------------+--------------------------+
| t_encrypt   | user_id      |                 | user_cipher   |                  | user_plain   |                 |                       |                          | AES            | aes-key-value=123456abc | true                     |
| t_encrypt   | order_id     |                 | order_cipher  |                  |              |                 |                       |                          | MD5            |                         | true                     |
+-------------+--------------+-----------------+---------------+------------------+--------------+-----------------+-----------------------+--------------------------+----------------+-------------------------+--------------------------+
2 rows in set (0.01 sec))

输出说明

说明
table逻辑表名
logic_column逻辑列名
logic_data_type逻辑列数据类型
cipher_column密文列名
cipher_data_type密文列数据类型
plain_column明文列名
plain_data_type明文列数据类型
assisted_query_column辅助查询列名
assisted_query_data_type辅助查询列数据类型
encryptor_type加密算法类型
encryptor_props加密算法参数
query_with_cipher_column是否使用加密列进行查询

7. 创建密钥管理规则 #

createEncryptKeyManager
    : CREATE ENCRYPT KEY MANAGER keyManagerName keyManagerDefinition
    ;

keyManagerDefinition
    : TYPE (NAME = keyManagerName [, PROPERTIES ([keyManagerProperties]) ])
    ;

keyManagerProperties:
    keyManagerProperty [, keyManagerProperty] ...

keyManagerProperty:
    key=value

参数说明

名称数据类型说明
keyManagerNameSTRING密钥管理器名称

示例

本地密钥管理

CREATE ENCRYPT KEY MANAGER local_key_manage (
  TYPE(NAME='LOCAL',PROPERTIES("aes-key-value"="123456abc"))
);

云端密钥管理

CREATE ENCRYPT KEY MANAGER aws_key_manage (
TYPE(NAME='SphereEx:AWS_KMS',PROPERTIES(
  "access-key"="aaaaa",
  "secret-key"="bbbbb",
  "aws-region"="us-east-1",
  "secret-name"="testA"))
);

8. 修改密钥管理规则 #

alterEncryptKeyManager
    : ALTER ENCRYPT KEY MANAGER keyManagerName keyManagerDefinition
    ;

keyManagerDefinition
    : TYPE (NAME = keyManagerName [, PROPERTIES ([keyManagerProperties]) ])
    ;

keyManagerProperties:
    keyManagerProperty [, keyManagerProperty] ...

keyManagerProperty:
    key=value

参数说明

名称数据类型说明
keyManagerNameSTRING密钥管理器名称
示例

本地密钥管理

ALTER ENCRYPT KEY MANAGER local_key_manage (
TYPE(NAME='LOCAL',PROPERTIES("aes-key-value"="123456abcd"))
);

云端密钥管理

ALTER ENCRYPT KEY MANAGER aws_key_manage (
TYPE(NAME='SphereEx:AWS_KMS',PROPERTIES(
  "access-key"="aaaaa",
  "secret-key"="bbbbb",
  "aws-region"="us-east-1",
  "secret-name"="testB"))
);

9. 删除密钥管理规则 #

DROP ENCRYPT KEY MANAGER ifExists? keyManagerName;

参数说明

名称数据类型说明
keyManagerNameSTRING密钥管理器名称

示例

DROP ENCRYPT KEY MANAGER ifExists Aws_key_Manager;

10. 查询解密结果 #

select 用法一致,提供 decrypt() 函数,tableName 为需要解密的表,encryptColumn 为需要解密的列, encryptValue 为需要解密的真实值。使用时需要配置对应的加密规则。

select [,string]  [,] decrypt({tableName}, {encryptColumn}, {encryptValue}), [,string] 

示例

SELECT Lm04PRW+sp+mG/QuimAUew==, decrypt(t_user, pwd_cipher, Lm04PRW+sp+mG/QuimAUew==) UNION SELECT XXX, decrypt(t_user, pwd_cipher, XXX);