This commit is contained in:
Daniil Gentili 2016-07-15 13:18:17 +02:00
commit 9e0ab1f9c5

View File

@ -1,4 +1,5 @@
<?php <?php
// by https://github.com/mgp25 // by https://github.com/mgp25
class TelegramEncryption class TelegramEncryption
{ {
@ -16,9 +17,9 @@ class TelegramEncryption
$this->plainText = $plainText; $this->plainText = $plainText;
$this->debug = $debug; $this->debug = $debug;
} }
public function IGE256Decrypt() public function IGE256Decrypt()
{ {
$key = $this->key; $key = $this->key;
$message = $this->cipherText; $message = $this->cipherText;
$blockSize = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_ECB); $blockSize = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_ECB);
@ -28,28 +29,28 @@ $yPrev = substr($this->iv, $blockSize, strlen($this->iv));
$decrypted = ''; $decrypted = '';
for ($i=0; $i < strlen($message); $i += $blockSize) for ($i = 0; $i < strlen($message); $i += $blockSize) {
{
$x = substr($message, $i, $blockSize); $x = substr($message, $i, $blockSize);
$this->debugLog("x: " . _c($x) . "\n"); $this->debugLog('x: '._c($x)."\n");
$yXOR = $this->exor($x, $yPrev); $yXOR = $this->exor($x, $yPrev);
$this->debugLog("yPrev: " . _c($yPrev) . "\n"); $this->debugLog('yPrev: '._c($yPrev)."\n");
$this->debugLog("yXOR: " . _c($yXOR) . "\n"); $this->debugLog('yXOR: '._c($yXOR)."\n");
$yFinal = mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $key, $yXOR, MCRYPT_MODE_ECB); $yFinal = mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $key, $yXOR, MCRYPT_MODE_ECB);
$yFinal = str_pad($yFinal, strlen($xPrev), "\x00"); $yFinal = str_pad($yFinal, strlen($xPrev), "\x00");
$this->debugLog("yFinal: " . _c($yFinal) . "\n"); $this->debugLog('yFinal: '._c($yFinal)."\n");
$y = $this->exor($yFinal, $xPrev); $y = $this->exor($yFinal, $xPrev);
$this->debugLog("xPrev: " . _c($xPrev) . "\n"); $this->debugLog('xPrev: '._c($xPrev)."\n");
$this->debugLog("y: " . _c($y) . "\n"); $this->debugLog('y: '._c($y)."\n");
$xPrev = $x; $xPrev = $x;
$yPrev = $y; $yPrev = $y;
$decrypted .= $y; $decrypted .= $y;
$this->debugLog("Currently Decrypted: "._c($decrypted)."\n\n"); $this->debugLog('Currently Decrypted: '._c($decrypted)."\n\n");
} }
return $decrypted; return $decrypted;
} }
@ -64,36 +65,36 @@ $yPrev = substr($this->iv, 0, $blockSize);
$encrypted = ''; $encrypted = '';
for ($i=0; $i < strlen($message); $i += $blockSize) for ($i = 0; $i < strlen($message); $i += $blockSize) {
{
$x = substr($message, $i, $blockSize); $x = substr($message, $i, $blockSize);
$this->debugLog("x: " . _c($x) . "\n"); $this->debugLog('x: '._c($x)."\n");
$yXOR = $this->exor($x, $yPrev); $yXOR = $this->exor($x, $yPrev);
$this->debugLog("yPrev: " . _c($yPrev) . "\n"); $this->debugLog('yPrev: '._c($yPrev)."\n");
$this->debugLog("yXOR: " . _c($yXOR) . "\n"); $this->debugLog('yXOR: '._c($yXOR)."\n");
$yFinal = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key, $yXOR, MCRYPT_MODE_ECB); $yFinal = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key, $yXOR, MCRYPT_MODE_ECB);
$yFinal = str_pad($yFinal, strlen($xPrev), "\x00"); $yFinal = str_pad($yFinal, strlen($xPrev), "\x00");
$this->debugLog("yFinal: " . _c($yFinal) . "\n"); $this->debugLog('yFinal: '._c($yFinal)."\n");
$y = $this->exor($yFinal, $xPrev); $y = $this->exor($yFinal, $xPrev);
$this->debugLog("xPrev: " . _c($xPrev) . "\n"); $this->debugLog('xPrev: '._c($xPrev)."\n");
$this->debugLog("y: " . _c($y) . "\n"); $this->debugLog('y: '._c($y)."\n");
$xPrev = $x; $xPrev = $x;
$yPrev = $y; $yPrev = $y;
$encrypted .= $y; $encrypted .= $y;
$this->debugLog("Currently encrypted: "._c($encrypted)."\n\n"); $this->debugLog('Currently encrypted: '._c($encrypted)."\n\n");
} }
return $encrypted; return $encrypted;
} }
public function debugLog($message) public function debugLog($message)
{ {
if ($this->debug) if ($this->debug) {
echo $message; echo $message;
} }
}
public function exor($array1, $array2) public function exor($array1, $array2)
{ {
@ -103,16 +104,16 @@ $array1 = str_pad($array1, $len, "\x00");
$array2 = str_pad($array2, $len, "\x00"); $array2 = str_pad($array2, $len, "\x00");
$res = ''; $res = '';
for ($i=0; $i < $len; $i++) for ($i = 0; $i < $len; $i++) {
{
$res .= $array1[$i] ^ $array2[$i]; $res .= $array1[$i] ^ $array2[$i];
} }
return $res; return $res;
} }
function _c($binary) { public function _c($binary)
return sprintf("[%s]", chunk_split(bin2hex($binary), 4," ") {
return sprintf('[%s]', chunk_split(bin2hex($binary), 4, ' ')
); );
} }
} }