Code

Added encryption.
authorhickert <hickert@594d385d-05f5-0310-b6e9-bd551577e9d8>
Fri, 30 Nov 2007 12:37:48 +0000 (12:37 +0000)
committerhickert <hickert@594d385d-05f5-0310-b6e9-bd551577e9d8>
Fri, 30 Nov 2007 12:37:48 +0000 (12:37 +0000)
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
contrib/socket_server/server.php
include/class_socketClient.inc

index bdf01e26b2ef1ddb4d3af075c863490d05928cb4..b1f9e2ae8a1b2830addfdd4e814db0e8118af417 100755 (executable)
@@ -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");
index fc7849ac2964cea009e33db9e6694820f193b20b..b094930224ae6ec536bb20a6e6aab382965102ac 100755 (executable)
@@ -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);
+}
+
+
 ?> 
index 3823c466210a102c49a3e2d53cfd23937906e156..45d427c2e183fec9ffe4f31af53647474dac6b7e 100755 (executable)
@@ -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);
        }