Heres a function that I modified from a RC4 script found at http://www.xs4all.nl/~cg/ciphersaber/ This version is written a little better then the version by Ian Gulliver, and it is better fit for text because of the base64 decode, and the serialize feature is also nice in some scenarios. But the script by Ian Gulliver can binary files(I think) were mine was made with plaintext encryption in mind.
<edit>mcrypt is no good, it can only encypt upto 400 chars</edit>
<?php
# Copyright (c) 2003 [email]jasonlester@lycos.co.uk[/email]
# This program is free software. You can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License.
# ARCFOUR stream cipher encryption algorithm with CipherSaber-2 modification.
# ARCFOUR (aka RC4) is an industry standerd for strong encryption, and is aclaimed as an industry leader in performance.
# To vouch for its stregth, products such as Microsoft Terminal Server, Microsoft CryptoAPI 2.0 for Windows Server 2003 and Oracle9 databse
# with Oracle Advanced Security use the RC4 encryption algorithm.
# For information on ARCFOUR goto [url]http://www.mozilla.org/projects/security/pki/nss/draft-kaukonen-cipher-arcfour-03.txt[/url]
# ARC4() works as follows: ARC4( $data, $pass, "Action");
# The "action" is what you want ARC4 to do:
# action "e-1" - Encrypt data with ARC4.
# It is used like this: ARC4( $data, $pass, "e-1");
# action "d-1" - Decyrpt data encrypted by action e-1.
# It is used like this: ARC4( $data, $pass, "d-1");
# action "e-2" - Encrypt data with ARC4 and then base64 encode encrypted data.
# It is used like this: ARC4( $data, $pass, "e-2");
# action "d-2" - base64 decode and then decrypt data.
# It is used like this: ARC4( $data, $pass, "d-2");
# action "e-3" - Serialize input, Encrypt data with ARC4 and then base64 encode encrypted-serialized data.
# It is used like this: ARC4( $data, $pass, "e-3");
# action "d-3" - base64 decode, decrypt and then unserialize data.
# It is used like this: ARC4( $data, $pass, "d-3");
# $ivcl sets the character length of the initialization vector. The larger the better, but for security reasons the pass phrease shoud be 4-5 times
# LARGER then the IV. With a IV of ten (default) your pass should be 40-50 characters in length, or around 10-15 words. But this is only for
# MAX security. The more you vear away from it the less secure the encyption will be, but the importance of your encryption is only as
# important as what your encrypting, so you can be the judge.
function ARC4($p, $d, $action)
{ $ivcl= 10;
$N = 1;
static $randset = 0;
if($action == "d-1")
{ $k = substr($d, 0, $ivcl);
$d = substr($d, $ivcl);
}
elseif($action == "d-2" || $action =="d-3")
{ $d = base64_decode($d);
$k = substr($d, 0, $ivcl);
$d = substr($d, $ivcl);
}
elseif($action == "e-1" || $action == "e-2" || $action == "e-3")
{ $k="";
if($ivcl>0)
{ while(strlen($k)<$ivcl)
{ switch(mt_rand(1,3))
{
case 1:
$k.=chr(mt_rand(48,57));
break;
case 2:
$k.=chr(mt_rand(65,90));
break;
case 3:
$k.=chr(mt_rand(97,122));
break;
}
}
}
}
if($action == "e-3")
{ $d = serialize($d);
}
$p .= $k;
for ($i=0; $i < 255; $i++)
$S[$i] = $i;
$j = 0;
$t = strlen($p);
for ($i=0; $i < 255; $i++)
{ $K[$i] = ord(substr($p,$j,1));
$j = ($j + 1) % $t;
}
$j=0;
for ($kk=0; $kk < $N; $kk++)
{ for ($i = 0; $i < 255; $i++)
{ $j = ($j + $S[$i] + $K[$i]) & 0xff;
$t = $S[$i];
$S[$i] = $S[$j];
$S[$j] = $t;
}
}
$i=0;
$j=0;
$ii=0;
$ret = '';
$dlen = strlen($d);
for ($ii=0; $ii < $dlen; $ii++)
{ $c=$d{$ii};
$i = ($i + 1) & 0xff;
$j = ($j + $S[$i]) & 0xff;
$t = $S[$i];
$S[$i] = $S[$j];
$S[$j] = $t;
$t = ($S[$i] + $S[$j]) & 0xff;
$ret .= chr($S[$t]) ^ $c;
}
if($action == "d-1" || $action == "d-2")
{ return $ret;
}
elseif($action == "d-3")
{ $ret = unserialize(stripslashes($ret));
return $ret;
}
elseif($action == "e-1")
{ return $k .= $ret;
}
elseif($action == "e-2" || $action == "e-3")
{ return base64_encode($k .= $ret);
}
}