VMPC stream cipher implement in PHP and Javascript


VMPC (Variably Modified Permutation Composition) is a stream cipher similar to the well known and popular cipher RC4 designed by Ron Rivest. It was designed by Bartosz Zoltak, presented in 2004 at the Fast Software Encryption conference. VMPC is a modification of the RC4 cipher.

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

PHP

<?php

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

function vmpc($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++)
    {
        $x = $s[$i];
        $j = $s[($j + $x) % 256];
        $res .= $str[$y] ^ chr($s[$s[($s[$j] + 1) % 256]]);
        $x = $s[$i];
        $s[$i] = $s[$j];
        $s[$j] = $x;
        $i = ($i + 1) % 256;
    }
    return $res;
}

function vmpcv2($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++)
    {
        $x = $s[$i];
        $j = $s[($j + $x) % 256];
        $res .= $str[$y] ^ chr($s[($s[$s[$j]] + 1) % 256]);
        $x = $s[$i];
        $s[$i] = $s[$j];
        $s[$j] = $x;
        $i = ($i + 1) % 256;
    }
    return $res;
}

?>

function vmpcv2 is my modification of VMPC. you can use this instead of function vmpc

Javascript

function vmpc(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++)
    r = i[s],
    n = i[(n + r) % 256],
    a += String.fromCharCode(t.charCodeAt(o) ^ i[i[(i[n]+1) % 256]]),
    r = i[s],
    i[s] = i[n],
    i[n] = r,
    s = (s + 1) % 256;
  return a
}

function vmpcv2(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++)
    r = i[s],
    n = i[(n + r) % 256],
    a += String.fromCharCode(t.charCodeAt(o) ^ i[(i[i[n]] + 1) % 256]),
    r = i[s],
    i[s] = i[n],
    i[n] = r,
    s = (s + 1) % 256;
  return a
}

Example:

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

Result:

www.tutorialspots.com

Try it yourself

1 Comment

Leave a Reply