# Basic Use of the Fully-encrypted Database

## 1\. Introduction to the Fully-encrypted Database Features

A fully-encrypted database aims to protect privacy throughout the data lifecycle. Data is always encrypted during transmission, computing, and storage regardless of the service scenario or environment. After the data owner encrypts data on the client and sends the encrypted data to the server, even if an attacker manages to exploit some system vulnerability and steal user data, they cannot obtain valuable information. Data privacy is protected.

## 2\. Customer Benefits of the Fully-encrypted Database

The entire service data flow is encrypted during processing. A fully-encrypted database:

1. Protects data privacy and security throughout the lifecycle on the cloud. Attackers cannot obtain information from the database server regardless of the data status.
    
2. Helps cloud service providers earn the trust of third-party users. Users, including service administrators and O&M administrators in enterprise service scenarios and application developers in consumer cloud services, can keep the encryption keys themselves so that even users with high permissions cannot access unencrypted data.
    
3. Enables cloud databases to better comply with personal privacy protection laws and regulations.
    

## 3\. Use of the Fully-encrypted Database

Currently, the fully-encrypted database supports two connection modes: gsql and JDBC. This chapter describes how to use the database in the two connection modes.

### 3.1 Connecting to a Fully-encrypted Database

1. Run the **gsql -p PORT –d postgres -r –C** command to enable the encryption function.
    

Parameter description:

**\-p** indicates the port number. **\-d** indicates the database name. **\-C** indicates that the encryption function is enabled.

1. To support JDBC operations on a fully-encrypted database, set **enable\_ce** to **1**.
    

### 3.2 Creating a User Key

A fully-encrypted database has two types of keys: client master key (CMK) and data encryption key (CEK).

The CMK is used to encrypt the CEK. The CEK is used to encrypt user data.

Before creating a key, use gs\_ktool to create a key ID for creating a CMK.

openGauss=# **\\! gs\_ktool -g**

The sequence and dependency of creating a key are as follows: creating a key ID &gt; creating a CMK &gt; creating a CEK.

* **1\. Creating a CMK and a CEK in the GSQL Environment**
    
* \[Creating a CMK\]
    
    CREATE CLIENT MASTER KEY client\_master\_key\_name WITH (KEY\_STORE = key\_store\_name, KEY\_PATH = "key\_path\_value", ALGORITHM = algorithm\_type);
    
    Parameter description:
    
    * client\_master\_key\_name
        
    
    This parameter is used as the name of a key object. In the same namespace, the value of this parameter must be unique.
    
    Value range: a string. It must comply with the naming convention.
    
    * KEY\_STORE
        
    
    Tool or service that independently manages keys. Currently, only the key management tool gs\_ktool provided by GaussDB Kenel and the online key management service huawei\_kms provided by Huawei Cloud are supported. Value range: **gs\_ktool** and **huawei\_kms**
    
    * KEY\_PATH
        
    
    A key in the key management tool or service. The **KEY\_STORE** and **KEY\_PATH** parameters can be used to uniquely identify a key entity. When **KEY\_STORE** is set to **gs\_ktool**, the value is **gs\_ktool** or **KEY\_ID**. When **KEY\_STORE** is set to **huawei\_kms**, the value is a 36-byte key ID.
    
    * ALGORITHM
        
    
    This parameter specifies the encryption algorithm used by the key entity. When **KEY\_STORE** is set to **gs\_ktool**, the value can be **AES\_256\_CBC** or **SM4**. When **KEY\_STORE** is set to **huawei\_kms**, the value is **AES\_256**.
    
* \[Creating a CEK\]
    
    CREATE COLUMN ENCRYPTION KEY column\_encryption\_key\_name WITH(CLIENT\_MASTER\_KEY = client\_master\_key\_name, ALGORITHM = algorithm\_type, ENCRYPTED\_VALUE = encrypted\_value);
    
    Parameter description:
    
    * column\_encryption\_key\_name
        
    
    This parameter is used as the name of a key object. In the same namespace, the value of this parameter must be unique.
    
    Value range: String, which must comply with the naming convention.
    
    * CLIENT\_MASTER\_KEY
        
    
    Specifies the CMK used to encrypt the CEK. The value is the CMK object name, which is created using the **CREATE CLIENT MASTER KEY** syntax.
    
    * ALGORITHM
        
    
    Encryption algorithm to be used by the CEK. The value can be **AEAD\_AES\_256\_CBC\_HMAC\_SHA256**, **AEAD\_AES\_128\_CBC\_HMAC\_SHA256**, or **SM4\_SM3**.
    
    * **ENCRYPTED\_VALUE (optional)**
        
    
    A key password specified by a user. The key password length ranges from 28 to 256 bits. The derived 28-bit key meets the AES128 security requirements. If the user needs to use AES256, the key password length must be 39 bits. If the user does not specify the key password length, a 256-bit key is automatically generated.
    
    \[Example in the GSQL environment\]
    
    <table><tbody><tr><td colspan="1" rowspan="1"><p><strong>1</strong></p></td><td colspan="1" rowspan="1"><p>-- (1) Use the key management tool <strong>gs_ktool</strong> to create a key. The tool returns the ID of the newly generated key.</p></td></tr></tbody></table>
    
* **2\. Creating a CMK and a CEK in the JDBC Environment**
    
    <table><tbody><tr><td colspan="1" rowspan="1"><p><strong>1</strong></p></td><td colspan="1" rowspan="1"><p>// Create a CMK.</p></td></tr></tbody></table>
    

### 3.3 Creating an Encrypted Table

After creating the CMK and CEK, you can use the CEK to create an encrypted table.

An encrypted table can be created in two modes: randomized encryption and deterministic encryption.

* **Creating an Encrypted Table in the GSQL Environment**
    

\[Example\]

<table><tbody><tr><td colspan="1" rowspan="1"><p><strong>1</strong></p></td><td colspan="1" rowspan="1"><p>openGauss<strong>=</strong># <strong>CREATE</strong> <strong>TABLE</strong> creditcard_info <strong>(</strong>id_number <strong>int,</strong></p></td></tr></tbody></table>

Parameter description:

**ENCRYPTION\_TYPE** indicates the encryption type in the ENCRYPTED WITH constraint. The value of **encryption\_type\_value** can be **DETERMINISTIC** or **RANDOMIZED**.

---

* **Creating an Encrypted Table in the JDBC Environment**
    

<table><tbody><tr><td colspan="1" rowspan="1"><p><strong>1</strong></p></td><td colspan="1" rowspan="1"><p>int rc3 <strong>=</strong> stmt<strong>.</strong>executeUpdate<strong>(</strong>"CREATE TABLE creditcard_info (id_number int, name varchar(50) encrypted with (column_encryption_key = ImgCEK1, encryption_type = DETERMINISTIC),credit_card varchar(19) encrypted with (column_encryption_key = ImgCEK1, encryption_type = DETERMINISTIC));"<strong>);</strong></p></td></tr></tbody></table>

### 3.4 Inserting Data into the Encrypted Table and Querying the Data

After an encrypted table is created, you can insert and view data in the encrypted table in encrypted database mode (enabling the connection parameter **\-C**). When the common environment (disabling the connection parameter **\-C**) is used, operations cannot be performed on the encrypted table, and only ciphertext data can be viewed in the encrypted table.

* **Inserting Data into the Encrypted Table and Viewing the Data in the GSQL Environment**
    
    <table><tbody><tr><td colspan="1" rowspan="1"><p><strong>1</strong></p></td><td colspan="1" rowspan="1"><p>openGauss<strong>=</strong># <strong>INSERT</strong> <strong>INTO</strong> creditcard_info <strong>VALUES</strong> <strong>(</strong>1<strong>,</strong>'joe'<strong>,</strong>'6217986500001288393'<strong>);</strong></p></td></tr></tbody></table>
    
    Note: The data in the encrypted table is displayed in ciphertext when you use a non-encrypted client to view the data.
    
    <table><tbody><tr><td colspan="1" rowspan="1"><p><strong>1</strong></p></td><td colspan="1" rowspan="1"><p>openGauss<strong>=</strong># <strong>select</strong> id_number<strong>,</strong>name <strong>from</strong> creditcard_info<strong>;</strong></p></td></tr></tbody></table>
    
* **Inserting Data into the Encrypted Table and Viewing the Data in the JDBC Environment**
    
    <table><tbody><tr><td colspan="1" rowspan="1"><p><strong>1</strong></p></td><td colspan="1" rowspan="1"><p>// Insert data.</p></td></tr></tbody></table>
    
    The preceding describes how to use the fully-encrypted database features. For details, see the corresponding sections in the official document. However, for a common user, the functions described above are sufficient to ensure smooth implementation of daily work. In the future, fully-encrypted databases will evolve to be easier to use and provide higher performance. Stay tuned!
