1<?php declare(strict_types=1); 2 3namespace PrinsFrank\PdfParser\Document\Encryption; 4 5/** @internal NEVER USE THIS FOR SECURITY, THIS IS AN INSECURE ALGORITHM */ 6class RC4 { 7 public static function crypt(string $key, string $data): string { 8 $s = range(0, 255); 9 $j = 0; 10 11 for ($i = 0; $i < 256; $i++) { 12 $j = ($j + $s[$i] + ord($key[$i % strlen($key)])) % 256; 13 [$s[$i], $s[$j]] = [$s[$j], $s[$i]]; 14 } 15 16 $i = $j = 0; 17 $output = ''; 18 foreach (str_split($data) as $byte) { 19 $i = ($i + 1) % 256; 20 $j = ($j + $s[$i]) % 256; 21 [$s[$i], $s[$j]] = [$s[$j], $s[$i]]; 22 23 $k = $s[($s[$i] + $s[$j]) % 256]; 24 $output .= chr(ord($byte) ^ $k); 25 } 26 27 return $output; 28 } 29} 30