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>