thecfguy

A Unique Developer

Encrypt query parameter (URL) in alphanumeric format.

I was working in on project in which I required to send link in email which redirect user to my site and automatically login to system and redirect to proper page. For security reason I like to entry URL query parameter, so it will be bit easy, build URL parameter string (ex. userId=123&commid=293&redirecturl=xyz.cfm) and enctry it with coldfusion inbuilt function encrypt. Now the problem is, encrypt function give some special characters which cause issue in URL, it is always better to have only alphanumeric values as URL paramter.

Here is solution to convert URL paramter to alphanumeric encrypted format.

<cfset Secret = 'commid=1&personId=4&redirecturl=/home/test.cfm'><cfset eKey= 'EKEKEIasf(##)sdfewr w'><cfset Encrypted = Encrypt(Secret, eKey)><cfset Secret64 = ToBase64(Encrypted)>

As a result you will get Encrypted format
"M=-P4D?>3X/HZ@KINOHF=!:MT>M^7'E]6NYN%AI*\GTK<5Q8>4@(JK(F_RHVX!T@" with lot of special characters. but base64 format will be
"TT0tUDREPz4zWC9IWkBLSU5PSEY9ITpNVD5NXjcnRV02TllOJUFJKlxHVEs8NVE4PjRAKEpLKEZfUkhWWAohVEAgIAo="

Now to decrypt code

<cfset UnlockedSecret = Decrypt(ToString(ToBinary(Secret64)), eKey)>