Categories: Cryptography
DECRYPT
Decrypts data using AES decryption with various modes, including AES-ECB, AES-CBC, and AES-GCM. Supports PKCS7 padding or no padding options. For AES-GCM mode, verifies the authentication tag.
Syntax
DECRYPT(data VARBINARY, key VARBINARY, mode VARCHAR) → VARBINARY
- data: The encrypted data to decrypt.
- key: The decryption key. Must be 16, 24, or 32 bytes.
- mode: The decryption mode. Supported modes:
AES-ECB(defaults toPKCS7padding),AES-ECB-PKCS7,AES-ECB-NONE,AES-CBC(defaults toPKCS7padding),AES-CBC-PKCS7,AES-CBC-NONE,AES-GCM.
Examples
DECRYPT exampleSELECT DECRYPT(encrypted_data, key, 'AES-ECB')
FROM
(
SELECT
FROM_BASE64('xQAXVi52g+wT7xoVN08ssQ==') AS encrypted_data,
CONVERT_TO('1234567890123456', 'UTF8') AS key
)
-- /* Binary */ Hello World
DECRYPT(data VARBINARY, key VARBINARY, mode VARCHAR, iv VARBINARY) → VARBINARY
- data: The encrypted data to decrypt.
- key: The decryption key. Must be 16, 24, or 32 bytes.
- mode: The decryption mode. Supported modes:
AES-ECB(defaults toPKCS7padding),AES-ECB-PKCS7,AES-ECB-NONE,AES-CBC(defaults toPKCS7padding),AES-CBC-PKCS7,AES-CBC-NONE,AES-GCM. - iv: The initialization vector (IV) or nonce used during encryption. Required for
AES-CBCandAES-GCMmodes. Must be 16 bytes forAES-CBCmode, 12 bytes recommended forAES-GCMmode.
Examples
DECRYPT exampleSELECT DECRYPT(encrypted_data, key, 'AES-CBC', iv)
FROM
(
SELECT
FROM_BASE64('ZyODokM33Io1ZKIA8h7owA==') AS encrypted_data,
CONVERT_TO('1234567890123456', 'UTF8') AS key,
CONVERT_TO('1234567890123456', 'UTF8') AS iv
)
-- /* Binary */ Hello World
DECRYPT(data VARBINARY, key VARBINARY, mode VARCHAR, iv VARBINARY, aad VARBINARY) → VARBINARY
- data: The encrypted data to decrypt.
- key: The decryption key. Must be 16, 24, or 32 bytes.
- mode: Must be
AES-GCM - iv: The initialization vector (IV) or nonce used during encryption. Required for
AES-CBCandAES-GCMmodes. Must be 16 bytes forAES-CBCmode, 12 bytes recommended forAES-GCMmode. - aad: The additional authenticated data (AAD) used during
AES-GCMencryption. Must match theaadused during encryption for authentication to succeed.DECRYPTwill verify ciphertext integrity by using the authentication tag concatenated during encryption.
Examples
DECRYPT exampleSELECT DECRYPT(encrypted_data, key, 'AES-GCM', iv, aad)
FROM
(
SELECT
FROM_BASE64('+RQCxLexA2fVnX9N/SMT820KqW52WM9fVx9j') AS encrypted_data,
CONVERT_TO('1234567890123456', 'UTF8') AS key,
CONVERT_TO('123456789012', 'UTF8') AS iv,
CONVERT_TO('metadata', 'UTF8') AS aad
)
-- /* Binary */ Hello World
Usage Notes
- Ciphertext created with
AES_ENCRYPTcan be decrypted withDECRYPTinAES-ECBmode and does not need to be recreated withENCRYPTunless a stronger cipher mode is required. - Retrieve keys at runtime rather than hardcoding them in queries.
- Rotate keys periodically according to your organization's security policies.
- Use different keys for different data classifications or tenants.
- Ensure key length matches your security requirements: 32 bytes for AES-256 (recommended), 24 bytes for AES-192, or 16 bytes for AES-128.