protected Message _decrypt(final Cipher cipher, Message msg) throws Exception { if(msg.getLength() == 0) return msg; byte[] decrypted_msg; if(cipher == null) decrypted_msg=code(msg.getRawBuffer(), msg.getOffset(), msg.getLength(), true); else try { decrypted_msg=cipher.doFinal(msg.getRawBuffer(), msg.getOffset(), msg.getLength()); } catch(BadPaddingException | IllegalBlockSizeException e) { // if any exception is thrown, this cipher object may need to be reset before it can be used again. cipher.init(Cipher.DECRYPT_MODE, secret_key); throw e; } return msg.setBuffer(decrypted_msg); }
protected void encryptAndSend(Message msg) throws Exception { EncryptHeader hdr=new EncryptHeader(EncryptHeader.ENCRYPT, symVersion()); // copy neeeded because same message (object) may be retransmitted -> prevent double encryption Message msgEncrypted=msg.copy(false).putHeader(this.id, hdr); if(msg.getLength() > 0) msgEncrypted.setBuffer(code(msg.getRawBuffer(),msg.getOffset(),msg.getLength(),false)); else { // length is 0 byte[] payload=msg.getRawBuffer(); if(payload != null) // we don't encrypt empty buffers (https://issues.jboss.org/browse/JGRP-2153) msgEncrypted.setBuffer(payload, msg.getOffset(), msg.getLength()); } down_prot.down(msgEncrypted); }
protected Message _decrypt(final Cipher cipher, Message msg) throws Exception { if(msg.getLength() == 0) return msg; byte[] decrypted_msg; if(cipher == null) decrypted_msg=code(msg.getRawBuffer(), msg.getOffset(), msg.getLength(), true); else decrypted_msg=cipher.doFinal(msg.getRawBuffer(), msg.getOffset(), msg.getLength()); return msg.setBuffer(decrypted_msg); }
protected void encryptAndSend(Message msg) throws Exception { EncryptHeader hdr=new EncryptHeader(EncryptHeader.ENCRYPT, symVersion()); // copy neeeded because same message (object) may be retransmitted -> prevent double encryption Message msgEncrypted=msg.copy(false).putHeader(this.id, hdr); if(msg.getLength() > 0) msgEncrypted.setBuffer(code(msg.getRawBuffer(),msg.getOffset(),msg.getLength(),false)); else { // length is 0 byte[] payload=msg.getRawBuffer(); if(payload != null) // we don't encrypt empty buffers (https://issues.jboss.org/browse/JGRP-2153) msgEncrypted.setBuffer(payload, msg.getOffset(), msg.getLength()); } down_prot.down(msgEncrypted); }