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