Skip to main content
Participant
July 17, 2006
Question

Help! Converting javascript function to cfml

  • July 17, 2006
  • 4 replies
  • 875 views
Can anyone help me to convert the javascript function shown below to cfml. Its a shorts script used to provide RC4 encryption/decryption. I've done most of the work but I get run errors that I can't resolve. Some of the initial variable values I've changed from 0 to 1 because cfml arrays start at 1 whereas javascript start at 0
Any help would be much appreciated. Thanks

<!---

function rc4(key, text) {
var i=0;
var x=0;
var y=0;
var t=0;
var x2=0;

s=new Array(0);

for (i=0; i<256; i++) {
s =i;
}

y=0;
for (x=0; x<256; x++) {
y=(key.charCodeAt(x % key.length) + s + y) % 256;
t=s; s=s; s=t;
}

x=0;
y=0;
var z="";
for (x=0; x<text.length; x++) {
x2=x % 256;
y=( s[x2] + y) % 256;
t=s[x2]; s[x2]=s; s=t;
z+= String.fromCharCode((text.charCodeAt(x) ^ s[(s[x2] + s) % 256]))
}
return z
}
--->

<cfset key = '123456789'>
<cfset text = 'this is my secret message'>



<cfset y = 0>
<cfset t = 0>
<cfset x2 = 0>

<cfset s = ArrayNew(1)>


<cfset i = 1>
<cfloop condition="i LT 257">
<cfset s
= i>
<cfset i = i + 1>
</cfloop>



<cfset x = 1>
<cfloop condition="x LT 257">
<cfset y = ( mid(key, x%len(key), 1) + s +y ) % 256 >
<cfset t = s>
<cfset s = s>
<cfset s = t>
<cfset x = x + 1>
</cfloop>



<cfset y = 0>
<cfset z = "">

<cfset x = 1>
<cfloop condition="x LT 257">
<cfset x2 = x % 256>
<cfset y = (s[x2] + y) % 256>
<cfset t = s[x2]>
<cfset s[x2] = s>
<cfset s = t>
<cfset z = z + chr( asc(x) ^ s[(s[x2] + s) % 256] )>
<cfset x = x + 1>
</cfloop>
    This topic has been closed for replies.

    4 replies

    Participant
    July 23, 2006
    Hi CR,

    Thankyou so much for converting this this for me. You have save me many hours of struggleing with error codes. I will compare the output from the CFML code with the Javascript version and make sure they are the save. When completed I will turn this into a function and post here so anyone wanting to use RC4 encryption in their application can use this work.

    Many thanks

    Reference (for those interested in RC4):
    http://b-con.us/security/rc4.php
    http://www.cebrasoft.co.uk/encryption/rc4.htm

    Participating Frequently
    July 24, 2006
    >Note that the CF BitOR function does not produce the same result as the ^ in JS.

    That's for sure, because "^" should be replaced with BitXOR (exclusive OR).
    Inspiring
    July 24, 2006
    Mr. Black nailed it... I kept wondering why saying "Exclusive OR" but typing "BitOR" was just not working. Here is the corrected line if you didn't already.

    <cfset z = z & chr(BitXOR(Asc(mid(text, x, 1)), (s[((s[x2] + s[y2]) mod 256)+1])))>

    That made everything identicle between the two. Thanks for the catch Mr. Black.

    CR
    Inspiring
    July 18, 2006
    This is the closest I can get to the JS code you gave. Note that the CF BitOR function does not produce the same result as the ^ in JS. That was the only thing that was not the same as the JS code.

    CR

    July 18, 2006
    I don't believe the % is a valid ColdFusion operator. The backslash ( \ ) is used to divide one integer by another. For example 9 \ 4 = 2. Another way to look at it is \ produces the quotient and MOD produces the remainder.
    Inspiring
    July 17, 2006
    First main thing I saw... still working through the rest

    <cfset i = 1>
    <cfloop condition="i LT 257">
    <cfset s = i> <!--- added array element --->
    <cfset i = i + 1>
    </cfloop>

    CR
    Participant
    July 17, 2006
    I thought the script for setting up the array was OK. The first run error occured at:

    <cfset y = ( mid(key, x%len(key), 1) + s +y ) % 256 >

    I'm not sure what % does but it doesn't like the way I've coded the mid() function.