release encoder and decoder

This commit is contained in:
Pleshevskiy 2015-10-01 20:29:56 +04:00
parent d213f8346b
commit 16eb41e6c3
3 changed files with 251 additions and 0 deletions

View file

@ -1,2 +1,9 @@
# jjcrypto
jjcrypto for php
## HOW USE
### encoding
```php encode.php /path/to/file.js```
### decoding
```php decode.php /path/to/encoded/file.js.jj```

79
decode.php Normal file
View file

@ -0,0 +1,79 @@
<?php
// Decoder wrapper
$start = '$=~[];$={___:++$,$$$$:(![]+"")[$],__$:++$,$_$_:(![]+"")[$],_$_:++$,$_$$:({}+"")[$],$$_$:($[$]+"")[$],_$$:++$,$$$_:(!""+"")[$],$__:++$,$_$:++$,$$__:({}+"")[$],$$_:++$,$$$:++$,$___:++$,$__$:++$};$.$_=($.$_=$+"")[$.$_$]+($._$=$.$_[$.__$])+($.$$=($.$+"")[$.__$])+((!$)+"")[$._$$]+($.__=$.$_[$.$$_])+($.$=(!""+"")[$.__$])+($._=(!""+"")[$._$_])+$.$_[$.$_$]+$.__+$._$+$.$;$.$$=$.$+(!""+"")[$._$$]+$.__+$._+$.$+$.$$;$.$=($.___)[$.$_][$.$_];$.$($.$($.$$+"\""+';
$end = '+"\"")())();';
if (!isset($argv[1])) {
die("Decoder required encoded file");
}
// Read file
$r = file_get_contents($argv[1]);
// Replace decoder wrapper
$r = str_replace($start, "", $r);
$r = str_replace($end, "", $r);
// Template decoder
// from largest char length to smallest
$subst = array(
array('$.$___','8'),
array('$.$__$','9'),
array('$.$_$_','a'),
array('$.$_$$','b'),
array('$.$$__','c'),
array('$.$$_$','d'),
array('$.$$$$','f'),
array('$.$$$_','e'),
array('$.___','0'),
array('$.__$','1'),
array('$._$_','2'),
array('$._$$','3'),
array('$.$__','4'),
array('$.$_$','5'),
array('$.$$_','6'),
array('$.$$$','7'),
array('$.$_', 'c'),
array('$._$', 'o'),
array('$.$$', 'n'),
array('$.__', 't'),
array('$.$', 'r'),
array('$._', 'u'),
array('"\\\\"' , '\\'),
array('"+"', '%%%%'), // tmp
array('+', ""),
array('%%%%', "+")
);
// replace by template
foreach ($subst as $s) {
$r = str_replace($s[0], $s[1], $r);
}
echo "$r\n";
// replace my quotes
$r = preg_replace('/([^\\\\])"([^"]+)"/', '${1}${2}', $r);
// ASCII decode
preg_match_all('/\\\\(\d{3})/', $r, $matches);
for ($i = 0; $i < count($matches[1]); $i++) {
// convert octal to decimal
$octal = base_convert($matches[1][$i], 8, 10);
// get character and replace
$r = str_replace($matches[0][$i], chr($octal), $r);
}
// UTF-8 decode
preg_match_all('/(\\\\u\w{4})/', $r, $matches);
for ($i = 0; $i < count($matches[1]); $i++) {
// get character and replace
$r = str_replace($matches[0][$i], json_decode('"'.$matches[1][$i].'"'), $r);
}
// show decoded code
echo $r;
// if need save file
// file_put_contents(substr($argv[1], 0, -3), $r);

165
encode.php Normal file
View file

@ -0,0 +1,165 @@
<?php
if (!isset($argv[1])) {
die("Script required url to file");
}
// support method for generate template
function getCharKey($i, $maxPow) {
$resultKey = "";
for ($j = $maxPow; $j >= 0; $j--) {
// i love bitmasks
$resultKey .= $i & pow(2, $j) ? "$" : "_";
}
return $resultKey;
}
// check utf-8 character
function isUtf8($char) {
return mb_detect_encoding($char) == "UTF-8";
}
// str_split bad working for utf-8...
// it's best alternative
function mbStringToArray($string) {
$strlen = mb_strlen($string);
while ($strlen) {
$array[] = mb_substr($string, 0, 1, "UTF-8");
$string = mb_substr($string, 1, $strlen, "UTF-8");
$strlen = mb_strlen($string);
}
return $array;
}
// encode char
function encodeChar($char) {
// get template
global $substr;
// write prefix for char code
$result = '"\\\\"';
// if character is utf-8
if (isUtf8($char)) {
// get utf-8 code without quotes and slash
$code = substr(json_encode($char), 2, -1);
} else {
// convert decimal to octal
$code = base_convert(ord($char), 10, 8);
// fix code if digist < 3
$code = str_pad($code, 3, 0, STR_PAD_LEFT) . '';
}
// convert string to array
$digits = str_split($code);
for ($i = 0; $i < count($digits); $i++) {
// encode code
$result .= '+$.' . array_search($digits[$i], $substr);
}
return $result;
}
// its characters need as it
$ignoreChars = '!"#$%&()*+,-./:;<>=?@[]^_`{|}~\'';
// some characters for decoding
$substr = array(
'$_$_' => 'a',
'$_$$' => 'b',
'$$__' => 'c',
'$$_$' => 'd',
'$$$_' => 'e',
'$$$$' => 'f',
'_' => 'u',
);
// add to template digits
$i = -1;
while (++$i < 10) {
$key = getCharKey($i, $i <= 7 ? 2 : 3);
$substr[$key] = $i;
}
/**
* START ENCODING!
*/
$scriptText = file_get_contents($argv[1]);
if (!$scriptText) {
die("File not found\n");
}
// support variable for ignored charaters
$prevIgnored = false;
// for encoded text (result)
$encodedScript = "";
// split text
$chars = mbStringToArray($scriptText, 0, 1, "UTF-8");
// var_dump($chars);
// encoded each character
for ($i = 0; $i < count($chars); $i++) {
// get character
$ch = $chars[$i];
// if character is ignored
if (strpos($ignoreChars, $ch) != false) {
// if not first character
// and prev element not ignored
if ($i && !$prevIgnored) {
// open quotes
$encodedScript .= '+"';
}
// if character is quotes
if ($ch == '"') {
// need mirrors
$encodedScript .= "\\\\\\";
}
// else rewrite character
$encodedScript .= $ch;
// mark as ignored
$prevIgnored = true;
// get next character
continue;
} else if ($prevIgnored) {
// close quotes if character not ignored
// and if prev character is ignored
$encodedScript .= '"';
}
// add plus
if ($i) {
$encodedScript .= '+';
}
// search encoded character in template
$ech = array_search($ch, $substr);
// bug... if array_search is false then return "___"
if ($ech == "___" ? $ch == '0' : $ech != false) {
// add $. for decoder
$encodedScript .= '$.'.$ech;
} else {
// encode charater ASCII \xxx or UTF-8 \uxxxx
$encodedScript .= encodeChar($ch);
}
$prevIgnored = false;
}
if ($prevIgnored) {
$encodedScript .= '"';
}
// Decode wrapper
$start = '$=~[];$={___:++$,$$$$:(![]+"")[$],__$:++$,$_$_:(![]+"")[$],_$_:++$,$_$$:({}+"")[$],$$_$:($[$]+"")[$],_$$:++$,$$$_:(!""+"")[$],$__:++$,$_$:++$,$$__:({}+"")[$],$$_:++$,$$$:++$,$___:++$,$__$:++$};$.$_=($.$_=$+"")[$.$_$]+($._$=$.$_[$.__$])+($.$$=($.$+"")[$.__$])+((!$)+"")[$._$$]+($.__=$.$_[$.$$_])+($.$=(!""+"")[$.__$])+($._=(!""+"")[$._$_])+$.$_[$.$_$]+$.__+$._$+$.$;$.$$=$.$+(!""+"")[$._$$]+$.__+$._+$.$+$.$$;$.$=($.___)[$.$_][$.$_];$.$($.$($.$$+"\""+';
$end = '+"\"")())();';
// wrap encoded script
$encodedScript = $start.$encodedScript.$end;
// write to file with ext .jj
file_put_contents($argv[1].'.jj', $encodedScript);