Skip to content

Instantly share code, notes, and snippets.

@becker990
Created July 16, 2023 10:53
Show Gist options
  • Save becker990/18efade7c972647fffc37a8c926e79be to your computer and use it in GitHub Desktop.
Save becker990/18efade7c972647fffc37a8c926e79be to your computer and use it in GitHub Desktop.
arc4 encryption and decryption in php
<?php
// RC4 encryption function
function ARC4($data, $key) {
$s = array();
$keyLength = strlen($key);
if ($keyLength == 0) {
return "";
}
$dataLength = strlen($data);
$final = '';
for ($i = 0; $i < 256; $i++) {
$s[$i] = $i;
}
$j = 0;
for ($i = 0; $i < 256; $i++) {
$j = ($j + $s[$i] + ord($key[$i % $keyLength])) % 256;
$temp = $s[$i];
$s[$i] = $s[$j];
$s[$j] = $temp;
}
$i = 0;
$j = 0;
for ($k = 0; $k < $dataLength; $k++) {
$i = ($i + 1) % 256;
$j = ($j + $s[$i]) % 256;
$temp = $s[$i];
$s[$i] = $s[$j];
$s[$j] = $temp;
$final .= $data[$k] ^ chr($s[($s[$i] + $s[$j]) % 256]);
}
return $final;
}
// Example usage
$data = "Hello, World!";
$key = "SecretKeyaaa";
// Encrypt the data using RC4
$encryptedData = ARC4($data, $key);
// Encode the encrypted data using Base64
$encodedData = base64_encode($encryptedData);
echo "Original Data: " . $data . "\n";
echo "Encrypted Data: " . $encryptedData . "\n";
echo "Encoded Data: " . $encodedData . "\n";
$decodedString = base64_decode($encodedData);
$decryptedData = ARC4($decodedString, $key);
echo "Original Data: " . $data . "\n";
echo "Decrypted Data: " . $decryptedData . "\n";
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment