encrypt
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
Argument Reference
string string Required
key string Required
CFMX_COMPAT algorithm, any combination of any number of characters; used as a seed used to generate a 32-bit encryption key.* For all other algorithms, a key in the format used by the algorithm. For these algorithms, use the GenerateSecretKey function to generate the key. algorithm string
Default: CFMX_COMPAT
CFMX_COMPAT
CFMX_COMPATAESBLOWFISHDESDESEDEDESXRC2RC4RC5PBEAES/GCM/NoPaddingAES/CBC/PKCS5PaddingAES/CTR/PKCS5Padding
encoding string
Default: UU
UU
algorithm parameter. UUBase64Hex
iv binary
SALT.Specify this parameter to adjust ColdFusion encryption to match the details of other encryption software.* For Block Encryption Algorithms: This is the binary Initialization Vector value to use with the algorithm. The algorithm must contain a Feedback Mode other than ECB. This must be a binary value that is exactly the same size as the algorithm block size.NOTE: If you specify this parameter, you must also specify the algorithm parameter. salt binary
IV.Specify this parameter to adjust ColdFusion encryption to match the details of other encryption software.* For Password Based Encryption Algorithms: This is the binary Salt value to transform the password into a key.NOTE: If you specify this parameter, you must also specify the algorithm parameter. iterations numeric
Default: 0
0
algorithm parameter with a Password Based Encryption (PBE) algorithm.NOTE: This parameter is used with the salt parameter. Do not specify this parameter for Block Encryption Algorithms.NOTE: You must use the same value to encrypt and decrypt the data. Compatibility
ColdFusion:
Lucee:
IVorSalt attribute. Links more information about encrypt
- Strong Encryption in CF7 - Overview of the encryption algorithms
Examples Sample code invoking the encrypt function
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 );
© 2012–present Foundeo, Inc. and the CFDocs contributors.
Licensed under the MIT License.
ColdFusion is a trademark of Adobe Systems Incorporated.
https://cfdocs.org/encrypt