From a6bd5655c043b1ea347c13d0e2f17a530a53ae57 Mon Sep 17 00:00:00 2001 From: danogentili Date: Thu, 14 Jul 2016 12:43:38 +0200 Subject: [PATCH] Translating... --- crypt.php | 102 ++++++++++++++++++++++++++-------------------------- mtproto.php | Bin 15963 -> 16177 bytes mtproto.py | 2 +- 3 files changed, 53 insertions(+), 51 deletions(-) diff --git a/crypt.php b/crypt.php index 80e7dd4b..1aa60dec 100644 --- a/crypt.php +++ b/crypt.php @@ -2,57 +2,59 @@ set_include_path(get_include_path() . PATH_SEPARATOR . dirname(__FILE__) . DIRECTORY_SEPARATOR . 'libpy2php'); require_once ('libpy2php.php'); require_once ('AES.class.php'); -function ige_encrypt($message, $key, $iv) { - return py2php_kwargs_function_call('_ige', [$message, $key, $iv], ["operation" => 'encrypt']); -} -function ige_decrypt($message, $key, $iv) { - return py2php_kwargs_function_call('_ige', [$message, $key, $iv], ["operation" => 'decrypt']); -} -/** - * Given a key, given an iv, and message - * do whatever operation asked in the operation field. - * Operation will be checked for: "decrypt" and "encrypt" strings. - * Returns the message encrypted/decrypted. - * message must be a multiple by 16 bytes (for division in 16 byte blocks) - * key must be 32 byte - * iv must be 32 byte (it's not internally used in AES 256 ECB, but it's - * needed for IGE) - */ -function _ige($message, $key, $iv, $operation = 'decrypt') { - $message = $bytes($message); - if ((count($key) != 32)) { - throw new $ValueError('key must be 32 bytes long (was ' . pyjslib_str(count($key)) . ' bytes)'); +class crypt { + function ige_encrypt($message, $key, $iv) { + return _ige($message, $key, $iv, 'encrypt'); } - if ((count($iv) != 32)) { - throw new $ValueError('iv must be 32 bytes long (was ' . pyjslib_str(count($iv)) . ' bytes)'); + function ige_decrypt($message, $key, $iv) { + return _ige($message, $key, $iv, 'decrypt'); } - $cipher = new AES($key); - $cipher = $cipher->encrypt($iv); - $blocksize = $cipher->block_size; - if (((count($message) % $blocksize) != 0)) { - throw new $ValueError('message must be a multiple of 16 bytes (try adding ' . pyjslib_str((16 - (count($message) % 16))) . ' bytes of padding)'); - } - $ivp = array_slice($iv, 0, $blocksize - 0); - $ivp2 = array_slice($iv, $blocksize, null); - $ciphered = $bytes(); - foreach (pyjslib_range(0, count($message), $blocksize) as $i) { - $indata = array_slice($message, $i, ($i + $blocksize) - $i); - if (($operation == 'decrypt')) { - $xored = new strxor($indata, $ivp2); - $decrypt_xored = $cipher->decrypt($xored); - $outdata = new strxor($decrypt_xored, $ivp); - $ivp = $indata; - $ivp2 = $outdata; - } else if (($operation == 'encrypt')) { - $xored = new strxor($indata, $ivp); - $encrypt_xored = $cipher->encrypt($xored); - $outdata = new strxor($encrypt_xored, $ivp2); - $ivp = $outdata; - $ivp2 = $indata; - } else { - throw new $ValueError('operation must be either \'decrypt\' or \'encrypt\''); + /** + * Given a key, given an iv, and message + * do whatever operation asked in the operation field. + * Operation will be checked for: "decrypt" and "encrypt" strings. + * Returns the message encrypted/decrypted. + * message must be a multiple by 16 bytes (for division in 16 byte blocks) + * key must be 32 byte + * iv must be 32 byte (it's not internally used in AES 256 ECB, but it's + * needed for IGE) + */ + function _ige($message, $key, $iv, $operation = 'decrypt') { + $message = str_split($message); + if ((len($key) != 32)) { + throw new Exception('key must be 32 bytes long (was ' . len($key) . ' bytes)'); } - $ciphered+= $outdata; + if ((len($iv) != 32)) { + throw new Exception('iv must be 32 bytes long (was ' . len($iv) . ' bytes)'); + } + $cipher = new AES($key); + $cipher = $cipher->encrypt($iv); + $blocksize = $cipher->block_size; + if ((len($message) % $blocksize) != 0) { + throw new Exception('message must be a multiple of 16 bytes (try adding ' . (16 - (count($message) % 16)) . ' bytes of padding)'); + } + $ivp = substr($iv, 0, $blocksize - 0); + $ivp2 = substr($iv, $blocksize, null); + $ciphered = null; + foreach (pyjslib_range(0, len($message), $blocksize) as $i) { + $indata = substr($message, $i, ($i + $blocksize) - $i); + if (($operation == 'decrypt')) { + $xored = strxor($indata, $ivp2); + $decrypt_xored = $cipher->decrypt($xored); + $outdata = strxor($decrypt_xored, $ivp); + $ivp = $indata; + $ivp2 = $outdata; + } else if (($operation == 'encrypt')) { + $xored = strxor($indata, $ivp); + $encrypt_xored = $cipher->encrypt($xored); + $outdata = strxor($encrypt_xored, $ivp2); + $ivp = $outdata; + $ivp2 = $indata; + } else { + throw new Exception('operation must be either \'decrypt\' or \'encrypt\''); + } + $ciphered .= $outdata; + } + return $ciphered; } - return $ciphered; -} +} \ No newline at end of file diff --git a/mtproto.php b/mtproto.php index 3f26db44d858060f02240f893e2917cfa0f85c1f..982e39642b9fb74fcf1d58678b2b8da03ea7ce86 100755 GIT binary patch delta 843 zcmaJ<&ui0Q7$*LL#E3e({sOz#Ei}o9{v(OaAM@R;mUd#1+f(kKZ=Fp(>LGF?r={#F|{Jsr@&w??N*s3MH-zC zEJy(fp#e1Zyn5=X;|IjF9EJ3Io^KOHHVj#VLzWq`^8q=D;C7fKTpCG0xy*jw*IP-> zMZLc`iA>OGxk>|AV^TL^yv6)51r1hJ6`!BW3IU1YfF{V{-Se-wQzHoeo_npO3X{PT z=VS*A+oXQi*u1%E+)f*!sCC@0W$@jFAEinB%6iYVwiWq$lVws|8mag>vwQ-U;R^oD z*moxSBpZEQKkAiLsDSj4grV6bZP!k|{#ommjLmEw-_CEKcuiXU3u9C^oH6zPLU=X1 z#@I!C24}==vdcn`d-5hbD5v|we0zk=mQX9$?M1|H+h$}Yj(VnTv(aN~sepc|h=W|c zIksuWk<(^+GX|0h6-aezHJ3#jiyoz}EtF8pXQoyVoB7uC#i#r|4u*xojhXFzRQ$g5 E2m8Ygh5!Hn delta 636 zcmdm3ce`eT5GQ+9erBGAO6FuuE~UxVoW?v#ss=_8N(!n9Dycb>t2igfYAV>;Di~-g zRC6gnL27bFz5-aE#$-hj@mgla|~ml2zeLSAW3&g27J^1Nl4 z#TrUkc_~?bN}AT2?{RHs6m<9TcXIRzj#erzDJo4aQHtHXkVldki|{f2a>mVqLK7Le z6N`!xE8~lEGLus$uMl21xk}`(Hij{}b_I#a*&6CLf$BO68U>YEK=n!SnRz7|lc$Pm zv1ux3DVS(ZJ}jD|tIhxgATOxorWO|`rl-bdrhs@D#!fC4OJ-5f(9oFtK~%&-TS28H zBePi7t{A8*Gd~ZgOH% diff --git a/mtproto.py b/mtproto.py index db69b89a..869b157d 100644 --- a/mtproto.py +++ b/mtproto.py @@ -40,7 +40,7 @@ def vis(bs): if not len(bs) % symbols_in_one_line == 0: print(str((i+1)*symbols_in_one_line)+" | "+" ".join(["%02X" % b for b in bs[(i+1)*symbols_in_one_line:]])+"\n") # for last line - +vis(b'ddd') class Session: """ Manages TCP Transport. encryption and message frames """ def __init__(self, ip, port, auth_key=None, server_salt=None):