<?php
function xor_encrypt($in) {
$key = '<censored>';
$text = $in;
$outText = '';
// Iterate through each character
for($i=0;$i<strlen($text);$i++) {
$outText .= $text[$i] ^ $key[$i % strlen($key)];
}
return $outText;
}
// CALLED FIRST
function loadData($def) {
global $_COOKIE;
$mydata = $def;
// If data is in the cookie
if(array_key_exists("data", $_COOKIE)) {
// Get the data in rawform. base64 -> XORED -> json
$tempdata = json_decode(xor_encrypt(base64_decode($_COOKIE["data"])), true);
// outputs an assoc array with a key "showpassword" and "bgcolor"
if(is_array($tempdata) && array_key_exists("showpassword", $tempdata) && array_key_exists("bgcolor", $tempdata)) {
// If bg color looks like # + 6 of a-f then copy it over to mydata
if (preg_match('/^#(?:[a-fd]{6})$/i', $tempdata['bgcolor'])) {
$mydata['showpassword'] = $tempdata['showpassword'];
$mydata['bgcolor'] = $tempdata['bgcolor'];
}
}
}
return $mydata;
}
// CALLED NEXT
// go the other way
function saveData($d) {
setcookie("data", base64_encode(xor_encrypt(json_encode($d))));
}
function decryptcookie($data){
$base64_decoded = base64_decode($data);
$xored = xor_encrypt(base64_decode($data));
$json = json_decode(xor_encrypt(base64_decode($data)), true);
$out = json_decode(xor_encrypt(base64_decode($data)), true);
////////////////////////////////////////////////////////////////
echo "This is it:\n";
echo "HEXX: ". bin2hex($data)."\n";
echo "orig: ". $data."\n";
echo "base64 decoded: ".$base64_decoded."\n";
echo "xor: ".$xored."\n";
echo "json_decode: ".$json."\n";
echo "out: ". $out."\n\n";
////////////////////////////////////////////////////////////////
print_r($out);
print_r(array_values($out));
echo "Type of based64:". gettype($base64_decoded)."\n";
echo "Type of xored:". gettype($xored)."\n";
echo "Type of json:". gettype($json)."\n";
echo "Type of out:". gettype($out);
echo "\n****\n";
}
/////////////////////////////////////////////
$defaultdata = array( "showpassword"=>"no", "bgcolor"=>"#ffffff");
// put the cookie data in the new data
$data = loadData($defaultdata);
// if the color is a valid hexcode then add it to the cookie
if(array_key_exists("bgcolor",$_REQUEST)) {
if (preg_match('/^#(?:[a-fd]{6})$/i', $_REQUEST['bgcolor'])) {
$data['bgcolor'] = $_REQUEST['bgcolor'];
}
}
saveData($data);
$cook = "ClVLIh4ASCsCBE8lAxMacFMZV2hdVVotEhhUJQNVAmhSRwh6QUcIaAw%3D";
$cook_alt = "ClVLIh4ASCsCBE8lAxMacFMZV2hdVVotEhhUJQNVAmhSRwh6QUcIaAw=";
decryptcookie($cook_alt);
echo 'Hello World!';