Pablo Varando's ColdFusion Blog

Encrypt() - Decrypt() Issues in ColdFusion 8.... Ugggg!

So today I openned up an older application I previously developed in ColdFusion 7. I copied the files to my ColdFusion 8 environment and most everything worked... what didnt? Encryption and Decryption.

I was passing in a "path" that is supposed to get encrypted and then passed in the URL.. but I kept getting pure gibberish... so I openned up the ColdFusion 8 documentation and started my search for a soltuion.

This is the code I have in the ColdFusion 7 system; basically it's a UDF (User Defined Function) that will encrypt a string and then one that will decrypt a string; based on a common system key. Nothing fancy; but it shoudl work.... or should it?


<cfscript>
    //Encrypt a string and append it to URLEncrypted variables
    function EncString(theString){
        tNewString = Encrypt(theString, request.EncKey);
        tNewString = URLEncodedFormat(tNewString);
        return tNewString;
    }
    //Decrypt a string and return its value.
    function DecString(theString){
        tNewString = Decrypt(theString, request.EncKey);
        return tNewString;
    }
</cfscript>

So after a few hours of research, I found that I needed to add a few things to the code... particularly 'CFMX_COMPAT','Hex' those settings... without them it wasnt working! Uggg!


<cfscript>
    //Encrypt a string and append it to URLEncrypted variables
    function EncString(theString){
        tNewString = Encrypt(theString, request.EncKey, 'CFMX_COMPAT','Hex');
        tNewString = URLEncodedFormat(tNewString);
        return tNewString;
    }
    //Decrypt a string and return its value.
    function DecString(theString){
        tNewString = Decrypt(theString, request.EncKey, 'CFMX_COMPAT','Hex');
        return tNewString;
    }
</cfscript>

Apparently, the documentation says that "CFMX_COMPAT" is supposed to be the "default" encryption algorythm; however unless I define it... it wasnt working... Also you need the "Hex" definition right after it... I put that in and voila... it works!

Just wanted to post it on here as it might help someone later on!