Skip to main content
These functions implement encryption and decryption of data with AES (Advanced Encryption Standard) algorithm. The key length depends on the encryption mode: 16, 24, and 32 bytes long for -128-, -196-, and -256- modes respectively. The initialization vector length is always 16 bytes (bytes in excess of 16 are ignored).

HMAC

Introduced in: v25.12.0 Computes the HMAC (Hash-based Message Authentication Code) for the given message using the specified hash algorithm and secret key. Supported hash algorithms:
  • blake2b512
  • blake2s256
  • md4
  • md5
  • md5-sha1
  • mdc2
  • ripemd (aliases: RIPEMD160, ripemd)
  • ripemd160
  • rmd160 (aliases: RIPEMD160, rmd160)
  • sha1
  • sha224
  • sha256
  • sha3-224
  • sha3-256
  • sha3-384
  • sha3-512
  • sha384
  • sha512
  • sha512-224
  • sha512-256
  • shake128
  • shake256
  • sm3
  • ssl3-md5 (aliases: MD5, ssl3-md5)
  • ssl3-sha1 (aliases: SHA1, ssl3-sha1)
  • whirlpool
Syntax
Arguments
  • mode — Hash algorithm name (case-insensitive). Supported: md5, sha1, sha224, sha256, sha384, sha512. String
  • message — Message to be authenticated. String
  • key — Secret key for HMAC. String
Returned value Returns a binary string containing the HMAC digest. String Examples Basic HMAC-SHA256
Query
Response
Different hash algorithms
Query
Response
Case-insensitive mode
Query
Response

aes_decrypt_mysql

Introduced in: v20.12.0 Decrypts data encrypted by MySQL’s AES_ENCRYPT function. Produces the same plaintext as decrypt for the same inputs. When key or iv are longer than they should normally be, aes_decrypt_mysql will stick to what MySQL’s aes_decrypt does which is to ‘fold’ key and ignore the excess bits of IV. Supports the following decryption modes:
  • aes-128-ecb, aes-192-ecb, aes-256-ecb
  • aes-128-cbc, aes-192-cbc, aes-256-cbc
  • aes-128-cfb128
  • aes-128-ofb, aes-192-ofb, aes-256-ofb
Syntax
Arguments
  • mode — Decryption mode. String
  • ciphertext — Encrypted text that needs to be decrypted. String
  • key — Decryption key. String
  • iv — Optional. Initialization vector. String
Returned value Returns the decrypted String. String Examples Decrypt MySQL data
Query
Response

aes_encrypt_mysql

Introduced in: v20.12.0 Encrypts text the same way as MySQL’s AES_ENCRYPT function does. The resulting ciphertext can be decrypted with MySQL’s AES_DECRYPT function. Produces the same ciphertext as the encrypt function for the same inputs. When key or iv are longer than they should normally be, aes_encrypt_mysql will stick to what MySQL’s aes_encrypt does which is to ‘fold’ key and ignore the excess bits of iv. The supported encryption modes are:
  • aes-128-ecb, aes-192-ecb, aes-256-ecb
  • aes-128-cbc, aes-192-cbc, aes-256-cbc
  • aes-128-ofb, aes-192-ofb, aes-256-ofb
Syntax
Arguments
  • mode — Encryption mode. String
  • plaintext — Text that should be encrypted. String
  • key — Encryption key. If the key is longer than required by mode, MySQL-specific key folding is performed. String
  • iv — Optional. Initialization vector. Only the first 16 bytes are taken into account. String
Returned value Ciphertext binary string. String Examples Equal input comparison
Query
Response
Encrypt fails with long key
Query
Response
MySQL compatibility
Query
Response
Longer IV produces the same result
Query
Response

decrypt

Introduced in: v20.12.0 This function decrypts an AES-encrypted binary string using the following modes:
  • aes-128-ecb, aes-192-ecb, aes-256-ecb
  • aes-128-cbc, aes-192-cbc, aes-256-cbc
  • aes-128-ofb, aes-192-ofb, aes-256-ofb
  • aes-128-gcm, aes-192-gcm, aes-256-gcm
  • aes-128-ctr, aes-192-ctr, aes-256-ctr
  • aes-128-cfb, aes-128-cfb1, aes-128-cfb8
Syntax
Arguments
  • mode — Decryption mode. String
  • ciphertext — Encrypted text that should be decrypted. String
  • key — Decryption key. String
  • iv — Initialization vector. Required for -gcm modes, optional for others. String
  • aad — Additional authenticated data. Won’t decrypt if this value is incorrect. Works only in -gcm modes, for others throws an exception. String
Returned value Returns decrypted plaintext. String Examples Correctly decrypting encrypted data
Query
Response
Incorrectly decrypting encrypted data
Query
Response

encrypt

Introduced in: v20.12.0 Encrypts plaintext into ciphertext using AES in one of the following modes:
  • aes-128-ecb, aes-192-ecb, aes-256-ecb
  • aes-128-cbc, aes-192-cbc, aes-256-cbc
  • aes-128-ofb, aes-192-ofb, aes-256-ofb
  • aes-128-gcm, aes-192-gcm, aes-256-gcm
  • aes-128-ctr, aes-192-ctr, aes-256-ctr
  • aes-128-cfb, aes-128-cfb1, aes-128-cfb8
Syntax
Arguments
  • mode — Encryption mode. String
  • plaintext — Text that should be encrypted. String
  • key — Encryption key. String
  • iv — Initialization vector. Required for -gcm modes, optional for others. String
  • aad — Additional authenticated data. It isn’t encrypted, but it affects decryption. Works only in -gcm modes, for others it throws an exception. String
Returned value Returns binary string ciphertext. String Examples Example encryption
Query
Response
Example with GCM mode
Query
Response

tryDecrypt

Introduced in: v22.10.0 Similar to the decrypt function, but returns NULL if decryption fails when using the wrong key. Syntax
Arguments
  • mode — Decryption mode. String
  • ciphertext — Encrypted text that should be decrypted. String
  • key — Decryption key. String
  • iv — Optional. Initialization vector. Required for -gcm modes, optional for other modes. String
  • aad — Optional. Additional authenticated data. Won’t decrypt if this value is incorrect. Works only in -gcm modes, for other modes throws an exception. String
Returned value Returns the decrypted String, or NULL if decryption fails. Nullable(String) Examples Create table and insert data
Query
Response
Last modified on June 23, 2026