Encrypts a string, using a symmetric key-based algorithm, in which the same key is used to encrypt and decrypt a string. The security of the encrypted string depends on maintaining the secrecy of the key, and the algorithm choice. Algorithm support is determined by the installed default JCE provider in Lucee or ColdFusion Standard. On ColdFusion Enterprise the algorithms are provided by the FIPS certified RSA BSafe Crypto-J JCE provider.
encrypt(string, key [, algorithm [, encoding] [, iv | salt [, iterations]]])
→ returns string
CFMX_COMPAT
algorithm, any combination of any number of characters; used as a seed used to generate a 32-bit encryption key.GenerateSecretKey
function to generate the key.
CFMX_COMPAT
CFMX_COMPAT
AES
BLOWFISH
DES
DESEDE
DESX
RC2
RC4
RC5
PBE
AES/GCM/NoPadding
AES/CBC/PKCS5Padding
AES/CTR/PKCS5Padding
UU
algorithm
parameter.
UU
Base64
Hex
SALT
.algorithm
parameter.
IV
.algorithm
parameter.
0
algorithm
parameter with a Password Based Encryption (PBE) algorithm.salt
parameter. Do not specify this parameter for Block Encryption Algorithms.IVorSalt
attribute.
The key must be generated using the generateSecretKey("AES") function.
encrypt("top secret", "WTq8zYcZfaWVvMncigHqwQ==", "AES", "Base64")
Expected Result: keciULin7bxOWvN/BOarWw==
By default encrypt() uses the Electronic Code Book (ECB) mode for encryption.
For increased security you should specify the mode and padding to use. In this example we will use CBC mode and PKCS5Padding. The value of the encrypted string will be different every time it runs because the IV is generated at random.
msg = 'data to encrypt';
key = generateSecretKey('AES');
encMsg = encrypt( msg, key, 'AES/CBC/PKCS5Padding', 'HEX');
writeOutput( encMsg );
Using GCM mode works CF 2016+ after update 2. It does not currently work on Lucee (bug: LDEV-904)
msg = 'data to encrypt';
key = generateSecretKey('AES');
encMsg = encrypt( msg, key, 'AES/GCM/NoPadding', 'Base64');
writeOutput( encMsg );
Signup for cfbreak
to stay updated on the latest news from the ColdFusion / CFML community. One email, every friday.