SYM_DECRYPT

Updated at:

Decrypts data that is encrypted by using a random key in specified columns of a table and returns the decrypted plaintext 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_decrypt(binary <value_to_decrypt>,
                   binary <key>
                   [,string <encryption_method> ,
                    [ string <additional_authenticated_data> ]
                   ]
                  )

Parameters

  • value_to_decrypt: required. The data that you want to decrypt. Only data of the BINARY type can be decrypted.

  • key: required. The key that is used to decrypt data. The key must be of the BINARY type and must be 256 bits in length.

  • encryption_method: optional. The mode that is used to decrypt data, which must be the same as the encryption mode.

  • additional_authenticated_data: optional. The additional authenticated data (AAD), which is used to verify the authenticity and integrity of the data. If AAD is used during data encryption, the same AAD is required for decryption.

Return value

Plaintext of the BINARY type is returned. You can use the CAST function to convert the BINARY type to the STRING type based on your business requirements.

Examples

The following examples illustrate how to decrypt data that is encrypted by using SYM_ENCRYPT.

  • Example 1: Decrypt data that is encrypted by using a plaintext key.

    Decrypt data in the specified column of the mf_user_info table. The data is encrypted based on Example 1: Encrypt data by using a plaintext key. Sample statements:

    • Use the authenticated encryption with associated data (AEAD) encryption algorithm to decrypt data.

      -- Decrypt data in the id_card_no column.
      insert overwrite table mf_user_info
      select id,
          name,
          gender,
             cast(sym_decrypt(unbase64(id_card_no),
                         cast('b75585cf321cdcad42451690cdb7bfc4' as binary)
                        ) as string) as id_card_no,
             tel
      from mf_user_info;
      
      -- Query the decrypted plaintext data.
      select * from mf_user_info;

      The following result is returned:

      +------------+------+--------+------------+-------------+
      | 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 |
      +------------+------+--------+------------+-------------+
    • Use the AEAD algorithm with AAD to decrypt data.

      -- Decrypt data in the id_card_no column.
      insert overwrite table mf_user_info
      select id,
                   name,
                   gender,
                    cast(sym_decrypt(unbase64(id_card_no),
                      cast('b75585cf321cdcad42451690cdb7bfc4' as binary),
                         'AES-GCM-256',
                             'test'
                        )as string)as id_card_no,
             tel
      from mf_user_info;
      
      -- Query the decrypted plaintext data.
      select * from mf_user_info;

      The following result is returned:

      +------------+------+--------+------------+------------+
      | 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 2: Decrypt data that is encrypted by using a key table.

    Decrypt data in the specified column of the mf_user_info table. The data is encrypted based on Example 2: Encrypt data by using a key table. Sample statement:

    -- Decrypt 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,
          cast(sym_decrypt(unbase64(a.id_card_no), b.key) as string) 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 decrypted data.
    select * from mf_user_info;

    The following result is returned:

    +------------+------+--------+------------+-------------+
    | 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 |
    +------------+------+--------+------------+-------------+

Related functions

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