AES_ENCRYPT
Encryption of data using the OpenSSL. This function is consistent with the AES_ENCRYPT function in MySQL. Using AES_128_ECB algorithm by default, and the padding mode is PKCS7. Reference: https://dev.mysql.com/doc/refman/8.0/en/encryption-functions.html#function_aes-decrypt
AES_ENCRYPT(str, key_str[, init_vector])
str: Content to be encryptedkey_str: Secret keyinit_vector: Initialization Vector. The default value for the block_encryption_mode system variable is aes ecb mode, which does not require an initialization vector. The alternative permitted block encryption modes CBC, CFB1, CFB8, CFB128, and OFB all require an initialization vector.VARCHAR(*)
The AES_ENCRYPT function is not used the user secret key directly, but will be further processed. The specific steps are as follows:
128 / 8 = 16(if using AES_256_ECB, then SECRET KEY length are 128 / 8 = 32);i bit and the 16*k+i bit of the SECRET KEY entered by the user. If the length of the SECRET KEY less than 16 bytes, 0 will be padded;select to_base64(aes_encrypt('text','F3229A0B371ED2D9441B830D21A390C3'));
The results are consistent with those executed in MySQL.
+--------------------------------+ | to_base64(aes_encrypt('text')) | +--------------------------------+ | wr2JEDVXzL9+2XtRhgIloA== | +--------------------------------+ 1 row in set (0.01 sec)
If you want to change other encryption algorithms, you can:
set block_encryption_mode="AES_256_CBC"; select to_base64(aes_encrypt('text','F3229A0B371ED2D9441B830D21A390C3', '0123456789'));
Here is the result:
+-----------------------------------------------------+ | to_base64(aes_encrypt('text', '***', '0123456789')) | +-----------------------------------------------------+ | tsmK1HzbpnEdR2//WhO+MA== | +-----------------------------------------------------+ 1 row in set (0.01 sec)
For more information about block_encryption_mode, see also variables.
AES_ENCRYPT
AES_DECRYPT
Decryption of data using the OpenSSL. This function is consistent with the AES_DECRYPT function in MySQL. Using AES_128_ECB algorithm by default, and the padding mode is PKCS7.
AES_DECRYPT(str,key_str[,init_vector])
str: Content that encryptedkey_str: Secret keyinit_vector: Initialization VectorVARCHAR(*)
select aes_decrypt(from_base64('wr2JEDVXzL9+2XtRhgIloA=='),'F3229A0B371ED2D9441B830D21A390C3');
The results are consistent with those executed in MySQL.
+------------------------------------------------------+ | aes_decrypt(from_base64('wr2JEDVXzL9+2XtRhgIloA==')) | +------------------------------------------------------+ | text | +------------------------------------------------------+ 1 row in set (0.01 sec)
If you want to change other encryption algorithms, you can:
set block_encryption_mode="AES_256_CBC"; select aes_decrypt(from_base64('tsmK1HzbpnEdR2//WhO+MA=='),'F3229A0B371ED2D9441B830D21A390C3', '0123456789');
Here is the result:
+---------------------------------------------------------------------------+ | aes_decrypt(from_base64('tsmK1HzbpnEdR2//WhO+MA=='), '***', '0123456789') | +---------------------------------------------------------------------------+ | text | +---------------------------------------------------------------------------+ 1 row in set (0.01 sec)
For more information about block_encryption_mode, see also variables.
AES_DECRYPT