Skip to main content

Categories: Cryptography

DECRYPT

Decrypts data encrypted with Advanced Encryption Standard (AES) using various modes, including AES-ECB, AES-CBC, and AES-GCM. Returns a VARBINARY value (use CONVERT_FROM to read the decrypted text), or NULL if any argument is NULL.

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 to PKCS7 padding), AES-ECB-PKCS7, AES-ECB-NONE, AES-CBC (defaults to PKCS7 padding), AES-CBC-PKCS7, AES-CBC-NONE, AES-GCM. For AES-GCM, verifies the authentication tag.

Examples

DECRYPT example
SELECT DECRYPT(encrypted_data, key, 'AES-ECB')
FROM
(
SELECT
UNBASE64('xQAXVi52g+wT7xoVN08ssQ==') AS encrypted_data,
BINARY_STRING('1234567890123456') AS key
)

-- /* Binary */ Hello World
Decrypt with NULL arguments
SELECT
DECRYPT(NULL, FROM_HEX('01020304050607080910111213141516'), 'AES-ECB'),
DECRYPT(UNBASE64('x6iS1a/aW6YD769IbQaIKQ=='), NULL, 'AES-ECB'),
DECRYPT(UNBASE64('x6iS1a/aW6YD769IbQaIKQ=='), FROM_HEX('01020304050607080910111213141516'), NULL)

-- null, null, null

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 to PKCS7 padding), AES-ECB-PKCS7, AES-ECB-NONE, AES-CBC (defaults to PKCS7 padding), AES-CBC-PKCS7, AES-CBC-NONE, AES-GCM. For AES-GCM, verifies the authentication tag.
  • iv: The initialization vector (IV) or nonce used during encryption. Required for AES-CBC and AES-GCM modes. Must be 16 bytes for AES-CBC mode, 12 bytes recommended for AES-GCM mode.

Examples

DECRYPT example
SELECT DECRYPT(encrypted_data, key, 'AES-CBC', iv)
FROM
(
SELECT
UNBASE64('ZyODokM33Io1ZKIA8h7owA==') AS encrypted_data,
BINARY_STRING('1234567890123456') AS key,
BINARY_STRING('1234567890123456') 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-CBC and AES-GCM modes. Must be 16 bytes for AES-CBC mode, 12 bytes recommended for AES-GCM mode.
  • aad: The additional authenticated data (AAD) used during AES-GCM encryption. Must match the aad used during encryption for authentication to succeed. DECRYPT will verify ciphertext integrity by using the authentication tag concatenated during encryption.

Examples

DECRYPT example
SELECT DECRYPT(encrypted_data, key, 'AES-GCM', iv, aad)
FROM
(
SELECT
UNBASE64('+RQCxLexA2fVnX9N/SMT820KqW52WM9fVx9j') AS encrypted_data,
BINARY_STRING('1234567890123456') AS key,
BINARY_STRING('123456789012') AS iv,
BINARY_STRING('metadata') AS aad
)

-- /* Binary */ Hello World

Usage Notes​

  • Ciphertext created with AES_ENCRYPT can be decrypted with DECRYPT in AES-ECB mode and does not need to be recreated with ENCRYPT unless 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.