ColdFusion 7 Strong Encryption
By Pete Freitag
ColdFusion MX 7 adds strong encryption support to the Encrypt and Decrypt functions. In addition to the legacy algorithm used in Encrypt, and Decrypt - ColdFusion MX 7 now makes it incredibly easy to use AES, Blowfish, DES, and Triple DES encryption. It also adds the ability to encode the encrypted string using three different binary encoding algorithms Base64, Hexidecimal, and the UUEncode algorithm.
Here's an example:
<!--- options for algorithm are CFMX_COMPAT (default), AES, BLOWFISH, DES, and DESEDE ---> <cfset algorithm = "AES"> <!--- encoding options, Base64, hex, or uu ---> <cfset encoding = "hex"> <!--- generate a key ---> <cfset key = GenerateSecretKey(algorithm)> <cfset str = "This is my secret string." > <cfset enc = Encrypt(str, key, algorithm, encoding)> <cfset dec = Decrypt(enc, key, algorithm, encoding)> <cfoutput> <pre> string=#str# encrypted=#enc# decrypted=#dec# key=#key# algorithm=#algorithm# </pre> </cfoutput>
Encoding
The default encoding algorithm is UUEncode, this algorithm however may not be best if you need to pass the encrypted value around (as the possible character values are greatest). The safest choice for encoding is hex
which will only use the characters A-F and 0-9 - it also will yield the longest string. The next best choice is Base64 encoding, this encoding will use characters a-z A-Z 0-9 and sometimes will use = signs at the end for padding.
Encryption Algorithms
The DES (Data Encryption Standard) algorithm was developed in the US in the 1970's by the NSA. DES is no longer considered secure and can be broken in hours or days by exhaustive key search. There are around 72 quadrillion possible keys.
Triple DES (DESEDE) to make it harder to break we encrypt using one key, encrypt using another key, and finally decrypt using the first key. Triple DES is still considered a secure algorithm, and is in wide use.
The AES/Rijndael (Advanced Encryption Standard) algorithm is your strongest choice, it uses at least 128 bit keys (can use 128, 192, or 256), and even executes faster than DES and Triple DES algorithms (which use 56 bit keys).
Assuming that one could build a machine that could recover a DES key in a second (i.e., try 255 keys per second), then it would take that machine approximately 149 thousand-billion (149 trillion) years to crack a 128-bit AES key.NIST AES Fact Sheet (link no longer works; http://csrc.nist.gov/CryptoToolkit/aes/aesfact.html)
The blowfish algorithm was also designed as a replacement to DES - it uses variable key lengths (32-448 bits) and is appropriate for both domestic (US) and international use. Blowfish is significantly faster than DES (20x).
Algorithm | Strength | Speed | Key Length |
DES | Low | Slow | 256 |
Triple DES | Good | Slowest | 256 |
Blowfish | Strong | Fast, but time consuming to initialize a new key | 232 - 2448 |
AES | Strong | Fast | 2128, 2192, 2256 |
Other Sources: An introduction to modern crypto systems (link no longer works: http://www.giac.org/practical/GSEC/Andrew_Zwicke_GSEC.pdf), and Do we need AES? (link no longer applicable: http://www.cryptomathic.com/company/aes.html), Description of a new variable-length key, 64-bit block cipher (blowfish) all good reads.
ColdFusion 7 Strong Encryption was first published on February 10, 2005.
If you like reading about cfml, coldfusion 7, or crypto then you might also like:
- Hash in ColdFusion
- Strong Encryption Technote shows undocumented features
- CFFUNCTION and CFARGUMENT don't support new types in ColdFusion 7
- CFTIMER - Little things in ColdFusion 7
The FuseGuard Web Application Firewall for ColdFusion & CFML is a high performance, customizable engine that blocks various attacks against your ColdFusion applications.
CFBreak
The weekly newsletter for the CFML Community
Comments
SHA was developed as a replacement for triple-DES. I would use SHA-1 or higher over triple-DES any day.
"SHA-1, SHA-224, SHA-256, SHA-384, and SHA-512 are the required secure hash algorithms for use in U.S. Federal applications, including use by other cryptographic algorithms and protocols, for the protection of sensitive unclassified information. FIPS PUB 180-1 also encouraged adoption and use of SHA-1 by private and commercial organizations."
- http://en.wikipedia.org/wiki/SHA1
Try SHA1 at:
http://www.cs.eku.edu/faculty/styer/460/Encrypt/JS-SHA1.html
(sorry, will not work with safari users)
.pjf
You might also use other encryption methods if your integrating with a legacy system, or there are cryptography restrictions (blowfish is safe to use internationally).
I can think of a couple of things:
1. Be compatible with older systems or athentication methods - web services, interfacing with other systems, etc.
2. If I remember correctly, AES is subject to U.S. customs export controls. In short, it would be illegal to export it to specific countries (the list is quite long with I remember correctly).
Best,
.pjf
Regards, Dave.
AES is a data encryption/DECRYPTION protocol.
SHA-1, et al, are hashing functions - it's a one-way encrypt. You can't take an SHA-1 encryption and restore the original data.
Yeah, I wasn't very clear about that - sha is a one-way hash. I wasn't thinking about decrypting anything. Just about a translucent DB I was working on.
.pjf
If you don't mind using UDFs, there are ones for SHA1, SHA256, and RIPEMD160 over at cflib.org.
I already use them... ;-)
.pjf
this article (http://cfdj.sys-con.com/read/46359.htm) but was just wondering if anything was easier in 7.
One interesting thing I pulled form the docs on the new encryption features is this gem:
"The JCE framework includes facilities for using other provider implementations; however, Macromedia cannot provide technical support for third-party security providers."
What this means is that the encryption framework in CF is now plugable, and you can theoretically add any additional encryption types supportable through JCE.