# DBE_OBFUSCATION_TOOLKIT
#### 接口介绍
![](https://support.huaweicloud.com/distributed-ref-v10-gaussdb/public_sys-resources/notice_3.0-zh-cn.png)
由于DES算法、DES3算法和MD5哈希算法存在算法安全风险，请谨慎使用DBE_OBFUSCATION_TOOLKIT进行加解密操作。
DBE_OBFUSCATION_TOOLKIT高级功能包是GaussDB提供的兼容性功能包，主要用于支持DES、DES3、MD5哈希算法的接口，以保持与原有业务逻辑的兼容。DBE_OBFUSCATION_TOOLKIT支持的接口请参见[表1]。
 表1DBE_OBFUSCATION_TOOLKIT 
| 接口名称                                                                                   | 描述                |
|:---|:---|
| [DBE_OBFUSCATION_TOOLKIT.DESGETKEY]    | 生成DES算法的密钥。      |
| [DBE_OBFUSCATION_TOOLKIT.DESENCRYPT]  | 使用DES算法对数据进行加密。    |
| [DBE_OBFUSCATION_TOOLKIT.DESDECRYPT]   | 使用DES算法对数据进行解密。   |
| [DBE_OBFUSCATION_TOOLKIT.DES3GETKEY]  | 生成DES3算法的密钥。      |
| [DBE_OBFUSCATION_TOOLKIT.DES3ENCRYPT] | 使用DES3算法对数据进行加密。  |
| [DBE_OBFUSCATION_TOOLKIT.DES3DECRYPT]   | 使用DES3算法对数据进行解密。 |
| [DBE_OBFUSCATION_TOOLKIT.MD5]          | 计算数据的MD5哈希值。       |
   
- DBE_OBFUSCATION_TOOLKIT.DESGETKEY
  功能：生成DES算法的密钥。
  DBE_OBFUSCATION_TOOLKIT.DESGETKEY原型可以分为2种：
  ```
  --原型1：入参为VARCHAR2类型
  DBE_OBFUSCATION_TOOLKIT.DESGETKEY(
  seed_string IN VARCHAR2
  ) RETURN VARCHAR2;
  --原型2：入参为RAW类型
  DBE_OBFUSCATION_TOOLKIT.DESGETKEY(
  seed IN RAW
  ) RETURN RAW;
  ```
  表2DBE_OBFUSCATION_TOOLKIT.DESGETKEY接口参数说明 
  | 参数          | 类型     | 是否可以为空 | 描述                      |
  |:---|:---|:---|:---|
  | seed_string | VARCHAR2 | 否      | 用于生成密钥的种子值，其长度至少为80字节。 |
  | seed        | RAW    | 否       | 用于生成密钥的种子值，其长度至少为80字节。 |
     
  示例：
  ```
  -- 创建ORA兼容模式的数据库
  gaussdb=# CREATE DATABASE ora_db dbcompatibility = 'ORA' encoding = 'utf8';
  gaussdb=# \c ora_db
  -- 生成随机密钥
  ora_db=# SELECT rawtohex(dbe_raw.cast_from_varchar2_to_raw(dbe_obfuscation_toolkit.desgetkey(seed_string => '00000000000000000000000000000000000000000000000000000000000000000000000000000000')));
  WARNING:  The DES algorithm is an insecure algorithm, please use it with caution.
  CONTEXT:  referenced column: rawtohex
       rawtohex     
  ------------------
   A7F0B24CF66DAECF
  (1 row)
  ora_db=# SELECT rawtohex(dbe_obfuscation_toolkit.desgetkey(seed => dbe_raw.cast_from_varchar2_to_raw('00000000000000000000000000000000000000000000000000000000000000000000000000000000')));
  WARNING:  The DES algorithm is an insecure algorithm, please use it with caution.
  CONTEXT:  referenced column: rawtohex
       rawtohex     
  ------------------
   2A5984CA3AB67344
  (1 row)
  ```
  ![](https://support.huaweicloud.com/distributed-ref-v10-gaussdb/public_sys-resources/note_3.0-zh-cn.png)
  DBE_OBFUSCATION_TOOLKIT.DESGETKEY生成的密钥为随机密钥。
  
- DBE_OBFUSCATION_TOOLKIT.DESENCRYPT
  功能：使用DES算法对数据进行加密。
  DBE_OBFUSCATION_TOOLKIT.DESENCRYPT原型可以分为2种：
  ```
  --原型1：入参为VARCHAR2类型
  DBE_OBFUSCATION_TOOLKIT.DESENCRYPT(
  input_string IN VARCHAR2,
  key_string   IN VARCHAR2
  ) RETURN VARCHAR2;
  --原型2：入参为RAW类型
  DBE_OBFUSCATION_TOOLKIT.DESENCRYPT(
  input IN RAW,
  key   IN RAW
  ) RETURN RAW;
  ```
  表3DBE_OBFUSCATION_TOOLKIT.DESENCRYPT接口参数说明 
  | 参数          | 类型         | 是否可以为空 | 描述                              |
  |:---|:---|:---|:---|
  | input_string | VARCHAR2 | 否        | 待加密的数据，其长度必须为8字节的倍数。            |
  | input      | RAW      | 否       | 待加密的数据，其长度必须为8字节的倍数。            |
  | key_string   | VARCHAR2  | 否        | 加密密钥，其长度至少为8字节，超过8字节的数据不影响加密结果。 |
  | key          | RAW        | 否       | 加密密钥，其长度至少为8字节，超过8字节的数据不影响加密结果。 |
     
  示例：
  ```
  ora_db=# SELECT rawtohex(dbe_raw.cast_from_varchar2_to_raw(dbe_obfuscation_toolkit.desencrypt(input_string => '12345678', key_string => '12345678')));
  WARNING:  The DES algorithm is an insecure algorithm, please use it with caution.
  CONTEXT:  referenced column: rawtohex
       rawtohex     
  ------------------
   96D0028878D58C89
  (1 row)
  ora_db=# SELECT rawtohex(dbe_obfuscation_toolkit.desencrypt(input => dbe_raw.cast_from_varchar2_to_raw('12345678'), key => dbe_raw.cast_from_varchar2_to_raw('12345678')));
  WARNING:  The DES algorithm is an insecure algorithm, please use it with caution.
  CONTEXT:  referenced column: rawtohex
       rawtohex     
  ------------------
   96D0028878D58C89
  (1 row)
  ```
  ![](https://support.huaweicloud.com/distributed-ref-v10-gaussdb/public_sys-resources/note_3.0-zh-cn.png)
  由于该函数执行过程需传入密钥，出于安全考虑，gsql工具不会将包含该函数名称的SQL记录至执行历史。因此，无法在gsql中通过上下翻页功能查找该函数的执行历史。
  
- DBE_OBFUSCATION_TOOLKIT.DESDECRYPT
  功能：使用DES算法对数据进行解密。
  DBE_OBFUSCATION_TOOLKIT.DESDECRYPT原型可以分为2种：
  ```
  --原型1：入参为VARCHAR2类型
  DBE_OBFUSCATION_TOOLKIT.DESDECRYPT(
  input_string IN VARCHAR2,
  key_string   IN VARCHAR2
  ) RETURN VARCHAR2;
  --原型2：入参为RAW类型
  DBE_OBFUSCATION_TOOLKIT.DESDECRYPT(
  input IN RAW,
  key   IN RAW
  ) RETURN RAW;
  ```
  表4DBE_OBFUSCATION_TOOLKIT.DESDECRYPT接口参数说明 
  | 参数          | 类型       | 是否可以为空 | 描述                           |
  |:---|:---|:---|:---|
  | input_string | VARCHAR2  | 否      | 待解密的数据，其长度必须为8字节的倍数。         |
  | input          | RAW      | 否      | 待解密的数据，其长度必须为8字节的倍数。         |
  | key_string   | VARCHAR2 | 否      | 解密密钥，其长度至少为8字节，超过8字节的数据不影响解密结果。 |
  | key           | RAW    | 否      | 解密密钥，其长度至少为8字节，超过8字节的数据不影响解密结果。 |
     
  示例：
  ```
  ora_db=# SELECT rawtohex(dbe_raw.cast_from_varchar2_to_raw(dbe_obfuscation_toolkit.desdecrypt(input_string => '12345678', key_string => '12345678')));
  WARNING:  The DES algorithm is an insecure algorithm, please use it with caution.
  CONTEXT:  referenced column: rawtohex
       rawtohex     
  ------------------
   F5E8E9EB81F28B73
  (1 row)
  ora_db=# SELECT rawtohex(dbe_obfuscation_toolkit.desdecrypt(input => dbe_raw.cast_from_varchar2_to_raw('12345678'), key => dbe_raw.cast_from_varchar2_to_raw('12345678')));
  WARNING:  The DES algorithm is an insecure algorithm, please use it with caution.
  CONTEXT:  referenced column: rawtohex
       rawtohex     
  ------------------
   F5E8E9EB81F28B73
  (1 row)
  ```
  ![](https://support.huaweicloud.com/distributed-ref-v10-gaussdb/public_sys-resources/note_3.0-zh-cn.png)
  由于该函数执行过程需传入密钥，出于安全考虑，gsql工具不会将包含该函数名称的SQL记录至执行历史。因此，无法在gsql中通过上下翻页功能查找该函数的执行历史。
  
- DBE_OBFUSCATION_TOOLKIT.DES3GETKEY
  功能：生成DES3算法的密钥。
  DBE_OBFUSCATION_TOOLKIT.DES3GETKEY原型可以分为2种：
  ```
  --原型1：入参为VARCHAR2类型
  DBE_OBFUSCATION_TOOLKIT.DES3GETKEY(
  which       IN INTEGER DEFAULT 0,
  seed_string IN VARCHAR2
  ) RETURN VARCHAR2;
  --原型2：入参为RAW类型
  DBE_OBFUSCATION_TOOLKIT.DES3GETKEY(
  which IN INTEGER DEFAULT 0,
  seed  IN RAW
  ) RETURN RAW;
  ```
  表5DBE_OBFUSCATION_TOOLKIT.DES3GETKEY接口参数说明 
  | 参数         | 类型      | 是否可以为空 | 描述                                                                                                                                                                                                                                                                                                                                                                                                              |
  |:---|:---|:---|:---|
  | which      | INTEGER  | 否      | 密钥模式。 取值范围：0或1 - 0：返回16字节的密钥（即两个密钥）。  - 1：返回24字节的密钥（即三个密钥）。   默认值：0 |
  | seed_string | VARCHAR2 | 否     | 用于生成密钥的种子值，其长度至少为80字节。                                                                                                                                                                                                                                                                                                                                                                                          |
  | seed         | RAW     | 否       | 用于生成密钥的种子值，其长度至少为80字节。                                                                                                                                                                                                                                                                                                                                                                                          |
     
  示例：
  ```
  -- 生成随机密钥
  ora_db=# SELECT rawtohex(dbe_raw.cast_from_varchar2_to_raw(dbe_obfuscation_toolkit.des3getkey(seed_string => '00000000000000000000000000000000000000000000000000000000000000000000000000000000')));
  WARNING:  The DES3 algorithm is an insecure algorithm, please use it with caution.
  CONTEXT:  referenced column: rawtohex
               rawtohex             
  ----------------------------------
   9A64E470BC16D4969E782F3F5A8491A6
  (1 row)
  ora_db=# SELECT rawtohex(dbe_obfuscation_toolkit.des3getkey(which => 1, seed => dbe_raw.cast_from_varchar2_to_raw('00000000000000000000000000000000000000000000000000000000000000000000000000000000')));
  WARNING:  The DES3 algorithm is an insecure algorithm, please use it with caution.
  CONTEXT:  referenced column: rawtohex
                       rawtohex                     
  --------------------------------------------------
   7497F5D61F23E751D3D337EE6B88B257350FF8B9732BC288
  (1 row)
  ```
  ![](https://support.huaweicloud.com/distributed-ref-v10-gaussdb/public_sys-resources/note_3.0-zh-cn.png)
  DBE_OBFUSCATION_TOOLKIT.DES3GETKEY生成的密钥为随机密钥。
  
- DBE_OBFUSCATION_TOOLKIT.DES3ENCRYPT
  功能：使用DES3算法对数据进行加密。
  DBE_OBFUSCATION_TOOLKIT.DES3ENCRYPT原型可以分为2种：
  ```
  --原型1：入参为VARCHAR2类型
  DBE_OBFUSCATION_TOOLKIT.DES3ENCRYPT(
  input_string IN VARCHAR2,
  key_string   IN VARCHAR2,
  which        IN INTEGER DEFAIULT 0,
  iv_string    IN VARCHAR2 DEFAULT NULL
  ) RETURN VARCHAR2;
  --原型2：入参为RAW类型
  DBE_OBFUSCATION_TOOLKIT.DES3ENCRYPT(
  input IN RAW,
  key   IN RAW,
  which IN INTEGER DEFAIULT 0,
  iv    IN RAW DEFAULT NULL
  ) RETURN RAW;
  ```
  表6DBE_OBFUSCATION_TOOLKIT.DES3ENCRYPT接口参数说明 
  | 参数          | 类型       | 是否可以为空 | 描述                                                                                                                                                                                                                                                                                                                                                                                                                      |
  |:---|:---|:---|:---|
  | input_string | VARCHAR2 | 否       | 待加密的数据，其长度必须为8字节的倍数。                                                                                                                                                                                                                                                                                                                                                                                                     |
  | input        | RAW        | 否          | 待加密的数据，其长度必须为8字节的倍数。                                                                                                                                                                                                                                                                                                                                                                                                     |
  | key_string  | VARCHAR2  | 否        | 加密密钥，其长度依赖which的取值。 - 当which取值为0时：加密密钥的长度至少为16字节，超过16字节的数据不影响加密结果。  - 当which取值为1时：加密密钥的长度至少为24字节，超过24字节的数据不影响加密结果。                                                    |
  | key         | RAW        | 否         | 加密密钥，其长度依赖which的取值。 - 当which取值为0时：加密密钥的长度至少为16字节，超过16字节的数据不影响加密结果。  - 当which取值为1时：加密密钥的长度至少为24字节，超过24字节的数据不影响加密结果。                                                    |
  | which       | INTEGER  | 否       | 密钥模式。 取值范围：0或1 - 0：按照16字节的密钥模式（即两个密钥）进行加密。  - 1：按照24字节的密钥模式（即三个密钥）进行加密。   默认值：0 |
  | iv_string   | VARCHAR2  | 是       | 初始化向量，当该参数取值不为NULL时，其长度必须为8字节的倍数，超过8字节的数据不影响加密结果。 默认值：NULL                                                                                                                                                                                                                                                                                                                |
  | iv           | RAW        | 是         | 初始化向量，当该参数取值不为NULL时，其长度必须为8字节的倍数，超过8字节的数据不影响加密结果。 默认值：NULL                                                                                                                                                                                                                                                                                                                |
     
  示例：
  ```
  ora_db=# SELECT rawtohex(dbe_raw.cast_from_varchar2_to_raw(dbe_obfuscation_toolkit.des3encrypt(input_string => '12345678', key_string => '1234567823456789', iv_string => '12345678')));
  WARNING:  The DES3 algorithm is an insecure algorithm, please use it with caution.
  CONTEXT:  referenced column: rawtohex
       rawtohex     
  ------------------
   EFDD1DFBCEE3B0CA
  (1 row)
  ora_db=# SELECT rawtohex(dbe_obfuscation_toolkit.des3encrypt(input => dbe_raw.cast_from_varchar2_to_raw('12345678'), key => dbe_raw.cast_from_varchar2_to_raw('1234567812345678876543210'), which => 1));
  WARNING:  The DES3 algorithm is an insecure algorithm, please use it with caution.
  CONTEXT:  referenced column: rawtohex
       rawtohex     
  ------------------
   1A1546D3DEAEB8BE
  (1 row)
  ```
  ![](https://support.huaweicloud.com/distributed-ref-v10-gaussdb/public_sys-resources/note_3.0-zh-cn.png)
  由于该函数执行过程需传入密钥，出于安全考虑，gsql工具不会将包含该函数名称的SQL记录至执行历史。因此，无法在gsql中通过上下翻页功能查找该函数的执行历史。
  

- DBE_OBFUSCATION_TOOLKIT.DES3DECRYPT
  功能：使用DES3算法对数据进行解密。
  DBE_OBFUSCATION_TOOLKIT.DES3DECRYPT原型可以分为2种：
  ```
  --原型1：入参为VARCHAR2类型
  DBE_OBFUSCATION_TOOLKIT.DES3DECRYPT(
  input_string IN VARCHAR2,
  key_string   IN VARCHAR2,
  which        IN INTEGER DEFAIULT 0,
  iv_string    IN VARCHAR2 DEFAULT NULL
  ) RETURN VARCHAR2;
  --原型2：入参为RAW类型
  DBE_OBFUSCATION_TOOLKIT.DES3DECRYPT(
  input IN RAW,
  key   IN RAW,
  which IN INTEGER DEFAIULT 0,
  iv    IN RAW DEFAULT NULL
  ) RETURN RAW;
  ```
  表7DBE_OBFUSCATION_TOOLKIT.DES3DECRYPT接口参数说明 
  | 参数          | 类型           | 是否可以为空 | 描述                                                                                                                                                                                                                                                                                                                                                                                                                              |
  |:---|:---|:---|:---|
  | input_string | VARCHAR2 | 否       | 待解密的数据，其长度必须为8字节的倍数。                                                                                                                                                                                                                                                                                                                                                                                                           |
  | input        | RAW        | 否      | 待解密的数据，其长度必须为8字节的倍数。                                                                                                                                                                                                                                                                                                                                                                                                           |
  | key_string   | VARCHAR2    | 否      | 解密密钥，其长度依赖which的取值。 - 当which取值为0时：解密密钥的长度至少为16字节，超过16字节的数据不影响解密结果。  - 当which取值为1时：解密密钥的长度至少为24字节，超过24字节的数据不影响解密结果。                                                                  |
  | key          | RAW        | 否       | 解密密钥，其长度依赖which的取值。 - 当which取值为0时：解密密钥的长度至少为16字节，超过16字节的数据不影响解密结果。  - 当which取值为1时：解密密钥的长度至少为24字节，超过24字节的数据不影响解密结果。                                                                  |
  | which      | INTEGER     | 否      | 密钥模式。 取值范围：0或1 - 0：按照16字节的密钥模式（即两个密钥）进行解密。  - 1：按照24字节的密钥模式（即三个密钥）进行解密。   默认值：0 |
  | iv_string      | VARCHAR2    | 是      | 初始化向量，当该参数取值不为NULL时，其长度必须为8字节的倍数，超过8字节的数据不影响解密结果。 默认值：NULL                                                                                                                                                                                                                                                                                                                        |
  | iv          | RAW        | 是      | 初始化向量，当该参数取值不为NULL时，其长度必须为8字节的倍数，超过8字节的数据不影响解密结果。 默认值：NULL                                                                                                                                                                                                                                                                                                                        |
     
  示例：
  ```
  ora_db=# SELECT rawtohex(dbe_raw.cast_from_varchar2_to_raw(dbe_obfuscation_toolkit.des3decrypt(input_string => '12345678', key_string => '1234567823456789', iv_string => '12345678')));
  WARNING:  The DES3 algorithm is an insecure algorithm, please use it with caution.
  CONTEXT:  referenced column: rawtohex
       rawtohex     
  ------------------
   8C2ED115CCA35582
  (1 row)
  ora_db=# SELECT rawtohex(dbe_obfuscation_toolkit.des3decrypt(input => dbe_raw.cast_from_varchar2_to_raw('12345678'), key => dbe_raw.cast_from_varchar2_to_raw('1234567812345678876543210'), which => 1, iv => dbe_raw.cast_from_varchar2_to_raw('12345678')));
  WARNING:  The DES3 algorithm is an insecure algorithm, please use it with caution.
  CONTEXT:  referenced column: rawtohex
       rawtohex     
  ------------------
   A6FF1AA4A875B3E8
  (1 row)
  ```
  ![](https://support.huaweicloud.com/distributed-ref-v10-gaussdb/public_sys-resources/note_3.0-zh-cn.png)
  由于该函数执行过程需传入密钥，出于安全考虑，gsql工具不会将包含该函数名称的SQL记录至执行历史。因此，无法在gsql中通过上下翻页功能查找该函数的执行历史。
  

- DBE_OBFUSCATION_TOOLKIT.MD5
  功能：计算数据的MD5哈希值。
  DBE_OBFUSCATION_TOOLKIT.MD5原型可以分为2种：
  ```
  --原型1：入参为VARCHAR2类型
  DBE_OBFUSCATION_TOOLKIT.MD5(
  input_string IN VARCHAR2
  ) RETURN VARCHAR2;
  --原型2：入参为RAW类型
  DBE_OBFUSCATION_TOOLKIT.MD5(
  input IN RAW
  ) RETURN RAW;
  ```
  表8DBE_OBFUSCATION_TOOLKIT.MD5接口参数说明 
  | 参数         | 类型      | 是否可以为空 | 描述         |
  |:---|:---|:---|:---|
  | input_string | VARCHAR2 | 否    | 待进行哈希操作的数据。 |
  | input      | RAW      | 否    | 待进行哈希操作的数据。 |
     
  示例：
  ```
  ora_db=# SELECT rawtohex(dbe_raw.cast_from_varchar2_to_raw(dbe_obfuscation_toolkit.md5(input_string => '1234567890')));
  WARNING:  The MD5 algorithm is an insecure algorithm, please use it with caution.
  CONTEXT:  referenced column: rawtohex
               rawtohex             
  ----------------------------------
   E807F1FCF82D132F9BB018CA6738A19F
  (1 row)
  ora_db=# SELECT rawtohex(dbe_obfuscation_toolkit.md5(dbe_raw.cast_from_varchar2_to_raw('1234567890')));
  WARNING:  The MD5 algorithm is an insecure algorithm, please use it with caution.
  CONTEXT:  referenced column: rawtohex
               rawtohex             
  ----------------------------------
   E807F1FCF82D132F9BB018CA6738A19F
  (1 row)
  -- 删除ora_db数据库
  ora_db=# \c postgres
  gaussdb=# DROP DATABASE ora_db;
  ```
  
 
