Notice
Community Edition does not support the sm4_encrypt system function.
Syntax
SM4_ENCRYPT(str,key_str[,init_vector])
Purpose
The SM4_ENCRYPT function encrypts the string str using the key string key_str and returns the encrypted binary string. The str parameter has no length limit, and the function automatically fills str to an integer multiple of the encryption block. The length of the ciphertext is calculated as follows:
16 * (trunc(string_length / 16) + 1)
If any function parameter is NULL, the function returns NULL.
SM4 is a block encryption algorithm released by the Chinese National Cryptography Administration. It is a symmetric key algorithm with a 128-bit key length and a 128-bit block size. The longer the key length, the higher the security, but the slower the encryption speed.
The SM4_ENCRYPT() function can control the block encryption mode by using an initialization vector:
The block_encryption_mode system variable specifies the mode of the block encryption algorithm. The default value is
aes-128-ecb, which indicates that encryption is performed using a 128-bit key length and ECB mode.The
init_vectorparameter specifies the initialization vector:When the encryption mode requires the
init_vectorparameter, its length must be at least 16 bytes (any bytes exceeding 16 will be ignored). If theinit_vectorparameter is missing, an error will occur.When the encryption mode does not require the
init_vectorparameter, it will be ignored.
Examples
- In
sm4-ecbmode, theSM4_ENCRYPTfunction accepts two parameters{str, key_str}. If three values are provided, the third value will be 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, orsm4-ofbmode, theSM4_ENCRYPTfunction accepts three parameters. For example:
SELECT hex(sm4_encrypt('asdasdasdasd', '12312313123', 'asdasdkljasdkljalskdjaklsdjaklsjdaklsdjlaksdj')) FROM dual;
+--------------------------------------------------------------------------------------------------+
| hex(sm4_encrypt('asdasdasdasd', '12312313123', 'asdasdkljasdkljalskdjaklsdjaklsjdaklsdjlaksdj')) |
+--------------------------------------------------------------------------------------------------+
| 646B83FDA4E969244E2B9BD835578D50 |
+-------------------------------------------------------------------------------------
