## RC4A cipher implement in PHP and Javascript

RC4A is one of RC4 variants. Souradyuti Paul and Bart Preneel have proposed an RC4 variant, which they call RC4A.

We provide some function implement of RC4A cipher in PHP and Javascript:

PHP

<?php

/**
* @author www.Tutorialspots.com
*/

function rc4a($str,$key)
{
//Key-scheduling algorithm (KSA)
for ($s = array(),$i = 0; $i < 256;$i++)
$s[$i] = $i; for ($j = 0, $i = 0;$i < 256; $i++) {$j = ($j +$s[$i] + ord($key[$i % strlen($key)])) % 256;
$x =$s[$i];$s[$i] =$s[$j];$s[$j] =$x;
}

//Simple method for s2
$s2 = array_reverse($s);

//Pseudo-random generation algorithm (PRGA)
for ($i = 0,$j = 0, $j2 = 0,$res = '', $y = 0;$y < strlen($str);$y++)
{
$i = ($i + 1) % 256;
$j = ($j + $s[$i]) % 256;
$x =$s[$i];$s[$i] =$s[$j];$s[$j] =$x;
$v =$str[$y] ^ chr($s[($s[$i] + $s[$j]) % 256]);
$j2 = ($j2 + $s2[$i]) % 256;
$x =$s2[$i];$s2[$i] =$s2[$j2];$s2[$j2] =$x;
$res .=$v ^ chr($s[($s2[$i] +$s2[$j2]) % 256]); } return$res;
}

function rc4av2($str,$key)
{
//Key-scheduling algorithm (KSA)
for ($s = array(),$i = 0; $i < 256;$i++)
$s[$i] = $i; for ($j = 0, $i = 0;$i < 256; $i++) {$j = ($j +$s[$i] + ord($key[$i % strlen($key)])) % 256;
$x =$s[$i];$s[$i] =$s[$j];$s[$j] =$x;
}

$s2 =$s;
for ($j = 0,$i = 0; $i < 256;$i++)
{
$j = ($j + $s2[$i] + ord($key[$i % strlen($key)])) % 256;$x = $s2[$i];
$s2[$i] = $s2[$j];
$s2[$j] = $x; } //Pseudo-random generation algorithm (PRGA) for ($i = 0, $j = 0,$j2 = 0, $res = '',$y = 0; $y < strlen($str); $y++) {$i = ($i + 1) % 256;$j = ($j +$s[$i]) % 256;$x = $s[$i];
$s[$i] = $s[$j];
$s[$j] = $x;$v = $str[$y] ^ chr($s[($s[$i] +$s[$j]) % 256]);$j2 = ($j2 +$s2[$i]) % 256;$x = $s2[$i];
$s2[$i] = $s2[$j2];
$s2[$j2] = $x;$res .= $v ^ chr($s[($s2[$i] + $s2[$j2]) % 256]);
}
return \$res;
}

?>

Example:

var_dump(Hash::rc4a(Hash::rc4a('www.Tutorialspots.com','Tutorialspots'),'Tutorialspots'));

Result:

www.Tutorialspots.com

Javascript

function rc4a(t, e) {
var i, n, r, a, s, o, s2, v;
for (i = [], n = 0, a = "", s = 0; s < 256; s++) i[s] = s;
for (s = 0; s < 256; s++)
n = (n + i[s] + e.charCodeAt((s % e.length))) % 256,
r = i[s],
i[s] = i[n],
i[n] = (r);
s2 = i.reverse();
for (s = 0, n = 0, o = 0, j2 = 0; o < t.length; o++)
s = ((s + 1) % 256),
n = ((n + i[s]) % 256),
r = i[s],
i[s] = i[n],
i[n] = r,
v = t.charCodeAt(o) ^ i[(i[s] + i[n]) % 256],
j2 = (j2 + s2[s]) % 256,
r = s2[s],
s2[s] = s2[j2],
s2[j2] = r,
a += String.fromCharCode(v ^ i[(s2[s] + s2[j2]) % 256]);
return a
}
function rc4av2(t, e) {
var i, n, r, a, s, o, s2, v;
for (i = [], n = 0, a = "", s = 0; s < 256; s++) i[s] = s;
for (s = 0; s < 256; s++)
n = (n + i[s] + e.charCodeAt((s % e.length))) % 256,
r = i[s],
i[s] = i[n],
i[n] = r;
s2 = i;
for (s = 0; s < 256; s++)
n = (n + s2[s] + e.charCodeAt((s % e.length))) % 256,
r = s2[s],
s2[s] = s2[n],
s2[n] = r;
for (s = 0, n = 0, o = 0, j2 = 0; o < t.length; o++)
s = ((s + 1) % 256),
n = ((n + i[s]) % 256),
r = i[s],
i[s] = i[n],
i[n] = r,
v = t.charCodeAt(o) ^ i[(i[s] + i[n]) % 256],
j2 = (j2 + s2[s]) % 256,
r = s2[s],
s2[s] = s2[j2],
s2[j2] = r,
a += String.fromCharCode(v ^ i[(s2[s] + s2[j2]) % 256]);
return a
}

Example:

alert(rc4av2(rc4av2('www.tutorialspots.com', 'tutorialspots'), 'tutorialspots'))

Result:

www.tutorialspots.com

Try it yourself

### 1 Comment

• #### RC4 implementation in PHP and Javascript | Free Online Tutorials

(January 19, 2017 - 1:51 pm)

[…] Some RC4 variants RC4A cipher implement in PHP and Javascript […]