Declaration
SM4_ENCRYPT(str,key_str[,init_vector])
Description
Encrypts the string str using the key string key_str and returns the encrypted binary string. There is no length limit for str; this function automatically pads str to an integer multiple of the encryption block size. The ciphertext length is calculated as follows:
16 * (trunc(string_length / 16) + 1)
If any function parameter is NULL, the function returns NULL.
Data encryption is implemented using SM4. SM4 is a block cipher algorithm published by the National Cryptography Administration of China. The algorithm is public, with both the block length and key length being 128 bits. A longer key length provides higher security but reduces encryption speed.
The SM4_ENCRYPT() function allows you to control the block encryption mode using an initialization vector:
The block_encryption_mode specifies the block-based encryption algorithm mode. The default value is
aes-128-ecb, which indicates that the encryption will be performed using a 128-bit key length and the ECB mode.The
init_vectorparameter indicates the initialization vector:When the encryption mode requires setting the
init_vectorparameter, its length must be 16 bytes or more (bytes exceeding 16 will be ignored). An error occurs ifinit_vectoris missing.If the encryption mode does not require setting the
init_vectorparameter, this parameter will be ignored.
Examples
- In
sm4-ecbmode, theSM4_ENCRYPTfunction accepts two parameters:{str, key_str}. If three values are provided, the third value is ignored. For 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_ENCRYPTaccepts only three parameters. For example:
SELECT hex(sm4_encrypt('asdasdasdasd', '12312313123', 'asdasdkljasdkljalskdjaklsdjaklsjdaklsdjlaksdj')) FROM dual;
+--------------------------------------------------------------------------------------------------+
| hex(sm4_encrypt('asdasdasdasd', '12312313123', 'asdasdkljasdkljalskdjaklsdjaklsjdaklsdjlaksdj')) |
+--------------------------------------------------------------------------------------------------+
| 646B83FDA4E969244E2B9BD835578D50 |
+-------------------------------------------------------------------------------------