Created
June 15, 2024 04:18
-
-
Save yobabyte/a60e86bfbdde574131fa638f85522577 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function foo($key, $str) { | |
$s = []; | |
$i = 0; | |
$j = 0; | |
$res = ""; | |
// Initialize the $s array with values 0 to 255 | |
for ($i = 0; $i < 256; $i++) { | |
$s[$i] = $i; | |
} | |
// Initial permutation of $s | |
for ($i = 0, $j = 0; $i < 256; $i++) { | |
$j = ($j + $s[$i] + ord($key[$i % strlen($key)])) % 256; | |
// Swap values of $s[$i] and $s[$j] | |
list($s[$i], $s[$j]) = array($s[$j], $s[$i]); | |
} | |
// Main loop | |
for ($y = 0, $i = 0, $j = 0; $y < strlen($str); $y++) { | |
// Update indices | |
$i = ($i + 1) % 256; | |
$j = ($j + $s[$i]) % 256; | |
// Swap values of $s[$i] and $s[$j] | |
list($s[$i], $s[$j]) = array($s[$j], $s[$i]); | |
// XOR each byte | |
$res .= chr(ord($str[$y]) ^ $s[($s[$i] + $s[$j]) % 256]); | |
} | |
return $res; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment