I was using cfusion_encrypt
and cfusion_decrypt
today, and decided that even though they don't appear to be going anywhere, they might at some point, and dealing with it shouldn't be a big deal. I'm also pretty sure that BD doesn't have the functions. So I thought why not write my own?
<cffunction name="fusion_encrypt" output="false" returntype="string"> Â <cfargument name="string" type="string" required="true" /> Â <cfargument name="key" type="string" required="true" /> Â <cfset var i = "" /> Â <cfset var result = "" /> Â <cfset key = repeatString(key, ceiling(len(string) / len(key))) /> Â <cfloop from="1" to="#len(string)#" index="i"> Â Â Â <cfset result = result & rJustify(formatBaseN(binaryXOR(asc(mid(string, i, 1)), asc(mid(key, i, 1))), 16), 2) /> Â </cfloop> Â <cfreturn replace(result, " ", "0", "all") /> </cffunction> <cffunction name="fusion_decrypt" output="false" returntype="string"> Â <cfargument name="string" type="string" required="true" /> Â <cfargument name="key" type="string" required="true" /> Â <cfset var i = "" /> Â <cfset var result = "" /> Â <cfset key = repeatString(key, ceiling(len(string) / 2 / len(key))) /> Â <cfloop from="2" to="#len(string)#" index="i" step="2"> Â Â Â <cfset result = result & chr(binaryXOR(inputBaseN(mid(string, i - 1, 2), 16), asc(mid(key, i / 2, 1)))) /> Â </cfloop> Â <cfreturn result /> </cffunction> <cffunction name="binaryXOR" output="false" returntype="numeric"> Â <cfargument name="n1" type="numeric" required="true" /> Â <cfargument name="n2" type="numeric" required="true" /> Â <cfset n1 = formatBaseN(n1, 2) /> Â <cfset n2 = formatBaseN(n2, 2) /> Â <cfreturn inputBaseN(replace(n1 + n2, 2, 0, "all"), 2) /> </cffunction>
The binaryXOR
is a requirement for both functions, but it could easily be inlined out of existance if the extra UDF is a problem. Here's some code to illustrating:
<cfset key = "test" /> <cfoutput> <table> <cfloop list="barney,is,damn cool!" index="i"> Â <tr> Â Â Â <td>#i#</td> Â Â Â <td>#cfusion_encrypt(i, key)#</td> Â Â Â <td>#fusion_encrypt(i, key)#</td> Â Â Â <td>#cfusion_decrypt(cfusion_encrypt(i, key), key)#</td> Â Â Â <td>#fusion_decrypt(fusion_encrypt(i, key), key)#</td> Â </tr> </cfloop> </table> </cfoutput>
Thanks! This came in handy now that these functions have been removed from ColdFusion 11. But in order to keep the results IDENTICAL I added UCASE() to the cfreturn in fusion_encrypt. I did a test of looping 10,000 times, creating a new UUID each iteration and sending that UUID into your function and the native one. All 10,000 matched. Good enough for me. UUIDs only use alphanumerics so I'll test later using other special characters. Thanks again.
Thanks a lot this helped me in my pursuit to develop a C# equivalent to cfusion_encrypt. :)