1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62:
<?php
function rc4 ($pass, $message)
{
$k = unpack( 'C*', $pass );
for ($i=0;$i<=255;$i++) {
$state[$i]=$i;
}
$x=0;$y=0;
for ($x=0;$x<=255;$x++) {
$y=($k[($x % count($k))+1]+$state[$x]+$y)%256;
$temp=$state[$x];
$state[$x]=$state[$y];
$state[$y]=$temp;
}
$MAX_CHUNK_SIZE = 1024;
$num = strlen($message) / $MAX_CHUNK_SIZE;
$int = floor ($num);
$int == $num ? $int : $int+1;
$num_pieces=$int;
$x=0;$y=0;
for ($piece=0;$piece<=$num_pieces;$piece++) {
$mess_arr=unpack ("C*", substr($message, $piece * $MAX_CHUNK_SIZE, $MAX_CHUNK_SIZE));
for($i=1;$i<=count($mess_arr);$i++) {
if (++$x > 255) {
$x = 0 ;
}
if (($y += $state[$x]) > 255) {
$y -= 256 ;
}
$temp=$state[$x];
$state[$x]=$state[$y];
$state[$y]=$temp;
$mess_arr[$i]^=$state[( $state[$x] + $state[$y] ) % 256];
}
$addition='';
foreach ($mess_arr as $char) {
$addition.=pack("C*",$char);
}
$message=substr($message,0,$piece * $MAX_CHUNK_SIZE).$addition.substr($message,($piece+1)*$MAX_CHUNK_SIZE);
}
return $message;
}
?>