Using arcfour cipher with mcrypt extension

Need help installing PHP, configuring a script, or configuring a server? Then come on in and post your questions! We'll try to help the best we can!

Moderator: General Moderators

Post Reply
tonisscorp
Forum Newbie
Posts: 3
Joined: Mon Jun 09, 2008 10:34 am

Using arcfour cipher with mcrypt extension

Post 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.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: Using arcfour cipher with mcrypt extension

Post by Benjamin »

There are a few pure PHP solutions floating around. I found one for you.

http://www.devhome.org/php/tutorials/rc4crypt.html
tonisscorp
Forum Newbie
Posts: 3
Joined: Mon Jun 09, 2008 10:34 am

Re: Using arcfour cipher with mcrypt extension

Post 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.
tonisscorp
Forum Newbie
Posts: 3
Joined: Mon Jun 09, 2008 10:34 am

Re: Using arcfour cipher with mcrypt extension

Post 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.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: Using arcfour cipher with mcrypt extension

Post 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;
}
 
Post Reply