From 57122ef7f729e37a8e892e01dffe2da77aa3d638 Mon Sep 17 00:00:00 2001 From: hickert Date: Fri, 30 Nov 2007 12:37:48 +0000 Subject: [PATCH] Added encryption. Seems not to work in all cases ... git-svn-id: https://oss.gonicus.de/repositories/gosa/trunk@7954 594d385d-05f5-0310-b6e9-bd551577e9d8 --- contrib/socket_server/client.php | 5 ++-- contrib/socket_server/server.php | 43 ++++++++++++++++++++++++++----- include/class_socketClient.inc | 44 ++++++++++++++++++++++++++++++++ 3 files changed, 84 insertions(+), 8 deletions(-) diff --git a/contrib/socket_server/client.php b/contrib/socket_server/client.php index bdf01e26b..b1f9e2ae8 100755 --- a/contrib/socket_server/client.php +++ b/contrib/socket_server/client.php @@ -7,6 +7,7 @@ error_reporting(E_ALL); echo "\n\nTry to connect"; $sock = new Socket_Client("localhost","10000",TRUE,1); +#$sock->SetEncryptionKey("Hallo hier bin ich."); if($sock->connected()){ echo "... successful\n"; echo "|--Reading welcome message : \n"; @@ -14,13 +15,13 @@ if($sock->connected()){ /* Prepare a hunge bunch of data to be send */ $data = "a"; - for($i = 0 ; $i < (1024 * 1); $i++){ + for($i = 0 ; $i < (100 * 1); $i++){ $data .= "a"; } echo "|--Sending ".strlen($data)."bytes of data to socket.\n"; $sock->send($data); echo "|--Done!\n"; - $sock->read(); + echo $sock->read(); echo "|--".$sock->bytes_read()."bytes read.\n"; echo "|--Sending 'exit' command to socket.\n"; $sock->send("exit"); diff --git a/contrib/socket_server/server.php b/contrib/socket_server/server.php index fc7849ac2..b09493022 100755 --- a/contrib/socket_server/server.php +++ b/contrib/socket_server/server.php @@ -12,6 +12,9 @@ $bind_port = 10000; // Max clients $max_clients = 3; +// Rijndal encrypt key +$enable_encryption = FALSE; +$encrypt_key = "Hallo hier bin ich."; /* Create Socket - @@ -64,10 +67,10 @@ while(TRUE) { socket_getpeername($clients[$i]['socket'],$ip); $clients[$i]['ipaddy'] = $ip; - socket_write($clients[$i]['socket'], + socket_write($clients[$i]['socket'],encrypt( "Welcome to GOsa Test Server ============================ -Type some text here:\n"); +Type some text here:\n",$encrypt_key)); echo("New client connected: " . $clients[$i]['ipaddy'] . " \n"); break; @@ -90,7 +93,7 @@ Type some text here:\n"); if(isset($clients[$i]) && in_array($clients[$i]['socket'],$read)) { /* Read socket data */ - $data = @socket_read($clients[$i]['socket'],1024000, PHP_NORMAL_READ); + $data = socket_read($clients[$i]['socket'],1024000, PHP_NORMAL_READ); /* Client disconnected */ if ($data === FALSE) { @@ -99,20 +102,48 @@ Type some text here:\n"); continue; } - $data = trim($data); + $data = trim(decrypt($data,$encrypt_key)); echo "Client (".$clients[$i]['ipaddy'].") send : ".substr($data,0,30)."... \n"; if($data == "exit"){ /* Close conenction */ - socket_write($clients[$i]['socket'],"Bye Bye!"); + socket_write($clients[$i]['socket'],encrypt("Bye Bye!",$encrypt_key)); @socket_close($clients[$i]); echo "Client disconnected! bye bye!".$clients[$i]['ipaddy']."\n"; }else{ /* Send some data back to the client */ - $data = strrev($data); + $data = encrypt(strrev($data),$encrypt_key); socket_write($clients[$i]['socket'],$data); } } } } + + + +function encrypt($data,$key) +{ + global $enable_encryption; + /* Encrypt 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_encrypt (MCRYPT_RIJNDAEL_256, $key, $data, MCRYPT_MODE_ECB, $iv); + } + return($data); +} + +function decrypt($data,$key) +{ + global $enable_encryption; + /* 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, $data, MCRYPT_MODE_ECB, $iv); + } + return($data); +} + + ?> diff --git a/include/class_socketClient.inc b/include/class_socketClient.inc index 3823c4662..45d427c2e 100755 --- a/include/class_socketClient.inc +++ b/include/class_socketClient.inc @@ -12,6 +12,11 @@ class Socket_Client private $bytes_read = 0; private $error = ""; + private $encrypt = FALSE; + private $crypt_key = ""; + + private $iv; + public function __construct($host, $port, $connect = TRUE,$timeout = 3){ $this->host = $host; $this->port = $port; @@ -21,6 +26,38 @@ class Socket_Client } } + public function SetEncryptionKey($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_ECB); + $this->iv = mcrypt_create_iv($iv_size, MCRYPT_RAND); + return(TRUE); + } + } + + private function encrypt($data) + { + /* Encrypt data */ + if($this->encrypt){ + $data = mcrypt_encrypt (MCRYPT_RIJNDAEL_256, $this->crypt_key, $data, MCRYPT_MODE_ECB, $this->iv); + } + return($data); + } + + private function decrypt($data) + { + /* decrypt data */ + if($this->encrypt){ + $data = mcrypt_decrypt (MCRYPT_RIJNDAEL_256, $this->crypt_key, $data, MCRYPT_MODE_ECB, $this->iv); + } + return($data); + } + public function connected() { return($this->handle == TRUE); @@ -49,6 +86,10 @@ class Socket_Client public function send($data) { if($this->handle){ + if($this->encrypt){ + $data = $this->encrypt($data); + } + $data = trim($data); fputs($this->handle,$data."\n"); $this->b_data_send = TRUE; @@ -82,6 +123,9 @@ class Socket_Client } $this->bytes_read = strlen($str); $this->b_data_send = FALSE; + if($this->encrypt){ + $str = $this->decrypt($str); + } } return($str); } -- 2.30.2