SYM_ENCRYPT

Updated at:

Encrypts data in specified columns of a table by using a random key and returns the ciphertext of the BINARY type.

Notes

After you upgrade to MaxCompute 2.0, some functions have been extended. To use functions that involve the new data types (TINYINT, SMALLINT, INT, FLOAT, VARCHAR, TIMESTAMP, or BINARY), run the following statement to enable the new data type system:

  • At the session level: To use the new data types, add set odps.sql.type.system.odps2=true; before your SQL statement and submit them together.

  • At the project level: A project owner can configure this setting as needed. It takes 10 to 15 minutes for the configuration to take effect. Use the following command:

    setproject odps.sql.type.system.odps2=true;

    For more information about setproject, see Project commands. For notes on enabling data types at the project level, see Data type version guide.

Syntax

binary sym_encrypt(string|binary <value_to_encrypt>,
                   binary <key>
                   [,string <encryption_method> ,
                      [ string <additional_authenticated_data> ]
                    ]
                  )

Parameters

  • value_to_encrypt: required. The data that you want to encrypt. You can encrypt only data of the STRING and BINARY types.

  • key: required. The key that you use to encrypt data. The key must be of the BINARY type and must be 256 bits in length.

  • encryption_method: optional. The encryption mode. Only the AES-GCM-256 encryption algorithm is available and is used as the default value.

  • additional_authenticated_data: optional. The additional authenticated data (AAD), which is used to verify the authenticity and integrity of the data. Only an authenticated encryption with associated data (AEAD) encryption algorithm, such as AES-GCM, is supported.

Return value

Ciphertext of the BINARY type is returned.

  • The return value contains the Initialization Vector (IV), ciphertext, and AEAD tag in sequence.

  • The return value differs each time even if you use the same key to encrypt the same plaintext.

Sample data

-- Create a table.
create table mf_user_info(id bigint,
                          name string,
                          gender string,
                          id_card_no string,
                          tel string);
-- Insert data into external table.
insert overwrite table mf_user_info values(1,"bob","male","0001","13900001234"),
                                       (2,"allen","male","0011","13900001111"),
                                     (3,"kate","female","0111","13900002222"),
                                     (4,"annie","female","1111","13900003333");
-- Query data from the table.
select * from mf_user_info;
+------------+------+--------+------------+------------+
| id         | name | gender | id_card_no |    tel     |
+------------+------+--------+------------+------------+
| 1          | bob  | male   | 0001       | 13900001234|
| 2          | allen| male   | 0011       | 13900001111|
| 3          | kate | female | 0111       | 13900002222|
| 4          | annie| female | 1111       | 13900003333|
+------------+------+--------+------------+------------+

Example 1: Encrypt data by using a plaintext key.

Encrypt data in a specified column of the mf_user_info table based on the Sample data. Sample statements:

  • Use the AEAD algorithm to encrypt data.

    -- Encrypt data in the id_card_no column.
    insert overwrite table mf_user_info
    select id,
        name,
        gender,
           base64(sym_encrypt(id_card_no,
                       cast('b75585cf321cdcad42451690cdb7bfc4' as binary)
                      ))as id_card_no,
          tel
    from mf_user_info;
    
    select * from mf_user_info;

    The following result is returned:

    +------------+------+--------+------------+-----+
    | id         | name | gender | id_card_no | tel |
    +------------+------+--------+------------+-----+
    | 1          | bob  | male   | frgJZAEAQMeEuHqpS8lK9VxQhgPYpZ317V+oUla/xEc= | 13900001234|
    | 2          | allen| male   | frgJZAIAQMeEuHqpLeXQfETsFSLJxBwHhPx6tpzWUg4= | 13900001111|
    | 3          | kate | female | frgJZAMAQMeEuHqpdphXAU6iWelWenlDnVy+R0HMvAY= | 13900002222|
    | 4          | annie| female | frgJZAQAQMeEuHqpR5c8bj21dYCeM0C25bLRZIrP71c= | 13900003333|
    +------------+------+--------+------------+-----+
  • Use the AEAD algorithm to encrypt data and attach AAD.

    -- Encrypt data in the id_card_no column.
    insert overwrite table mf_user_info
    select id,
                 name,
                 gender,
           base64(sym_encrypt(id_card_no,
                  cast('b75585cf321cdcad42451690cdb7bfc4' as binary),
                  'AES-GCM-256',
                  'test'
                  ))as id_card_no,
          tel
    from mf_user_info;
    select * from mf_user_info;

    The following result is returned:

    +------------+------+--------+------------+-----+
    | id         | name | gender | id_card_no | tel |
    +------------+------+--------+------------+-----+
    | 1          | bob  | male   | gJ0QaAEAoGGWVw90H/zETg... | 13900001234|
    | 2          | allen| male   | gJ0QaAIAoGGWVw90TpNFC0... | 13900001111|
    | 3          | kate | female | gJ0QaAMAoGGWVw90KaQ8Vm... | 13900002222|
    | 4          | annie| female | gJ0QaAQAoGGWVw90nYCAS1... | 13900003333|
    +------------+------+--------+------------+-----+

Example 2: Encrypt data by using a key table.

Construct a key table based on the Sample data to manage keys.

  • Principle

    • For data encryption, you can store keys in a MaxCompute table and perform the JOIN operation on the MaxCompute key table and the table whose column data you want to encrypt. This prevents keys from being leaked when the keys are passed.

    • For data decryption, the project administrator does not grant the permission on the MaxCompute key table to users who want to use the key table. Instead, the project administrator creates a secure view and authorizes the users to access the key table and call the decryption function to decrypt data based on the view. Users can decrypt data only after they obtain the access permissions on the view. The plaintext of the key is not included in the view. This way, the key is not leaked.

  • Precautions

    • Customers are responsible for the creation and management of keys. MaxCompute does not store keys or mappings between keys and ciphertext. If a key is lost, the data cannot be decrypted.

    • To ensure data security, special processing is required when you pass parameters that involve plaintext keys.

    • If the data that you want to encrypt is of the BINARY type, you must run the set odps.sql.type.system.odps2=true; command to enable the MaxCompute V2.0 data type edition.

Sample statements:

-- Create a key table.
create table mf_id_key(id bigint,key binary);
-- Insert a key into the key table.
insert overwrite table mf_id_key
  values (1,cast('b75585cf321cdcad42451690cdb7bfc4' as binary));
-- Query the key.
select * from mf_id_key;
+------------+------+
| id         | key  |
+------------+------+
| 1          | b75585cf321cdcad42451690cdb7bfc4 |
+------------+------+

-- Query data from the mf_user_info table.
select * from mf_user_info;
+------------+------+--------+------------+------------+
| id         | name | gender | id_card_no |    tel     |
+------------+------+--------+------------+------------+
| 1          | bob  | male   | 0001       | 13900001234|
| 2          | allen| male   | 0011       | 13900001111|
| 3          | kate | female | 0111       | 13900002222|
| 4          | annie| female | 1111       | 13900003333|
+------------+------+--------+------------+------------+

-- Encrypt data in the specified column of the mf_user_info table.
insert overwrite table mf_user_info
select /*+mapjoin(b)*/
      a.id,
      a.name,
      a.gender,
      base64(
        (sym_encrypt(a.id_card_no, b.key))
      ) as id_card_no,
      a.tel
 from mf_user_info as a join mf_id_key as b on a.id>=b.id;

-- Query the encrypted data.
select * from mf_user_info;

The following result is returned:

+------------+------+--------+------------+-----+
| id         | name | gender | id_card_no | tel |
+------------+------+--------+------------+-----+
| 1          | bob  | male   | 9esKZAEAoBquXVJo3ZptvoI09XuM4bSFTqF1mXH1BO4= | 13900001234|
| 2          | allen| male   | 9esKZAIAoBquXVJoJYqnXieAANih7FR59luePvdHB9U= | 13900001111|
| 3          | kate | female | 9esKZAMAoBquXVJoppwxgVwPYBnvjIMklWLmJ/sU0Y8= | 13900002222|
| 4          | annie| female | 9esKZAQAoBquXVJoB85RUFCLMbdyEBSz7LdS4M3Guvk= | 13900003333|
+------------+------+--------+------------+-----+

Related functions

SYM_ENCRYPT is a decryption function. For more information about encryption and decryption functions, see Encryption functions.