protected function decryptPackets(array $packets = [])
    {

        // This will be returned
        $decrypted = [];

        foreach ($packets as $packet) {
            # Header :
            $header = substr($packet, 0, 20);

            $header_items = [];

            $header_key = unpack("n1", $header);

            $key = array_shift($header_key);

            $chars = unpack("C*", substr($header, 2));

            $a1 = $key & 0xFF;
            $a2 = $key >> 8;

            if ($a1 == 0) {
                throw new Exception(__METHOD__ . ": Header key is invalid");
            }

            $table = $this->head_encrypt_table;

            $characterCount = count($chars);

            $key = 0;
            for ($i = 1; $i <= $characterCount; $i++) {
                $chars[$i] -= ($table[$a2] + (($i - 1) % 5)) & 0xFF;
                $a2 = ($a2 + $a1) & 0xFF;
                if (($i % 2) == 0) {
                    $short_array = unpack("n1", pack("C2", $chars[$i - 1], $chars[$i]));
                    $header_items[$key] = $short_array[1];
                    ++$key;
                }
            }

            $header_items = array_combine([
                'zero',
                'cmd',
                'id',
                'totlen',
                'len',
                'totpck',
                'pck',
                'datakey',
                'crc',
            ], $header_items);

            // Check to make sure the number of packets match
            if ($header_items['totpck'] != count($packets)) {
                throw new Exception(__METHOD__ . ": Too few packets received");
            }

            # Data :
            $table = $this->data_encrypt_table;
            $a1 = $header_items['datakey'] & 0xFF;
            $a2 = $header_items['datakey'] >> 8;

            if ($a1 == 0) {
                throw new Exception(__METHOD__ . ": Data key is invalid");
            }

            $chars = unpack("C*", substr($packet, 20));
            $data = "";
            $characterCount = count($chars);

            for ($i = 1; $i <= $characterCount; $i++) {
                $chars[$i] -= ($table[$a2] + (($i - 1) % 72)) & 0xFF;
                $a2 = ($a2 + $a1) & 0xFF;
                $data .= chr($chars[$i]);
            }
            //@todo: Check CRC ???
            $decrypted[$header_items['pck']] = $data;
        }

        // Return the decrypted packets as one string
        return implode('', $decrypted);
    }
protected function decryptPackets(array $packets = [])
    {

        // This will be returned
        $decrypted = [];

        foreach ($packets as $packet) {
            # Header :
            $header = substr($packet, 0, 20);

            $header_items = [];

            $header_key = unpack("n1", $header);

            $key = array_shift($header_key);

            $chars = unpack("C*", substr($header, 2));

            $a1 = $key & 0xFF;
            $a2 = $key >> 8;

            if ($a1 == 0) {
                throw new Exception(__METHOD__ . ": Header key is invalid");
            }

            $table = $this->head_encrypt_table;

            $characterCount = count($chars);

            $key = 0;
            for ($i = 1; $i <= $characterCount; $i++) {
                $chars[$i] -= ($table[$a2] + (($i - 1) % 5)) & 0xFF;
                $a2 = ($a2 + $a1) & 0xFF;
                if (($i % 2) == 0) {
                    $short_array = unpack("n1", pack("C2", $chars[$i - 1], $chars[$i]));
                    $header_items[$key] = $short_array[1];
                    ++$key;
                }
            }

            $header_items = array_combine([
                'zero',
                'cmd',
                'id',
                'totlen',
                'len',
                'totpck',
                'pck',
                'datakey',
                'crc',
            ], $header_items);

            // Check to make sure the number of packets match
            if ($header_items['totpck'] != count($packets)) {
                throw new Exception(__METHOD__ . ": Too few packets received");
            }

            # Data :
            $table = $this->data_encrypt_table;
            $a1 = $header_items['datakey'] & 0xFF;
            $a2 = $header_items['datakey'] >> 8;

            if ($a1 == 0) {
                throw new Exception(__METHOD__ . ": Data key is invalid");
            }

            $chars = unpack("C*", substr($packet, 20));
            $data = "";
            $characterCount = count($chars);

            for ($i = 1; $i <= $characterCount; $i++) {
                $chars[$i] -= ($table[$a2] + (($i - 1) % 72)) & 0xFF;
                $a2 = ($a2 + $a1) & 0xFF;
                $data .= chr($chars[$i]);
            }
            //@todo: Check CRC ???
            $decrypted[$header_items['pck']] = $data;
        }

        // Return the decrypted packets as one string
        return implode('', $decrypted);
    }
protected function decryptPackets ( array $ packets = [ ] ) { // This will be returned $ decrypted = [ ] ; foreach ( $ packets as $ packet ) { # Header : $ header = substr ( $ packet , 0 , 20 ) ; $ header_items = [ ] ; $ header_key = unpack ( "n1" , $ header ) ; $ key = array_shift ( $ header_key ) ; $ chars = unpack ( "C*" , substr ( $ header , 2 ) ) ; $ a1 = $ key & 0xFF ; $ a2 = $ key >> 8 ; if ( $ a1 == 0 ) { throw new Exception ( __METHOD__ . ": Header key is invalid" ) ; } $ table = $ this -> head_encrypt_table ; $ characterCount = count ( $ chars ) ; $ key = 0 ; for ( $ i = 1 ; $ i <= $ characterCount ; $ i ++ ) { $ chars [ $ i ] -= ( $ table [ $ a2 ] + ( ( $ i - 1 ) % 5 ) ) & 0xFF ; $ a2 = ( $ a2 + $ a1 ) & 0xFF ; if ( ( $ i % 2 ) == 0 ) { $ short_array = unpack ( "n1" , pack ( "C2" , $ chars [ $ i - 1 ] , $ chars [ $ i ] ) ) ; $ header_items [ $ key ] = $ short_array [ 1 ] ; ++ $ key ; } } $ header_items = array_combine ( [ 'zero' , 'cmd' , 'id' , 'totlen' , 'len' , 'totpck' , 'pck' , 'datakey' , 'crc' , ] , $ header_items ) ; // Check to make sure the number of packets match if ( $ header_items [ 'totpck' ] != count ( $ packets ) ) { throw new Exception ( __METHOD__ . ": Too few packets received" ) ; } # Data : $ table = $ this -> data_encrypt_table ; $ a1 = $ header_items [ 'datakey' ] & 0xFF ; $ a2 = $ header_items [ 'datakey' ] >> 8 ; if ( $ a1 == 0 ) { throw new Exception ( __METHOD__ . ": Data key is invalid" ) ; } $ chars = unpack ( "C*" , substr ( $ packet , 20 ) ) ; $ data = "" ; $ characterCount = count ( $ chars ) ; for ( $ i = 1 ; $ i <= $ characterCount ; $ i ++ ) { $ chars [ $ i ] -= ( $ table [ $ a2 ] + ( ( $ i - 1 ) % 72 ) ) & 0xFF ; $ a2 = ( $ a2 + $ a1 ) & 0xFF ; $ data .= chr ( $ chars [ $ i ] ) ; } //@todo: Check CRC ??? $ decrypted [ $ header_items [ 'pck' ] ] = $ data ; } // Return the decrypted packets as one string return implode ( '' , $ decrypted ) ; }
Decrypt the incoming packets

@codeCoverageIgnore

@param array $packets

@return string
@throws \GameQ\Exception\Protocol
Decrypt the incoming packets