From 1a71066b85cc6a7416037d922db77b09a15ab5e8 Mon Sep 17 00:00:00 2001 From: hickert Date: Fri, 30 Nov 2007 13:02:32 +0000 Subject: [PATCH] Updated socket server & client to encrypt correctly git-svn-id: https://oss.gonicus.de/repositories/gosa/trunk@7956 594d385d-05f5-0310-b6e9-bd551577e9d8 --- contrib/socket_server/client.php | 5 ++--- contrib/socket_server/server.php | 9 ++++++--- include/class_socketClient.inc | 17 +++++++---------- 3 files changed, 15 insertions(+), 16 deletions(-) diff --git a/contrib/socket_server/client.php b/contrib/socket_server/client.php index c41f1d12b..97cd490a0 100755 --- a/contrib/socket_server/client.php +++ b/contrib/socket_server/client.php @@ -4,7 +4,6 @@ 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."); @@ -15,8 +14,8 @@ if($sock->connected()){ /* Prepare a hunge bunch of data to be send */ $data = "a"; - for($i = 0 ; $i < (100 * 1); $i++){ - $data .= "a"; + for($i = 0 ; $i < (12 * 1); $i++){ + $data .= "a\n"; } echo "|--Sending ".strlen($data)."bytes of data to socket.\n"; $sock->send($data); diff --git a/contrib/socket_server/server.php b/contrib/socket_server/server.php index 66663517a..bc65fab16 100755 --- a/contrib/socket_server/server.php +++ b/contrib/socket_server/server.php @@ -128,19 +128,22 @@ function encrypt($data,$key) if($enable_encryption){ $iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB); $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND); - $data = base64_encode(mcrypt_encrypt (MCRYPT_RIJNDAEL_256, $key, $data, MCRYPT_MODE_ECB, $iv)); + $data = mcrypt_encrypt (MCRYPT_RIJNDAEL_256, $key, $data, MCRYPT_MODE_ECB, $iv); } - return($data); + return(base64_encode($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 = mcrypt_create_iv($iv_size, MCRYPT_RAND); - $data = mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $key, base64_decode($data), MCRYPT_MODE_ECB, $iv); + $data = mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $key, $data, MCRYPT_MODE_ECB, $iv); + $data = rtrim($data,"\x00"); } return($data); } diff --git a/include/class_socketClient.inc b/include/class_socketClient.inc index 74066e4bb..da54da5a8 100755 --- a/include/class_socketClient.inc +++ b/include/class_socketClient.inc @@ -44,16 +44,18 @@ class Socket_Client { /* Encrypt data */ if($this->encrypt){ - $data = base64_encode(mcrypt_encrypt (MCRYPT_RIJNDAEL_256, $this->crypt_key, $data, MCRYPT_MODE_ECB, $this->iv)); + $data = mcrypt_encrypt (MCRYPT_RIJNDAEL_256, $this->crypt_key, $data, MCRYPT_MODE_ECB, $this->iv); } - return($data); + return(base64_encode($data)); } private function decrypt($data) { + $data = base64_decode($data); /* decrypt data */ if($this->encrypt){ - $data = mcrypt_decrypt (MCRYPT_RIJNDAEL_256, $this->crypt_key, base64_decode($data), MCRYPT_MODE_ECB, $this->iv); + $data = mcrypt_decrypt (MCRYPT_RIJNDAEL_256, $this->crypt_key, $data, MCRYPT_MODE_ECB, $this->iv); + $data = rtrim($data,"\x00"); } return($data); } @@ -86,10 +88,7 @@ class Socket_Client public function send($data) { if($this->handle){ - if($this->encrypt){ - $data = $this->encrypt($data); - } - + $data = $this->encrypt($data); $data = trim($data); fputs($this->handle,$data."\n"); $this->b_data_send = TRUE; @@ -124,9 +123,7 @@ class Socket_Client } $this->bytes_read = strlen($str); $this->b_data_send = FALSE; - if($this->encrypt){ - $str = $this->decrypt($str); - } + $str = $this->decrypt($str); } return($str); } -- 2.30.2