Skip to content

Instantly share code, notes, and snippets.

@syxc
Created July 10, 2015 10:06
Show Gist options
  • Save syxc/d081f54a238e2cd46a10 to your computer and use it in GitHub Desktop.
Save syxc/d081f54a238e2cd46a10 to your computer and use it in GitHub Desktop.
SimpleXOR for PHP
<?php
/**
* Created by PhpStorm.
* User: syxc
* Date: 15/7/10
* Time: 17:29
*/
class SimpleXOR
{
/*function simpleXor($data, $key)
{
$dataLen = strlen($data);
$keyLen = strlen($key);
for ($i = 0; $i < $dataLen; $i++) {
$data[$i] = $data[$i] ^ $key[$i % $keyLen];
}
return $data;
}
function fixedXOR($one, $two)
{
$output = '';
$limit = min(strlen($one), strlen($two));
for ($i = 0; $i < $limit; $i++) {
$output .= $one[$i] ^ $two [$i];
}
return $output;
}*/
private static function xor_encode($data, $key)
{
$dataLen = strlen($data);
$keyLen = strlen($key);
for ($i = 0; $i < $dataLen; $i++) {
$data[$i] = $data[$i] ^ $key[$i % $keyLen];
}
return $data;
}
public static function decode($data, $key)
{
return self::xor_encode(base64_decode($data), $key);
}
public static function encode($data, $key)
{
return base64_encode(self::xor_encode($data, $key));
}
}
<?php
include_once("SimpleXOR.php");
$base_data = "muid=0f074dc8e1f0547310e729032ac0730b&conv_time=1422263664&client_ip=10.11.12.13&sign=8a4d7f5323fd91b37430d639e6f7371b";
$encrypt_key = "test_encrypt_key";
$data = SimpleXOR::encode($base_data, $encrypt_key);
if (isset($data)) {
echo $data;
} else {
echo "sorry!";
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment