Question
Help! Converting javascript function to cfml
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>
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
t=s
}
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
z+= String.fromCharCode((text.charCodeAt(x) ^ s[(s[x2] + s
}
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
<cfset i = i + 1>
</cfloop>
<cfset x = 1>
<cfloop condition="x LT 257">
<cfset y = ( mid(key, x%len(key), 1) + s
<cfset t = s
<cfset s
<cfset s
<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
<cfset z = z + chr( asc(x) ^ s[(s[x2] + s
<cfset x = x + 1>
</cfloop>