CFML implementation of Password-Based Key-Derivation Function (PBKDF)
generatePBKDFKey(algorithm, passphrase, salt, iterations, keySize);
→ returns string
PBKDF2WithHmacSHA1PBKDF2WithSHA1PBKDF2WithSHA224PBKDF2WithSHA256PBKDF2WithSHA384PBKDF2WithSHA512PBKDF2WithHmacSHA256PBKDF2WithHmacSHA384PBKDF2WithHmacSHA512PBKDF2WithHmacSHA512) .
iterations and keySize parameters are optional in Lucee.
bx-password-encrypt module
The PBKDF2WithHmacSHA1 algorithm will work on older JVMs, or older versions of CF
generatePBKDFKey("PBKDF2WithHmacSHA1", "secret", "salty", 5000, 128)
Expected Result: Y0MCpCe3zb0CNJvyXNUWEQ==
// some variables
password = "top_secret";
dataToEncrypt= "the most closely guarded secret";
encryptionAlgorithm = "AES";
keysize = 128;
algorithmVersion = 512;
PBKDFalgorithm = 'PBKDF2WithHmacSHA' & algorithmVersion;
// Generate key as recommended in docs
length = keysize / 8;
multiplicator = 10 ^ length;
salt = Round(Randomize(5,'SHA1PRNG') * multiplicator);
// The magic happens here
PBKDFKey = GeneratePBKDFKey(PBKDFalgorithm, password, salt, algorithmVersion, keysize);
encryptedData = encrypt(dataToEncrypt, PBKDFKey, encryptionAlgorithm, "BASE64");
decryptedData = decrypt(encryptedData, PBKDFKey, encryptionAlgorithm, "BASE64");
//Output
writeOutput("<b>Generated PBKDFKey (Base 64)</b>: " & PBKDFKey);
writeOutput("<br /><b>Data After Encryption</b>: " & encryptedData);
writeOutput("<br /><b>Data After Decryption</b>: " & decryptedData);
Signup for cfbreak to stay updated on the latest news from the ColdFusion / CFML community. One email, every friday.