Notice
OceanBase Database Community Edition does not support the SM4_ENCRYPT system function.
Syntax
SM4_ENCRYPT(str,key_str[,init_vector])
Purpose
SM4_ENCRYPT() uses the key specified by key_str to encrypt the string specified by str and returns the encrypted binary string. It does not limit the length of str, and automatically pads str to a length that is an integral multiple of the encrypted blocks. The length of the ciphertext is calculated by using the following formula:
16 × (trunc(string_length/16) + 1)
If any argument is NULL, the function returns NULL.
The SM4 algorithm is used for data encryption. SM4 is a public block cipher algorithm released by the State Cryptography Administration Office of Security Commercial Code Administration (OSCCA). Both the block and the key are 128 bits long. The longer the key, the higher the security, but the lower the encryption speed.
The SM4_ENCRYPT() function specifies the block cipher mode by using an initialization vector:
The system variable block_encryption_mode specifies the mode for block-based encryption. The default value
aes-128-ecbindicates that a 128-bit key and the electronic codebook (ECB) mode are used for encryption.init_vectorindicates the initialization vector.When the
init_vectorargument is required in an encryption mode, it must be 16 bytes or greater in length. Bytes in excess of 16 are ignored. Ifinit_vectoris missing, an error occurs.When the
init_vectorargument is not required in an encryption mode, it is ignored.
Examples
In
sm4-ecbmode,SM4_ENCRYPT()accepts two arguments{str, key_str}. If three values are passed in, the third value is ignored. Here is an example:SET block_encryption_mode = 'sm4-ecb'; SELECT hex(sm4_encrypt('asdasdasdasd', '12312313123')) FROM dual; +--------------------------------------------------------------------------------------------------+ | hex(sm4_encrypt('asdasdasdasd', '12312313123')) | +--------------------------------------------------------------------------------------------------+ | E0640595B963E365A70CB24DC8A3E349 | +--------------------------------------------------------------------------------------------------+ SELECT hex(sm4_encrypt('asdasdasdasd', '12312313123', 'asdasdkljasdkljalskdjaklsdjaklsjdaklsdjlaksdj')) FROM dual; +--------------------------------------------------------------------------------------------------+ | hex(sm4_encrypt('asdasdasdasd', '12312313123', 'asdasdkljasdkljalskdjaklsdjaklsjdaklsdjlaksdj')) | +--------------------------------------------------------------------------------------------------+ | E0640595B963E365A70CB24DC8A3E349 | +--------------------------------------------------------------------------------------------------+In
sm4-cbc,sm4-cfb, andsm4-ofbmodes,SM4_ENCRYPT()accepts only three arguments. Here is an example:SELECT hex(sm4_encrypt('asdasdasdasd', '12312313123', 'asdasdkljasdkljalskdjaklsdjaklsjdaklsdjlaksdj')) FROM dual; +--------------------------------------------------------------------------------------------------+ | hex(sm4_encrypt('asdasdasdasd', '12312313123', 'asdasdkljasdkljalskdjaklsdjaklsjdaklsdjlaksdj')) | +--------------------------------------------------------------------------------------------------+ | 646B83FDA4E969244E2B9BD835578D50 | +-------------------------------------------------------------------------------------