Translating...

This commit is contained in:
danogentili 2016-07-14 12:43:38 +02:00
parent 7cf87ea1e9
commit a6bd5655c0
3 changed files with 53 additions and 51 deletions

View File

@ -2,13 +2,14 @@
set_include_path(get_include_path() . PATH_SEPARATOR . dirname(__FILE__) . DIRECTORY_SEPARATOR . 'libpy2php'); set_include_path(get_include_path() . PATH_SEPARATOR . dirname(__FILE__) . DIRECTORY_SEPARATOR . 'libpy2php');
require_once ('libpy2php.php'); require_once ('libpy2php.php');
require_once ('AES.class.php'); require_once ('AES.class.php');
function ige_encrypt($message, $key, $iv) { class crypt {
return py2php_kwargs_function_call('_ige', [$message, $key, $iv], ["operation" => 'encrypt']); function ige_encrypt($message, $key, $iv) {
} return _ige($message, $key, $iv, 'encrypt');
function ige_decrypt($message, $key, $iv) { }
return py2php_kwargs_function_call('_ige', [$message, $key, $iv], ["operation" => 'decrypt']); function ige_decrypt($message, $key, $iv) {
} return _ige($message, $key, $iv, 'decrypt');
/** }
/**
* Given a key, given an iv, and message * Given a key, given an iv, and message
* do whatever operation asked in the operation field. * do whatever operation asked in the operation field.
* Operation will be checked for: "decrypt" and "encrypt" strings. * Operation will be checked for: "decrypt" and "encrypt" strings.
@ -18,41 +19,42 @@ function ige_decrypt($message, $key, $iv) {
* iv must be 32 byte (it's not internally used in AES 256 ECB, but it's * iv must be 32 byte (it's not internally used in AES 256 ECB, but it's
* needed for IGE) * needed for IGE)
*/ */
function _ige($message, $key, $iv, $operation = 'decrypt') { function _ige($message, $key, $iv, $operation = 'decrypt') {
$message = $bytes($message); $message = str_split($message);
if ((count($key) != 32)) { if ((len($key) != 32)) {
throw new $ValueError('key must be 32 bytes long (was ' . pyjslib_str(count($key)) . ' bytes)'); throw new Exception('key must be 32 bytes long (was ' . len($key) . ' bytes)');
} }
if ((count($iv) != 32)) { if ((len($iv) != 32)) {
throw new $ValueError('iv must be 32 bytes long (was ' . pyjslib_str(count($iv)) . ' bytes)'); throw new Exception('iv must be 32 bytes long (was ' . len($iv) . ' bytes)');
} }
$cipher = new AES($key); $cipher = new AES($key);
$cipher = $cipher->encrypt($iv); $cipher = $cipher->encrypt($iv);
$blocksize = $cipher->block_size; $blocksize = $cipher->block_size;
if (((count($message) % $blocksize) != 0)) { if ((len($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)'); throw new Exception('message must be a multiple of 16 bytes (try adding ' . (16 - (count($message) % 16)) . ' bytes of padding)');
} }
$ivp = array_slice($iv, 0, $blocksize - 0); $ivp = substr($iv, 0, $blocksize - 0);
$ivp2 = array_slice($iv, $blocksize, null); $ivp2 = substr($iv, $blocksize, null);
$ciphered = $bytes(); $ciphered = null;
foreach (pyjslib_range(0, count($message), $blocksize) as $i) { foreach (pyjslib_range(0, len($message), $blocksize) as $i) {
$indata = array_slice($message, $i, ($i + $blocksize) - $i); $indata = substr($message, $i, ($i + $blocksize) - $i);
if (($operation == 'decrypt')) { if (($operation == 'decrypt')) {
$xored = new strxor($indata, $ivp2); $xored = strxor($indata, $ivp2);
$decrypt_xored = $cipher->decrypt($xored); $decrypt_xored = $cipher->decrypt($xored);
$outdata = new strxor($decrypt_xored, $ivp); $outdata = strxor($decrypt_xored, $ivp);
$ivp = $indata; $ivp = $indata;
$ivp2 = $outdata; $ivp2 = $outdata;
} else if (($operation == 'encrypt')) { } else if (($operation == 'encrypt')) {
$xored = new strxor($indata, $ivp); $xored = strxor($indata, $ivp);
$encrypt_xored = $cipher->encrypt($xored); $encrypt_xored = $cipher->encrypt($xored);
$outdata = new strxor($encrypt_xored, $ivp2); $outdata = strxor($encrypt_xored, $ivp2);
$ivp = $outdata; $ivp = $outdata;
$ivp2 = $indata; $ivp2 = $indata;
} else { } else {
throw new $ValueError('operation must be either \'decrypt\' or \'encrypt\''); throw new Exception('operation must be either \'decrypt\' or \'encrypt\'');
} }
$ciphered+= $outdata; $ciphered .= $outdata;
} }
return $ciphered; return $ciphered;
}
} }

Binary file not shown.

View File

@ -40,7 +40,7 @@ def vis(bs):
if not len(bs) % symbols_in_one_line == 0: 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 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: class Session:
""" Manages TCP Transport. encryption and message frames """ """ Manages TCP Transport. encryption and message frames """
def __init__(self, ip, port, auth_key=None, server_salt=None): def __init__(self, ip, port, auth_key=None, server_salt=None):