From 09a38ea44807e9eca25aab9bbec82516bea805e3 Mon Sep 17 00:00:00 2001 From: cajus Date: Thu, 6 Dec 2007 10:00:08 +0000 Subject: [PATCH] Updated socket stuff git-svn-id: https://oss.gonicus.de/repositories/gosa/trunk@8040 594d385d-05f5-0310-b6e9-bd551577e9d8 --- contrib/socket_server/client.php | 9 +++------ contrib/socket_server/server.php | 24 ++++++++++++++---------- include/class_socketClient.inc | 18 +++++++++--------- 3 files changed, 26 insertions(+), 25 deletions(-) diff --git a/contrib/socket_server/client.php b/contrib/socket_server/client.php index 97cd490a0..8cf8f2f2e 100755 --- a/contrib/socket_server/client.php +++ b/contrib/socket_server/client.php @@ -5,18 +5,15 @@ require_once("../../include/class_socketClient.inc"); error_reporting(E_ALL); echo "\n\nTry to connect"; -$sock = new Socket_Client("localhost","10000",TRUE,1); -$sock->SetEncryptionKey("Hallo hier bin ich."); +$sock = new Socket_Client("10.89.1.182","10000",TRUE,1); +$sock->SetEncryptionKey("ferdinand_frost"); if($sock->connected()){ echo "... successful\n"; echo "|--Reading welcome message : \n"; echo $sock->read(); /* Prepare a hunge bunch of data to be send */ - $data = "a"; - for($i = 0 ; $i < (12 * 1); $i++){ - $data .= "a\n"; - } + $data = "HullaHorst"; echo "|--Sending ".strlen($data)."bytes of data to socket.\n"; $sock->send($data); echo "|--Done!\n"; diff --git a/contrib/socket_server/server.php b/contrib/socket_server/server.php index bc65fab16..29a0736ca 100755 --- a/contrib/socket_server/server.php +++ b/contrib/socket_server/server.php @@ -14,8 +14,7 @@ $max_clients = 3; // Rijndal encrypt key $enable_encryption = TRUE; -$encrypt_key = "Hallo hier bin ich."; - +$encrypt_key = "ferdinand_frostferdinand_frostfe"; /* Create Socket - * AF_INET means IPv4 Socket @@ -70,7 +69,7 @@ while(TRUE) { socket_write($clients[$i]['socket'],encrypt( "Welcome to GOsa Test Server ============================ -Type some text here:",$encrypt_key)."\n"); +Type some text here:",$encrypt_key)); echo("New client connected: " . $clients[$i]['ipaddy'] . " \n"); break; @@ -124,26 +123,31 @@ Type some text here:",$encrypt_key)."\n"); function encrypt($data,$key) { global $enable_encryption; + + $data= str_repeat("0", 16 - strlen($data)%16).$data; + echo "1EEE>>>>>>>>>>>>>>>>>".strlen($data)."\n"; + /* Encrypt data */ if($enable_encryption){ - $iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB); + $iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_CBC); $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND); - $data = mcrypt_encrypt (MCRYPT_RIJNDAEL_256, $key, $data, MCRYPT_MODE_ECB, $iv); + echo "Size: $iv_size\n"; + $data = mcrypt_encrypt (MCRYPT_RIJNDAEL_256, $key, $data, MCRYPT_MODE_CBC, $iv); } - return(base64_encode($data)); + echo "2EEE>>>>>>>>>>>>>>>>>".strlen($data)."\n"; + return($data); } function decrypt($data,$key) { global $enable_encryption; - $data = base64_decode($data); /* Decrypt data */ if($enable_encryption){ - $iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB); + $iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_CBC); $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND); - $data = mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $key, $data, MCRYPT_MODE_ECB, $iv); - $data = rtrim($data,"\x00"); + $data = mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $key, $data, MCRYPT_MODE_CBC, $iv); + $data = ltrim($data,"0"); } return($data); } diff --git a/include/class_socketClient.inc b/include/class_socketClient.inc index bf0b79522..b2957e450 100755 --- a/include/class_socketClient.inc +++ b/include/class_socketClient.inc @@ -28,15 +28,15 @@ class Socket_Client public function SetEncryptionKey($data) { - $data= str_pad($data, 32, $data); if(!function_exists("mcrypt_get_iv_size")){ $this->error = _("The mcrypt module was not found. Please install php5-mcrypt.") ; return(FALSE); }else{ $this->encrypt = TRUE; - $this->crypt_key= $data; - $iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_CBC); - $this->iv = mcrypt_create_iv($iv_size, MCRYPT_RAND); + $this->iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_CBC); + $this->crypt_key= str_pad($data, $this->iv_size, $data); + echo "Setting key to $this->crypt_key\n"; + $this->iv = mcrypt_create_iv($this->iv_size, MCRYPT_RAND); return(TRUE); } } @@ -47,16 +47,16 @@ class Socket_Client if($this->encrypt){ $data = mcrypt_encrypt (MCRYPT_RIJNDAEL_256, $this->crypt_key, $data, MCRYPT_MODE_CBC, $this->iv); } + echo "EEEE>>>>>>>>>>>>>>>>>>>>>>>>> ".strlen($data)."\n"; return($data); } private function decrypt($data) { + echo "DDDD>>>>>>>>>>>>>>>>>>>>>>>>> ".strlen($data)."\n"; /* decrypt data */ - if($this->encrypt){ - $data = mcrypt_decrypt (MCRYPT_RIJNDAEL_256, $this->crypt_key, $data, MCRYPT_MODE_CBC, $this->iv); - $data = rtrim($data,"\x00"); - } + $data = mcrypt_decrypt (MCRYPT_RIJNDAEL_256, $this->crypt_key, rtrim($data), MCRYPT_MODE_CBC, $this->iv); + $data = ltrim($data,"0"); return($data); } @@ -126,7 +126,7 @@ class Socket_Client $this->b_data_send = FALSE; $str = $this->decrypt($str); } - return(preg_replace('/^0*/', '', $str)); + return($str); } public function read() -- 2.30.2