Skip to content

Instantly share code, notes, and snippets.

@wrossmann
Created May 1, 2015 21:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wrossmann/e0452d593293b45ec954 to your computer and use it in GitHub Desktop.
Save wrossmann/e0452d593293b45ec954 to your computer and use it in GitHub Desktop.
Reverse the lame home-rolled 'encryption' someone posted
<?php
// supplied encoding function
function stupidEncode($input, $key) {
$k = $key;
$r = $input;
$l = strlen($r);
$e = "";
for($i=0; $i < $l; $i++){
$s = dechex(ord(substr($key, $i%strlen($key), 1)));
$c = dechex(ord(substr($r, $i, 1)));
$x = bin2hex(pack('H*',$s) ^ pack('H*',$c));
$e = $e."&#x".$x.";";
}
return $e;
}
// derived reversal
function stupidDecode($input, $key) {
$parts = str_split($input, 6);
$parts = array_map(function($a){return substr($a,3,2);}, $parts);
$parts = array_map('hex2bin', $parts);
$encstr = implode('', $parts);
$out = '';
for($i=0, $l=strlen($encstr); $i<$l; $i++) {
$s = dechex(ord(substr($key, $i%strlen($key), 1)));
$chr = $encstr[$i] ^ pack('H*',$s);
$out .= $chr;
}
return $out;
}
// test
$input = 'Hello world!';
$key = 'foobar';
printf("Raw: %s\nKey: %s\n", $input, $key);
$en = stupidEncode($input, $key);
printf("Encoded: %s\n", $en);
$de = stupidDecode($en, $key);
printf("Decoded: %s\n", $de);
/* output:
Raw: Hello world!
Key: foobar
Encoded: &#x2e;&#x0a;&#x03;&#x0e;&#x0e;&#x52;&#x11;&#x00;&#x1d;&#x0e;&#x05;&#x53;
Decoded: Hello world!
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment