Merge pull request #18 from woodongwong/php

Update XOREncryption.php
This commit is contained in:
Kyle Banks 2020-08-17 08:30:32 +01:00 committed by GitHub
commit 2e36ac5081
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,13 +1,18 @@
<?php <?php
function encryptDecrypt($input) { function encryptDecrypt($input) {
$key = ['K', 'C', 'Q']; //Can be any chars, and any size array $key = 'KCQ';
$output = ''; $inputLen = strlen($input);
$keyLen = strlen($key);
for ($i = 0; $i < strlen($input); $i++) { if ($inputLen <= $keyLen) {
$output .= $input[$i] ^ $key[$i % count($key)]; return $input ^ $key;
} }
return $output;
for ($i = 0; $i < $inputLen; ++$i) {
$input[$i] = $input[$i] ^ $key[$i % $keyLen];
}
return $input;
} }
$encrypted = encryptDecrypt('kylewbanks.com'); $encrypted = encryptDecrypt('kylewbanks.com');
@ -15,4 +20,3 @@ echo "Encrypted:" . $encrypted . "\n";
$decrypted = encryptDecrypt($encrypted); $decrypted = encryptDecrypt($encrypted);
echo "Decrypted:" . $decrypted . "\n"; echo "Decrypted:" . $decrypted . "\n";