Exemplo n.º 1
0
  private void encryptAndSend(Message msg) throws Exception {
    EncryptHeader hdr = new EncryptHeader(EncryptHeader.ENCRYPT, getSymVersion());
    if (this.encrypt_entire_message) hdr.type |= EncryptHeader.ENCRYPT_ENTIRE_MSG;

    if (encrypt_entire_message) {
      if (msg.getSrc() == null) msg.setSrc(local_addr);

      Buffer serialized_msg = Util.streamableToBuffer(msg);
      byte[] encrypted_msg =
          code(
              serialized_msg.getBuf(),
              serialized_msg.getOffset(),
              serialized_msg.getLength(),
              false);

      // exclude existing headers, they will be seen again when we decrypt and unmarshal the msg at
      // the receiver
      Message tmp = msg.copy(false, false).setBuffer(encrypted_msg).putHeader(this.id, hdr);
      down_prot.down(new Event(Event.MSG, tmp));
      return;
    }

    // copy neeeded because same message (object) may be retransmitted -> no double encryption
    Message msgEncrypted =
        msg.copy(false)
            .putHeader(this.id, hdr)
            .setBuffer(code(msg.getRawBuffer(), msg.getOffset(), msg.getLength(), false));
    down_prot.down(new Event(Event.MSG, msgEncrypted));
  }