Parse the ciphertext, process it, and return the response.

FIXME Catch exceptions and return in a nice format.

@param string $blob
POST'ed ciphertext.
@return Message
181480
public function handle($blob) {
    try {
      $reqData = $this->decode(RegistrationMessage::NAME, $blob);
    }
    catch (InvalidMessageException $e) {
      $this->log->warning('Received invalid message', array(
        'exception' => $e,
      ));
      $resp = new InsecureMessage(array(
        'is_error' => 1,
        'error_message' => 'Invalid message coding',
        array(
          $e->getMessage(),
          $e->getTraceAsString(),
        ),
      ));
      return $resp->setCode(400);
    }

    $this->log->debug('Received registration request', array(
      'reqData' => $reqData,
    ));
    $cxn = $reqData['cxn'];
    $validation = Cxn::getValidationMessages($cxn);
    if (!empty($validation)) {
      // $cxn is not valid, so we can't use it for encoding.
      $resp = new InsecureMessage(array(
        'is_error' => 1,
        'error_message' => 'Invalid cxn details: ' . implode(', ', array_keys($validation)),
      ));
      return $resp->setCode(400);
    }

    $respData = $this->call($reqData);
    $this->log->debug('Responding', array($cxn['cxnId'], $cxn['secret'], $respData));
    return new StdMessage($cxn['cxnId'], $cxn['secret'], $respData);
  }
1036893
function decrypt($ciphertext)
    {
        if ($this->k <= 0) {
            return false;
        }

        $ciphertext = str_split($ciphertext, $this->k);
        $ciphertext[count($ciphertext) - 1] = str_pad($ciphertext[count($ciphertext) - 1], $this->k, chr(0), STR_PAD_LEFT);

        $plaintext = '';

        switch ($this->encryptionMode) {
            case self::ENCRYPTION_NONE:
                $decrypt = '_raw_encrypt';
                break;
            case self::ENCRYPTION_PKCS1:
                $decrypt = '_rsaes_pkcs1_v1_5_decrypt';
                break;
            //case self::ENCRYPTION_OAEP:
            default:
                $decrypt = '_rsaes_oaep_decrypt';
        }

        foreach ($ciphertext as $c) {
            $temp = $this->$decrypt($c);
            if ($temp === false) {
                return false;
            }
            $plaintext.= $temp;
        }

        return $plaintext;
    }
84018
public function decrypt($data)
    {
        $data = json_decode(base64_decode($data), true);

        $iv = base64_decode($data['iv']);

        if (!$this->isEncryptedDataValid($data)) {
            throw new Exception('The given encrypted data is invalid.');
        }
        if (!$this->isMacValid($data, 16)) {
            throw new Exception('The hash is invalid.');
        }
        /**
         * try to decrypt.
         */
        $decrypted = \openssl_decrypt(
            $data['value'], $this->cipher, $this->key, 0, $iv
        );

        /*
         * throw exception is we cannot decrypt
         */
        if ($decrypted === false) {
            throw new Exception('Data could not be decrypted.');
        }

        return $decrypted;
    }
581250
public function decrypt($payload, $unserialize = true)
    {
        $payload = $this->getJsonPayload($payload);

        $iv = base64_decode($payload['iv']);

        // Here we will decrypt the value. If we are able to successfully decrypt it
        // we will then unserialize it and return it out to the caller. If we are
        // unable to decrypt this value we will throw out an exception message.
        $decrypted = \openssl_decrypt(
            $payload['value'], $this->cipher, $this->key, 0, $iv
        );

        if ($decrypted === false) {
            throw new \Exception('Could not decrypt the data.');
        }

        return $unserialize ? unserialize($decrypted) : $decrypted;
    }
368743
function parse_CRYPTOVersion(blob, length/*:?number*/) {
	var o/*:any*/ = {};
	o.Major = blob.read_shift(2);
	o.Minor = blob.read_shift(2);
	/*:: if(length == null) return o; */
	if(length >= 4) blob.l += length - 4;
	return o;
}
978578
function parse_RC4Header(blob, length) {
	var o = {};
	var vers = o.EncryptionVersionInfo = parse_CRYPTOVersion(blob, 4); length -= 4;
	if(vers.Major != 1 || vers.Minor != 1) throw 'unrecognized version code ' + vers.Major + ' : ' + vers.Minor;
	o.Salt = blob.read_shift(16);
	o.EncryptedVerifier = blob.read_shift(16);
	o.EncryptedVerifierHash = blob.read_shift(16);
	return o;
}
1328975
private function decryptPublic($data)
    {
        if (!openssl_public_decrypt($data, $decrypted, $this->key, $this->cryptParams['padding'])) {
            throw new Exception('Failure decrypting Data (openssl public) - ' . openssl_error_string());
        }
        return $decrypted;
    }
501447
public function decryptResponse($response)
    {
        if (!is_string($response)) {
            return $response;
        }

        if ($this->usesEncryption()) {
            return json_decode(
                json_encode(
                    JWT::decode(
                        $response,
                        $this->encryptionKey,
                        array($this->encryptionAlgorithm)
                    )
                ),
                true
            );
        }

        throw EncryptionConfigurationException::undeterminedEncryption();
    }
659375
public function decryptECB($data) {
		return mcrypt_decrypt(MCRYPT_RIJNDAEL_128, self::BLOB_ENCRYPTION_KEY, self::pad($data), MCRYPT_MODE_ECB);
	}
393229
protected function parseMessageFromRequest($content)
    {
        $content = strval($content);

        $arrayable = json_decode($content, true);
        if (json_last_error() === JSON_ERROR_NONE) {
            return $arrayable;
        }

        if (!$this->encryptor) {
            throw new RuntimeException('Safe mode Encryptor is necessary, please use Guard::setEncryptor(Encryptor $encryptor) set the encryptor instance.');
        }

        $message = $this->encryptor->decryptMsg(
            $this->request->get('msg_signature'),
            $this->request->get('nonce'),
            $this->request->get('timestamp'),
            $content
        );

        return $message;
    }
984709
private function parseHeader() {
        $this->client->openFolder($this->folder_path);
        $this->header = $header = imap_fetchheader($this->client->getConnection(), $this->uid, IMAP::FT_UID);

        $this->priority = $this->extractPriority($this->header);

        if ($this->header) {
            $header = imap_rfc822_parse_headers($this->header);
        }

        if (property_exists($header, 'subject')) {
            if($this->config['decoder']['message']['subject'] === 'utf-8') {
                $this->subject = imap_utf8($header->subject);
            }else{
                $this->subject = mb_decode_mimeheader($header->subject);
            }
        }

        foreach(['from', 'to', 'cc', 'bcc', 'reply_to', 'sender'] as $part){
            $this->extractHeaderAddressPart($header, $part);
        }

        if (property_exists($header, 'references')) {
            $this->references = $header->references;
        }
        if (property_exists($header, 'in_reply_to')) {
            $this->in_reply_to = str_replace(['<', '>'], '', $header->in_reply_to);
        }
        if (property_exists($header, 'message_id')) {
            $this->message_id = str_replace(['<', '>'], '', $header->message_id);
        }
        if (property_exists($header, 'Msgno')) {
            $messageNo = (int) trim($header->Msgno);
            $this->message_no = ($this->fetch_options == IMAP::FT_UID) ? $messageNo : imap_msgno($this->client->getConnection(), $messageNo);
        } else {
            $this->message_no = imap_msgno($this->client->getConnection(), $this->getUid());
        }

        $this->date = $this->parseDate($header);
    }
613966
public function encrypt($message)
    {

        $nonceSize = \openssl_cipher_iv_length($this->method);
        $nonce     = \openssl_random_pseudo_bytes($nonceSize, $cryptStrong);

        if (false === $cryptStrong || false === $nonce)
        {
            throw new Exceptions\Runtime('IV generation failed');
        }

        $cipherText = \openssl_encrypt(
            $message,
            $this->method,
            $this->password,
            OPENSSL_RAW_DATA,
            $nonce
        );

        return \base64_encode($nonce . $cipherText);
    }
978757
public function decrypt(string $data)
    {
        $this->throwExceptionIfKeyEmpty();

        if ($data === '') {
            return '';
        }

        list($encrypted_data, $iv) = \explode('::', \base64_decode($data, true), 2);

        return \openssl_decrypt($encrypted_data, $this->method, $this->key, 0, $iv);
    }
307217
public function parse($payload)
    {
        if (function_exists('msgpack_unpack')) {
            if ($payload) {
                $prevHandler = set_error_handler(function ($errno, $errstr, $errfile, $errline, $errcontext) {
                    throw new \Exception($errstr);  // @codeCoverageIgnore
                });

                try {
                    $msg = msgpack_unpack(trim($payload));
                    if ( ! $msg) {
                        throw new \Exception('Unknown error');  // @codeCoverageIgnore
                    }
                } catch (\Exception $e) {
                    set_error_handler($prevHandler);
                    throw new ParserException('Failed To Parse MSGPack - ' . $e->getMessage());
                }

                set_error_handler($prevHandler);

                return $msg;
            }
            return [];
        }

        throw new ParserException('Failed To Parse MSGPack - Supporting Library Not Available');  // @codeCoverageIgnore
    }
1035588
private function parse_result($data) {
    try {
      if ($this->resp_type == 'json') {
        $result = @json_decode($data);
      } else {
        $result = @simplexml_load_string($data);
      }
    } catch (Exception $e) {
      throw new GiantBombException('Parse error occoured', null, $e);
    }

    if (empty($result)) {
      throw new GiantBombException('Unknown error occured');
    } else if (!empty($result->error) && strtoupper($result->error) != 'OK') {
      throw new GiantBombException('Following error encountered: ' . $result->error);
    }

    return $result;
  }
129868
function _BlobDecode_old( $blob )
	{
		$blobid = ibase_blob_open($this->_connectionID, $blob );
		$realblob = ibase_blob_get( $blobid,$this->maxblobsize); // 2nd param is max size of blob -- Kevin Boillet <kevinboillet@yahoo.fr>
		while($string = ibase_blob_get($blobid, 8192)){
			$realblob .= $string;
		}
		ibase_blob_close( $blobid );

		return( $realblob );
	}
978382
public static function decrypt($message, $key, $encoded = false)
    {
        if ($encoded) {
            $message = base64_decode($message, true);
            if ($message === false) {
                throw new Exception('Encryption failure');
            }
        }

        $nonceSize = openssl_cipher_iv_length(self::METHOD);
        $nonce = mb_substr($message, 0, $nonceSize, '8bit');
        $ciphertext = mb_substr($message, $nonceSize, null, '8bit');

        $plaintext = openssl_decrypt(
            $ciphertext,
            self::METHOD,
            $key,
            OPENSSL_RAW_DATA,
            $nonce
        );

        return $plaintext;
    }