2020-10-03 07:26:14 +00:00
|
|
|
<?php
|
2022-07-23 20:51:04 -06:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The XOR cipher is a type of additive cipher.
|
2022-07-23 20:41:10 -06:00
|
|
|
* Each character is bitwise XORed with the key.
|
|
|
|
* We loop through the input string, XORing each
|
|
|
|
* character with the key.
|
|
|
|
* The key is repeated until it is the same length as the input.
|
|
|
|
*
|
|
|
|
* @param string $input The input string.
|
|
|
|
* @param string $key The key to use.
|
|
|
|
* @return string The encrypted string.
|
|
|
|
*/
|
|
|
|
function xorCipher(string $input_string, string $key)
|
2020-10-03 07:26:14 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
$key_len = strlen($key);
|
|
|
|
$result = array();
|
|
|
|
|
2022-07-23 20:41:10 -06:00
|
|
|
for ($idx = 0; $idx < strlen($input_string); $idx++)
|
2020-10-03 07:26:14 +00:00
|
|
|
{
|
2022-07-23 20:41:10 -06:00
|
|
|
array_push($result, $input_string[$idx] ^ $key[$idx % $key_len]);
|
2020-10-03 07:26:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return join("", $result);
|
|
|
|
}
|