add methods to decrypt return data from router
[GitHub/Stricted/speedport-hybrid-php-api.git] / speedport.class.php
index 83c6b638312d1f8f8fd942b5169ecd2a8b09eec8..7bc5e8b2d246ddbf046bb5d9600745f9ffcb3219 100644 (file)
@@ -123,6 +123,7 @@ class speedport {
                $fields = array('csrf_token' => 'nulltoken', 'showpw' => 0, 'password' => $this->hash, 'reboot_device' => 'true');
                $cookie = 'challengev='.$this->challenge.'; '.$this->session;
                $data = $this->sentRequest($path, $fields, $cookie);
+               
                $json = json_decode($data['body'], true);
                
                return $json;
@@ -309,6 +310,52 @@ class speedport {
                return $json;
        }
        
+       /**
+        * decrypt data from router
+        * 
+        * @param       string  $data
+        * @return      array
+        */
+       public function decrypt ($data) {
+               require_once 'CryptLib/CryptLib.php';
+               $factory = new CryptLib\Cipher\Factory();
+               $aes = $factory->getBlockCipher('rijndael-128');
+               
+               $iv = hex2bin(substr($this->challenge, 16, 16));
+               $adata = hex2bin(substr($this->challenge, 32, 16));
+               $dkey = hex2bin($this->derivedk);
+               $enc = hex2bin($data);
+               
+               $aes->setKey($dkey);
+               $mode = $factory->getMode('ccm', $aes, $iv, [ 'adata' => $adata, 'lSize' => 7]);
+               
+               $mode->decrypt($enc);
+               
+               return $mode->finish();
+       }
+
+       /**
+        * decrypt data for the router
+        * 
+        * @param       array   $data
+        * @return      string
+        */
+       public function encrypt ($data) {
+               require_once 'CryptLib/CryptLib.php';
+               $factory = new CryptLib\Cipher\Factory();
+               $aes = $factory->getBlockCipher('rijndael-128');
+               
+               $iv = hex2bin(substr($this->challenge, 16, 16));
+               $adata = hex2bin(substr($this->challenge, 32, 16));
+               $dkey = hex2bin($this->derivedk);
+               
+               $aes->setKey($dkey);
+               $mode = $factory->getMode('ccm', $aes, $iv, [ 'adata' => $adata, 'lSize' => 7]);
+               $mode->encrypt(http_build_query($data));
+               
+               return $mode->finish();
+       }
+       
        /**
         * sends the request to router
         * 
@@ -347,6 +394,7 @@ class speedport {
                curl_close($ch);
                
                // fix invalid json
+               
                $body = preg_replace("/(\r\n)|(\r)/", "\n", $body);
                $body = preg_replace('/\'/i', '"', $body);
                $body = preg_replace("/\[\s+\]/i", '[ {} ]', $body);