crypto.decrypt_base64

STRINGcrypto.decrypt_base64IDcipherIDmodeIDpaddingSTRINGkey_hexSTRINGiv_hexSTRINGplaintext_base64

Available inall subroutines.

Symmetric key decryption.

The key and IV must be hex-encoded strings. The ciphertext must be Base64-encoded. The returned plaintext is also Base64-encoded.

This function uses standard Base64 encoding as defined in RFC 4648 Section 4, with the alphabet A-Za-z0-9+/ and = padding. The URL-safe variant (-_ instead of +/) is not supported. On input, padding is optional. On output, padding is always included.

Parameters

ParameterTypeDescription
cipherCIPHEREncryption algorithm: aes128, aes192, or aes256
modeMODEBlock cipher mode: cbc, ctr, gcm, or ccm
paddingPADDINGPadding scheme: pkcs7 or nopad
key_hexSTRINGHex-encoded decryption key
iv_hexSTRINGHex-encoded initialization vector or nonce
ciphertext_base64STRINGBase64-encoded data to decrypt

Supported combinations

CipherModePaddingKey lengthIV lengthTag lengthDescription
aes128cbcpkcs7128 bits128 bitsAES-128-CBC
aes192cbcpkcs7192 bits128 bitsAES-192-CBC
aes256cbcpkcs7256 bits128 bitsAES-256-CBC
aes128cbcnopad128 bits128 bitsAES-128-CBC raw
aes192cbcnopad192 bits128 bitsAES-192-CBC raw
aes256cbcnopad256 bits128 bitsAES-256-CBC raw
aes128ctrnopad128 bits128 bitsAES-128-CTR
aes192ctrnopad192 bits128 bitsAES-192-CTR
aes256ctrnopad256 bits128 bitsAES-256-CTR
aes128gcmnopad128 bits96 bits128 bitsAES-128-GCM
aes192gcmnopad192 bits96 bits128 bitsAES-192-GCM
aes256gcmnopad256 bits96 bits128 bitsAES-256-GCM
aes128ccmnopad128 bits56 bits96 bitsAES-128-CCM
aes192ccmnopad192 bits56 bits96 bitsAES-192-CCM
aes256ccmnopad256 bits56 bits96 bitsAES-256-CCM

Basic example

declare local var.key_hex STRING;
declare local var.iv_hex STRING;
declare local var.ciphertext_base64 STRING;
declare local var.plaintext_base64 STRING;
set var.key_hex = "603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4";
set var.iv_hex = "000102030405060708090a0b";
set var.ciphertext_base64 = "YATdP+tk7BHUzCaedOtOrgqm9KEpEx10uT25";
set var.plaintext_base64 = crypto.decrypt_base64(aes256, gcm, nopad, var.key_hex, var.iv_hex, var.ciphertext_base64);
if (fastly.error == "EBADDECRYPT") {
error 403 "Decryption failed";
}

For a complete example with secure nonce generation and key derivation, refer to crypto.encrypt_base64.

Authentication tags

For authenticated modes (GCM and CCM), the ciphertext input must include the authentication tag appended to the encrypted data (128 bits for GCM, 96 bits for CCM). This tag is automatically appended by crypto.encrypt_base64 and verified during decryption.

If the authentication tag is invalid or the ciphertext has been tampered with, decryption will fail and fastly.error will be set to EBADDECRYPT.

Decryption modes

AES-GCM and AES-CCM

These authenticated modes verify the integrity of the ciphertext before returning the plaintext. If verification fails, no plaintext is returned and fastly.error is set to EBADDECRYPT. This protects against tampering and ensures the data was encrypted with the correct key.

AES-CTR and AES-CBC

These unauthenticated modes will always produce output if the parameters are valid, even if the key is wrong or the ciphertext has been modified. There is no way to detect tampering or incorrect keys from the decryption result alone. Applications using these modes should implement their own integrity checks.

With AES-CBC and nopad padding, the ciphertext length (after Base64 decoding) must be a multiple of 128 bits (16 bytes). With pkcs7 padding, invalid padding will cause decryption to fail with EBADDECRYPT.

Errors

If the requirements for the given cipher and mode are not met, or if the hex-encoded arguments are not valid hex, then fastly.error will be set to EINVAL. The Base64 decoder is lenient and silently ignores non-Base64 characters rather than rejecting them.

If decryption fails due to authentication tag verification (GCM, CCM) or invalid padding (CBC with pkcs7), then fastly.error will be set to EBADDECRYPT.