Page 1 of 1

Using arcfour cipher with mcrypt extension

Posted: Tue Jun 10, 2008 2:26 am
by tonisscorp
Hi,

I need to encrypt data using the RC4 encryption. I already installed mcrypt (windows) and it works for ciphers such as MCRYPT_RC2 and MCRYPT_RIJNDAEL_128.

Running phpinfo() gives me the following:

Supported ciphers: cast-128 gost rijndael-128 twofish arcfour cast-256 loki97 rijndael-192 saferplus wake blowfish-compat des rijndael-256 serpent xtea blowfish enigma rc2 tripledes

That's probably why I can't use ARCFOUR. But it does say in the manual that arcfour is included in libmcrypt > 2.4.x.
http://de.php.net/manual/en/mcrypt.ciphers.php

I'm running Version 2.5.7 and Api No 20021217.

I also saw that there is a arcfour.h and arcfour.c file in the algorithms folder. So why aren't they recognized?

Thanks for any help. I really need to make this encryption work.

Re: Using arcfour cipher with mcrypt extension

Posted: Tue Jun 10, 2008 2:29 am
by Benjamin
There are a few pure PHP solutions floating around. I found one for you.

http://www.devhome.org/php/tutorials/rc4crypt.html

Re: Using arcfour cipher with mcrypt extension

Posted: Wed Jun 11, 2008 9:32 am
by tonisscorp
Yes,

thank you for the link. I already tried this class and it doesn't produce the correct encryption. Although it seems to be very popular on sourceforge.

But thanks.

Re: Using arcfour cipher with mcrypt extension

Posted: Thu Jun 12, 2008 5:35 am
by tonisscorp
ok,

I got it to work. ARCFOUR is only a STREAM cipher! :)

But something still doesn't work. Mcrypt seems to have a problem when encoding special characters such as ð ê ¢ Ø ò ç.

Regular strings seem to work though.

Maybe a unicode issue.

Re: Using arcfour cipher with mcrypt extension

Posted: Thu Jun 12, 2008 5:51 am
by Benjamin
I found this online a long time ago, it originally had a bug in it which I fixed. Maybe it will work for you.

Code: Select all

 
function RC4($pwd, $data) {
    $pwd_length = strlen($pwd);
    $x = $Zcrypt = $j = $a = null;
    for ($i = 0; $i <= 255; $i++)
    {
          $key[$i] = ord(substr($pwd, ($i % $pwd_length)+1, 1));
          $counter[$i] = $i;
    }
 
    for ($i = 0; $i <= 255; $i++)
    {
        $x = ($x + $counter[$i] + $key[$i]) % 256;
        $temp_swap = $counter[$i];
        $counter[$i] = $counter[$x];
        $counter[$x] = $temp_swap;
    }
 
    for ($i = 0; $i < strlen($data); $i++)
    {
        $a = ($a + 1) % 256;
        $j = ($j + $counter[$a]) % 256;
        $temp = $counter[$a];
        $counter[$a] = $counter[$j];
        $counter[$j] = $temp;
        $k = $counter[(($counter[$a] + $counter[$j]) % 256)];
        $Zcipher = ord(substr($data, $i, 1)) ^ $k;
        $Zcrypt .= chr($Zcipher);
    }
 
    return $Zcrypt;
}