Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class XLog
- {
- private $HEX_CHAR = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'];
- public function __construct()
- {
- $this->calculate("c223979477ffc516", 56);
- }
- public function decode($decode)
- {
- $decode = $this->bytearray($decode);
- $resultLen = 0;
- $last = '78468ec4';
- $s = substr($this->bytesToHexFun($decode) , 2);
- $stringBuffer = '';
- $strList = [];
- for ($i = 0;$i < strlen($s) / 16 - 1;$i++)
- {
- $input = substr($s, $i * 16, 16);
- $strList[] = $input;
- }
- $last = substr($s, strlen($s) - 8);
- $strList[] = $last;
- $times = $this->getHandleCount($last);
- for ($i = 0;$i < count($strList) - 1;$i++)
- {
- $calculate = $this->calculate($strList[$i], $times);
- if (0 == $i)
- {
- $tmp = $last . '744c0000';
- for ($j = 0;$j < 8;$j++)
- {
- $xor = $this->doxor(substr($calculate, ($j * 2) , 2) , substr($tmp, $j * 2, 2));
- if (strlen($xor) < 2)
- {
- $xor = '0' . $xor;
- }
- $stringBuffer .= $xor;
- }
- }
- if ($i >= 1)
- {
- $tmp = $strList[$i - 1];
- for ($j = 0;$j < 8;$j++)
- {
- $xor = $this->doxor(substr($calculate, ($j * 2) , 2) , substr($tmp, $j * 2, 2));
- if (strlen($xor) < 2)
- {
- $xor = '0' . $xor;
- }
- $stringBuffer .= $xor;
- }
- }
- }
- $string = $stringBuffer;
- $bytes = $this->hexToByteArray($string);
- $count = $bytes[0] & 7;
- $resultLen = count($decode) - 13 - $count;
- $count = $count % 4;
- if (0 == $count)
- {
- $count = 4;
- }
- $result = array_fill(0, $resultLen, 0);
- $result = $this->arraycopy($bytes, $count, $result, 0, $resultLen);
- $result = $this->changeToLongArray($result);
- return $this->bytearray_decode($result);
- }
- public function decodeHex($hex)
- {
- $decode = hex2bin($hex);
- return $this->decode($decode);
- }
- public function encode($input)
- {
- $inputStart = $this->changeLongArrayTobytes($this->bytearray($input));
- $sourceLen = count($inputStart);
- $fillCount = 4 - $sourceLen % 4;
- $fillNum = 8 - $sourceLen % 8;
- $isAdd = 4;
- if ($sourceLen % 8 >= 4)
- {
- $isAdd = 0;
- }
- if (8 == $fillNum)
- {
- $isAdd = - 4;
- $fillNum = 0;
- }
- $bytes = array_fill(0, $sourceLen + $fillNum + 8, 0);
- $eorByte = [0x78, 0x46, 0x8e, 0xc4, 0x74, 0x4c, 0x00, 0x00];
- $bytes[0] = 0x80 | $fillNum;
- $bytes[1] = 0x30;
- $bytes[2] = 0x22;
- $bytes[3] = 0x24;
- $result = '02';
- $bytes = $this->arraycopy($inputStart, 0, $bytes, $fillCount, count($inputStart));
- $bytes = $this->changeLongArrayTobytes($bytes);
- for ($i = 0;$i < count($bytes) / 8;$i++)
- {
- $sb = '';
- for ($j = 0;$j < 8;$j++)
- {
- $r1 = $bytes[$j + 8 * $i];
- $r2 = $eorByte[$j];
- if ($r2 < 0)
- {
- $r2 = $r2 + 256;
- }
- if ($r1 < 0)
- {
- $r1 = $r1 + 256;
- }
- $tmp = $r1 ^ $r2;
- if (0 == $tmp)
- {
- $sb .= '00';
- }
- else
- {
- $str = dechex($tmp);
- if (strlen($str) < 2)
- {
- $sb .= '0';
- }
- $sb .= $str;
- }
- }
- $times = $this->getHandleCount('78468ec4');
- $times = 56;
- $s = $this->calculateRev($sb, $times);
- for ($z = 0;$z < 8;$z++)
- {
- $substring = substr($s, (2 * $z) , (2 * $z + 2) - $this->makeSafe(2 * $z));
- $eorByte[$z] = $this->makeSafe($this->parseLong($substring, 16));
- }
- $result .= $s;
- }
- $result .= '78468ec4';
- $bytes1 = $this->hexToByteArray($result);
- return $this->bytearray_decode($bytes1);
- }
- private function arraycopy($src, $startPos = 0, $dest = [], $destPos = 0, $length = false)
- {
- $final = [];
- if (false == $length)
- {
- $length = count($src);
- }
- $final = array_merge($final, array_slice($dest, 0, $destPos));
- $final = array_merge($final, array_slice($src, $startPos, $length));
- $final = array_merge($final, array_slice($dest, $length + $destPos));
- return $final;
- }
- private function bigInt2Hex($num)
- {
- $d = $num;
- $result = "";
- while ("0" != $d)
- {
- $d = bcdiv($d, 16, 10);
- $exp = explode('.', $d);
- $d = $exp[0];
- $reminder = $exp[1];
- $reminder = floatval("0." . $reminder);
- $r = dechex($reminder * 16);
- $result .= $r;
- }
- $result = strrev($result);
- return $result;
- }
- private function bytearray($string)
- {
- return array_values(unpack('C*', $string));
- }
- private function bytearray_decode($byteArray)
- {
- $chars = array_map('chr', $byteArray);
- return join($chars);
- }
- private function bytesToHexFun($bytes)
- {
- $buf = array_fill(0, count($bytes) * 2, ' ');
- $a = 0;
- $index = 0;
- foreach ($bytes as $b)
- {
- if ($b < 0)
- {
- $a = 256 + $b;
- }
- else
- {
- $a = $b;
- }
- $buf[$index++] = $this->HEX_CHAR[(int)($a / 16) ];
- $buf[$index++] = $this->HEX_CHAR[$a % 16];
- }
- return join('', $buf);
- }
- private function calculate($input, $times)
- {
- if (strlen($input) != 16)
- {
- return '';
- }
- $s36 = 0;
- $s40 = 0;
- $s108 = - 1073747680;
- $s136 = $this->makeSafe(-1640531527 * $times);
- $str1 = substr($input, 0, 8 - 0);
- $str2 = substr($input, 8, 16 - 8);
- $s140 = $this->makeSafe($this->parseLong(substr($input, 0, 8 - 0) , 16));
- $s144 = $this->makeSafe($this->parseLong(substr($input, 8, 16 - 8) , 16));
- $r0 = 1180082309;
- $r2 = 1180082309;
- $r4 = 1180082309;
- $r6 = - 1436101968;
- $r5 = $s108;
- $r12 = 0;
- for ($i = 0;$i < $times;$i++)
- {
- $r0 = $s140;
- $r2 = $s140;
- $r4 = $s140;
- $r6 = $s136;
- $r5 = $s108;
- $string = $this->stablizieBinary(decbin($r6 >> 11));
- if (strlen($string) < 3)
- {
- $string = '0';
- }
- else
- {
- $string = substr($string, strlen($string) - 2);
- }
- $r6 = intval($string, 2);
- $r0 = $this->makeSafe(($this->unsignedRightShift($r2, 5) ^ $this->makeSafe($r0 << 4)) + $r4);
- $r5 = $this->getShifting($this->makeSafe($r5 + ($r6 << 2)));
- $r6 = 1640531527;
- $r2 = $this->makeSafe($s136 + $r5);
- $r5 = $s136;
- $r0 = $this->makeSafe($r0 ^ $r2);
- $r2 = $s108;
- $r6 = $this->makeSafe($r6 + $r5);
- $r4 = $this->makeSafe($s144 - $r0);
- $r5 = $this->makeSafe($r6 & 3);
- $r0 = $this->makeSafe($r4 << 4);
- $r2 = $this->getShifting($r2 + ($r5 << 2));
- $r0 = $this->makeSafe(($r0 ^ $this->unsignedRightShift($r4, 5)) + $r4);
- $r2 = $this->makeSafe($r2 + $r6);
- $r01 = $r0;
- $r0 = $this->makeSafe($r0 ^ $r2);
- $s140 = $this->makeSafe($s140 - $r0);
- $s136 = $r6;
- $s144 = $r4;
- }
- $str140 = $this->stablizeHex((strlen($this->toHexString($s140)) == 7 ? '0' : '') . $this->toHexString($s140));
- $str144 = $this->stablizeHex((strlen($this->toHexString($s144)) == 7 ? '0' : '') . $this->toHexString($s144));
- if (strlen($str140) < 8)
- {
- $count = 8 - strlen($str140);
- for ($i = 0;$i < $count;$i++)
- {
- $str140 = '0' . $str140;
- }
- }
- if (strlen($str144) < 8)
- {
- $count = 8 - strlen($str144);
- for ($i = 0;$i < $count;$i++)
- {
- $str144 = '0' . $str144;
- }
- }
- return $str140 . $str144;
- }
- private function calculateRev($input, $times)
- {
- $r12 = 0;
- $s108 = - 1073747680;
- $s136 = 0;
- $s140 = $this->makeSafe($this->parseLong(substr($input, 0, 8 - 0) , 16));
- $s144 = $this->makeSafe($this->parseLong(substr($input, 8, 16 - 8) , 16));
- for ($i = 0;$i < $times;$i++)
- {
- $r2 = $s108;
- $r6 = $s136;
- $r4 = $s144;
- $r5 = $r6 & 3;
- $r0 = $this->makeSafe($r4 << 4);
- $r2 = $this->getShifting($r2 + ($r5 << 2));
- $r0 = $this->makeSafe(($r0 ^ $this->unsignedRightShift($r4, 5)) + $r4);
- $r2 = $this->makeSafe($r2 + $r6);
- $r0 = $this->makeSafe($r0 ^ $r2);
- $s140 = $this->makeSafe($s140 + $r0);
- $s136 = $this->makeSafe($s136 - 0x61c88647);
- $r5 = $s108;
- $r4 = $s140;
- $r2 = $s140;
- $r0 = $s140;
- $r6 = $s136;
- $string = $this->stablizieBinary(decbin($r6 >> 11));
- if (strlen($string) < 3)
- {
- $string = '0';
- }
- else
- {
- $string = substr($string, strlen($string) - 2);
- }
- $r6 = intval($string, 2);
- $r0 = $this->makeSafe($this->makeSafe($this->unsignedRightShift($r2, 5) ^ $this->makeSafe($r0 << 4)) + $r4);
- $r5 = $this->getShifting($r5 + ($r6 << 2));
- $r2 = $this->makeSafe($s136 + $r5);
- $r0 = $this->makeSafe($r0 ^ $r2);
- $s144 = $this->makeSafe($s144 + $r0);
- }
- $str140 = $this->stablizeHex((strlen($this->toHexString($s140)) == 7 ? '0' : '') . $this->toHexString($s140));
- $str144 = $this->stablizeHex((strlen($this->toHexString($s144)) == 7 ? '0' : '') . $this->toHexString($s144));
- if (strlen($str140) < 8)
- {
- $count = 8 - strlen($str140);
- for ($i = 0;$i < $count;$i++)
- {
- $str140 = '0' . $str140;
- }
- }
- if (strlen($str144) < 8)
- {
- $count = 8 - strlen($str144);
- for ($i = 0;$i < $count;$i++)
- {
- $str144 = '0' . $str144;
- }
- }
- return $str140 . $str144;
- }
- private function changeLongArrayTobytes($arrays)
- {
- $result = [];
- for ($i = 0;$i < count($arrays);$i++)
- {
- if ($arrays[$i] > 127)
- {
- $result[$i] = $arrays[$i] - 256;
- }
- else
- {
- $result[$i] = $arrays[$i];
- }
- }
- return $result;
- }
- private function changeToLongArray($bytes)
- {
- $result = [];
- for ($i = 0;$i < count($bytes);$i++)
- {
- if ($bytes[$i] < 0)
- {
- $result[$i] = $bytes[$i] + 256;
- }
- else
- {
- $result[$i] = $bytes[$i];
- }
- }
- return $result;
- }
- private function doxor($strHex_X, $strHex_Y)
- {
- $anotherBinary = decbin(hexdec($strHex_X));
- $thisBinary = decbin(hexdec($strHex_Y));
- $result = '';
- if (strlen($anotherBinary) != 8)
- {
- for ($i = strlen($anotherBinary);$i < 8;$i++)
- {
- $anotherBinary = '0' . $anotherBinary;
- }
- }
- if (strlen($thisBinary) != 8)
- {
- for ($i = strlen($thisBinary);$i < 8;$i++)
- {
- $thisBinary = '0' . $thisBinary;
- }
- }
- for ($i = 0;$i < strlen($anotherBinary);$i++)
- {
- if ($thisBinary[$i] == $anotherBinary[$i])
- {
- $result .= '0';
- }
- else
- {
- $result .= '1';
- }
- }
- return dechex(intval($result, 2));
- }
- private function getHandleCount($hex)
- {
- $reverse = $this->reverse($hex);
- $r1 = $this->parseLong($reverse, 16);
- $r0 = 0xCCCCCCCD;
- $r2 = $this->getUmullHigh($r1, $r0);
- $s58 = $r0;
- $r2 = $r2 >> 2;
- $r2 = $r2 + ($r2 << 2);
- $r1 = $r1 - $r2;
- $r2 = 32;
- $r1 = $r2 + ($r1 << 3);
- return $r1;
- }
- private function getShifting($point)
- {
- switch ($point)
- {
- case -1073747680:
- return 1198522846;
- case -1073747676:
- return -87105875;
- case -1073747672:
- return 808464432;
- case -1073747668:
- return 959787575;
- }
- return 0;
- }
- // private function getUmullHigh($r0, $r2)
- // {
- // $n1 = $this->parseLong($this->stablizeHex(dechex($r0)), 16);
- // $n2 = $this->parseLong($this->stablizeHex(dechex($r2)), 16);
- // $result = $this->makeSafe($n1 * $n2);
- // $string = dechex($result);
- // $string = substr($string, 0, strlen($string) - 8 - 0);
- // return $this->makeSafe(hexdec($string));
- // }
- private function getUmullHigh($r0, $r2)
- {
- $r0_us = $this->signed2unsigned($r0);
- $r2_us = $this->signed2unsigned($r2);
- $mul = bcmul($r0_us, $r2_us);
- $res = substr($this->bigInt2Hex($mul) , 0, 8);
- return $this->parseLong($res, 16);
- }
- private function hexToByteArray($inHex)
- {
- $hexlen = strlen($inHex);
- if ($hexlen % 2 == 1)
- {
- $hexlen++;
- $result = array_fill(0, ((int)($hexlen / 2)) , NULL);
- $inHex = '0' . $inHex;
- }
- else
- {
- $result = array_fill(0, ((int)($hexlen / 2)) , NULL);
- }
- $j = 0;
- for ($i = 0;$i < $hexlen;$i += 2)
- {
- $result[$j] = hexdec(substr($inHex, $i, $i + 2 - $i));
- $j++;
- }
- return $this->changeLongArrayTobytes($result);
- }
- private function makeSafe($num)
- {
- $i = pack('i', $num);
- $i = unpack('i', $i) [1];
- return $i;
- }
- private function parseLong($str, $radix = 10)
- {
- return (int)base_convert($str, $radix, 10);
- }
- private function reverse($hex)
- {
- return substr($hex, 6, 8 - 6) . substr($hex, 4, 6 - 4) . substr($hex, 2, 4 - 2) . substr($hex, 0, 2 - 0);
- }
- // converts signed integer to unsigned
- private function signed2unsigned($num)
- {
- $res = pack('i', $num);
- $res = unpack('I', $res) [1];
- return $res;
- }
- private function stablizeHex($val)
- {
- $final = $val;
- if (strlen($val) == 16)
- {
- $final = substr($val, 8);
- }
- return $final;
- }
- private function stablizieBinary($val)
- {
- $final = $val;
- if (strlen($val) == 64)
- {
- $final = substr($val, 32);
- }
- return $final;
- }
- private function toHexString($hex)
- {
- return dechex($hex);
- }
- private function unsignedRightShift($a, $b)
- {
- if ($b >= 32 || $b < - 32)
- {
- $m = (int)($b / 32);
- $b = $b - ($m * 32);
- }
- if ($b < 0)
- {
- $b = 32 + $b;
- }
- if (0 == $b)
- {
- return (($a >> 1) & 0x7fffffff) * 2 + (($a >> $b) & 1);
- }
- if ($a < 0)
- {
- $a = ($a >> 1);
- $a &= 0x7fffffff;
- $a |= 0x40000000;
- $a = ($a >> ($b - 1));
- }
- else
- {
- $a = ($a >> $b);
- }
- return $a;
- }
- }
- $xlog = new Xlog;
- //$test = $xlog->encode("i love cock");
- /*$filename = "xlog.bin";
- $file = fopen( $filename, "r" );
- $filesize = filesize( $filename );
- $filetext = fread( $file, $filesize );
- print $xlog->decode($filetext); */
- $filename = "xlog.hex";
- //print hex2bin("02d5195d516a1481464b25dad88517d35ca207b3db88bb5ed09eb08871df15cab02a2b799c39c10af5296cf79fe43a7baadcb2316ab8697b3c34beeced4169cb2c0e840c10678f0a05104c11f68bb75827cd8904bf0210470d9c023dba60de2fa04a893269a68e241d69563b40f51110700c98a01f8c35645ef3b171e68192432e5228b3e34bebebb836a1204edc54db0b0603d7de80424e64d8ead79125ee5ff0bd185dd234169e21b106ee8e4e6f83adcd00c9075cd4192736a272e6e46fca1d82da7d004c03e742f2cdae0aae9de86b1a41df1cdd59f4c39ff94e53b57fbadfc0eb98c946b57907f2bb1df693200e9c70b8ad758485afd0beb4e8c0a00f0d1d515820ddd84935cee045ceed6675a626754f098cb30998e7cf20e5ecb4515de06353e60470a59583e7c01ff8187c3c18bc9e76da3ab2d2d0e7970329a624c2fad35748816fe719118060f8cc0dbeb2140ce2888f862f8db8299e2e4c43e5218f1fc7d84a67aef1eba7fcaed898658dbc0fde3ca2e3bdc79803f2b260af1f218e0159a16d990bde9d56fc7df144d6f1e8ddbfb1c219186795e7e220f579c95ecc73490b7264f0be68af014cc4629829057c833f1e537fba25cc21efba73bb3b46b6a1ff01b4b3d20ed88e9b843a299afdab469ae2ddf0c24e84a54a05cdb7b21be6dbd4e6a5524d518f32ba6aca857d2da82994dfc9a9237ac71d0e97a5e60d2242bc659fb0c24692a74ca529f4c893582c5a8513fc955e60172b62afad893c0f21baa2891be780fd4f0f5eee3e517595a7846f59ddd098a801b905327e67b1d1d127fc16386a748057200c938f47dcc15958a7e96e66726f21038dfde4488035b382d5c9d1fc75075cb8bfc49d143df72b0cd032e2a0ca2640c279173dfaee51575648458ec7f35778545d0ba9bfb72e220fc6d7ac217af96028843260b11ad889a5a9f468a70fefb0057e10e1e8090ef7099d5d0230a10265109c24388abfc13bce4a98bd3a2ea945cb37fd3a4dcb10fce16ee3f062db95ecd4f287bfef323d95a2c2600e42857e7b8fc3985462c12b5e22e1278d6fd41d4d6dba76f6151d1462cc6f78d263d3aa5f4eca7adffdcfb3c043e088aab76a79ae98c7d7aef348fb8c6a5ce48d5fc6df663a10f12a31604d8146b4c41e9511cadced601513a3d8a067c87cde8267fc1a01baf25064ed809c1792f8de8d9d34dfc56cb1677524852696066b64a76c9fc2696ca18070b0d4e2c24c6d6f32fda6eb5a6588e4ee8bff1ee2e6dc415245784741306caa9f0be96e07b53205c74e79125c06454634ae21f0c8969b6af05bf1a095900a45484a74da8e3c4da45864d71f668943cada7fa6c2185056dbbe39bf3cedec379dd5aa0ca28e059e22dba988f165037ab26aefd87bf3272513cbafeb10e18365335060358f8612d9aeddc6fb0c901fc8823b89ef7a67aabedaebf085615055738efee61c6bb9770903779da9a01e3924e6d52f9806b35f91bf20321c476f666b79e1a7c871952b94b2e5f4802b4be8f201557f3e499b9a7c60d6f3c059f9b0f760dcef3b63836593ecfa6599cfdb0b8a8a1266f25926ceaa3f91bb3c18a1ecbc835bdffc7b677c7b450890b20a22c41da4563de2cc8a6475eb6a10ac23fea2f63b32a10fce426a111055ec496ecfa18f82c28a06ecd5483d8270a42c1c429a7d8081fa76b4b8e3a10875caf6d98c189b826d6b7835af4f20c179896c32d38098ab16ef30f5ea78221572216918dceb412f088819063b88759604d99422dda103e9f6f899f0428acf8b7ac0afe729e536f09989b2b5a2b611d415c2dd3283a65feb0f285256502ee8663ecc28c069fa6b87ce92cf0bdfb185a85b6110854d2abc6003f1905436b306126055cb4c4c43ef4608e460c2c25114fdb1b8cec2a7778502e8ce0265a22abd03aad7950b685576ddf559520c54f0c452a21e658df00846fa5c41e2bebf437ba0a041f8fffdf41503760bcc33ced2e63fef922072a2a0f2d3218c51943b9d4c15f3318301b0fbf01637d93782f5d7d01e2a118302e95f62bb32c583ef90b634f38c49bac04ae6c82bd3e40f3cf89d6707d9943e510b2f8b4e25498bf335809b0d3caa4c54bb7ac7f34bf1a0b6464b4ef9882b191384bb42ce983d361da62419a15a8d8f5a3d0485d3215b4357a18c2b3e9112090faeb7be8b51ef2ed25df2cb40afc0f717d3766f4b25332fb9d6b0042386b8c7269ac95e7f68d05332106cc4f734d27e4091785e94e323430c260b08d897de94a8822b6fdee16700f1eb558522c021dedc711eb9cd7488d4e6fbbe468a0cc844ca16d37c4d09571cb6ea6ab240fa664fa190856e34f3ec633fddcfc8d38738bb80c79e699962cff0a2c9465a05f166443653006c7cc4bba27b701f32f7e54303485761c271eb61c551025ebf25634ae907b40029e23d018c23692f92ac8f91236f9b87f7c5671bd45529a3a91827c991d439fcf7662cfde63cb6bd3b318afc940efe5e148978afd21e6c3b493537fb2604612fb6f8693d2a8f413bbd6a36a36ed77a3b9c7f1a99a017283e67c86e3bcf9058ff3f8ed95e7ba95b1cd5d8a01e7c36a827f4dd07f27333f329c5e2ffabeb8ab028c6e04ac7e15ff3423fb473359d423841eb51cef59876f73dd46e76ca09f13f359a85b11958c5a185a7304c741b1162038f8b");
- $file = fopen( $filename, "r" );
- $filesize = filesize( $filename );
- $filetext = fread( $file, $filesize );
- $decodedhex = hex2bin($filetext);
- print $xlog->decode($decodedhex);
- ?>
Add Comment
Please, Sign In to add comment