SHARE

RC4+ is a modified version of RC4 with a more complex three-phase key schedule (taking about 3× as long as RC4, or the same as RC4-drop512), and a more complex output function which performs four additional lookups in the S array for each byte output, taking approximately 1.7× as long as basic RC4.

We provide some function of RC4+ stream cipher implement in PHP and Javascript
PHP

<?php

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

function rc4plus($str, $key)
{
    //Key-scheduling algorithm (KSA)
    for ($s = array(), $i = 0; $i < 256; $i++)
        $s[$i] = $i;
    for ($k = 0; $k < 3; $k++)
        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;
        }
    //Pseudo-random generation algorithm (PRGA)
    for ($i = 0, $j = 0, $x = 0, $res = '', $y = 0; $y < strlen($str); $y++)
    {
        $i = ($i + 1) % 256;
        $x = $s[$i];
        $j = ($j + $x) % 256;
        $x = $s[$i];
        $s[$i] = $s[$j];
        $s[$j] = $x;
        $x = $s[(($i << 5) ^ ($j >> 3)) % 256] + $s[(($j << 5) ^ ($i >> 3)) % 256];
        $res .= $str[$y] ^ chr((($s[($s[$j] + $s[$i]) % 256] + $s[($x ^ 0xAA) % 256]) ^ $s[($j + $s[$i]) % 256]) %
            256);
    }
    return $res;
}

?>

Javascript:

function rc4plus(t, e) {
  var i, n, r, a, s, o, s2, v, k;
  for (i = [], n = 0, a = "", s = 0; s < 256; s++) i[s] = s;
  for (k = 0; k < 3; k++)
    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);
  for (s = 0, n = 0, o = 0, r = 0; o < t.length; o++)
    s = (s + 1) % 256,
    r = i[s],
    n = (n + r) % 256,
    r = i[s],
    i[s] = i[n],
    i[n] = r,
    r = i[((s << 5) ^ (n >> 3)) % 256] + i[((n << 5) ^ (s >> 3)) % 256],
    a += String.fromCharCode(t.charCodeAt(o) ^ ((i[(i[s] + i[n]) % 256] + i[(r ^ 0xAA) % 256]) ^ i[(n + i[s]) % 256]) % 256);
  return a
}

Example:

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

Result:

www.tutorialspots.com

Try it yourself

1 COMMENT