Mini PHP RC4 Encryption Class

Just a little portable PHP RC4 Encryption class...

<?php
// Mini RC4 Encryption Class

$key = "mrkey";
$data = "this is very secret";

$encrypted = RC4::Encrypt($key,$data);
$decrypted = RC4::Decrypt($key,$encrypted);

echo '<pre>Encrypted: '.bin2hex($encrypted).' (Hex Encoded)
Decrypted: '.$decrypted.'</pre>';


class RC4{
   // Mini RC4 Encryption Class - Brad
   public function Encrypt($a,$b){
      for($i,$c;$i<256;$i++)$c[$i]=$i;
      for($i=0,$d,$e,$g=strlen($a);$i<256;$i++){
         $d=($d+$c[$i]+ord($a[$i%$g]))%256;
         $e=$c[$i];
         $c[$i]=$c[$d];
         $c[$d]=$e;
      }
      for($y,$i,$d=0,$f;$y<strlen($b);$y++){
         $i=($i+1)%256;
         $d=($d+$c[$i])%256;
         $e=$c[$i];
         $c[$i]=$c[$d];
         $c[$d]=$e;
         $f.=chr(ord($b[$y])^$c[($c[$i]+$c[$d])%256]);
      }
      return $f;
   }
   public function Decrypt($a,$b){return RC4::Encrypt($a,$b);}
}
?>


Posted 2/12/2012 by Brad