generateSecretKey

Creates a secret encryption key that can be used with the encrypt() and decrypt() functions.

generateSecretKey([algorithm] [,keysize]) → returns string

Argument Reference

algorithm string
Default: AES

Encryption algorithm used to generate the key. Common values are AES, DES, BLOWFISH, and DESEDE (Triple DES). The generated key cannot be used with the default CFMX_COMPAT algorithm.
Values:
  • AES
  • BLOWFISH
  • DES
  • DESEDE

keysize numeric

Optional key size in bits for the generated encryption key. Common AES key sizes are 128, 192, and 256. If this value is not provided, the default size depends on the Java version used by the server. For example, Java 19 and later may automatically generate 256-bit AES keys instead of 128-bit keys.
Values:
  • 128
  • 192
  • 256
  • 512

Compatibility

ColdFusion:

Version 7+ ColdFusion 2025 running on Java 19 or later may generate AES 256-bit keys by default when keysize is not specified.

Lucee:

Available encryption algorithms may vary depending on the Java version and JVM configuration.

Links more information about generateSecretKey

Examples
Sample code invoking the generateSecretKey function

Generate a secret key and use it to encrypt and decrypt a message.

message = "Top Secret";

// Generate AES secret key
secretKey = generateSecretKey("AES", 256);

// Encrypt the message
encryptedText = encrypt(message, secretKey, "AES", "Base64");

// Decrypt the message
decryptedText = decrypt(encryptedText, secretKey, "AES", "Base64");

writeDump({
    encrypted = encryptedText,
    decrypted = decryptedText
});

Expected Result: Displays the encrypted and decrypted message.

Signup for cfbreak to stay updated on the latest news from the ColdFusion / CFML community. One email, every friday.

Fork me on GitHub