public function encrypt($data) {
    $plaintext = json_encode($data);

    $public_key = $this->getPublicKey();

    $result = openssl_seal($plaintext, $message, $keys, array($public_key));

    if ($result === FALSE || empty($keys[0]) || empty($message) || $message === $plaintext) {
      throw new EncryptionException('Unable to encrypt a message: ' . openssl_error_string());
    }

    $envelope = (object) array(
      'key' => base64_encode($keys[0]),
      'message' => base64_encode($message),
    );

    return base64_encode(json_encode($envelope));
  }