private int writeEncryptedBlock(final byte[] input,final int off,final int len,final byte[] out,final int outOff) throws BadCiphertextException {
  if (out.length == 0) {
    return 0;
  }
  int outLen=0;
  final int seqNum=1;
  final byte[] contentAad=Utils.generateContentAad(messageId_,Constants.SINGLE_BLOCK_STRING_ID,seqNum,len);
  final byte[] nonce=new byte[nonceLen_];
  RND.nextBytes(nonce);
  final CipherHandler cipherHandler=new CipherHandler(encryptionKey_,nonce,contentAad,Cipher.ENCRYPT_MODE,cryptoAlgo_);
  final byte[] encryptedBytes=cipherHandler.cipherData(input,off,len);
  final int encryptedContentLen=encryptedBytes.length - tagLenBytes_;
  final CipherBlockHeaders cipherBlockHeaders=new CipherBlockHeaders(nonce,encryptedContentLen);
  final byte[] cipherBlockHeaderBytes=cipherBlockHeaders.toByteArray();
  System.arraycopy(cipherBlockHeaderBytes,0,out,outOff + outLen,cipherBlockHeaderBytes.length);
  outLen+=cipherBlockHeaderBytes.length;
  System.arraycopy(encryptedBytes,0,out,outOff + outLen,encryptedBytes.length);
  outLen+=encryptedBytes.length;
  return outLen;
}

private int writeEncryptedBlock ( final byte [ ] input , final int off , final int len , final byte [ ] out , final int outOff ) throws BadCiphertextException { if ( out . length == 0 ) { return 0 ; } int outLen = 0 ; final int seqNum = 1 ; final byte [ ] contentAad = Utils . generateContentAad ( messageId_ , Constants . SINGLE_BLOCK_STRING_ID , seqNum , len ) ; final byte [ ] nonce = new byte [ nonceLen_ ] ; RND . nextBytes ( nonce ) ; final CipherHandler cipherHandler = new CipherHandler ( encryptionKey_ , nonce , contentAad , Cipher . ENCRYPT_MODE , cryptoAlgo_ ) ; final byte [ ] encryptedBytes = cipherHandler . cipherData ( input , off , len ) ; final int encryptedContentLen = encryptedBytes . length - tagLenBytes_ ; final CipherBlockHeaders cipherBlockHeaders = new CipherBlockHeaders ( nonce , encryptedContentLen ) ; final byte [ ] cipherBlockHeaderBytes = cipherBlockHeaders . toByteArray ( ) ; System . arraycopy ( cipherBlockHeaderBytes , 0 , out , outOff + outLen , cipherBlockHeaderBytes . length ) ; outLen += cipherBlockHeaderBytes . length ; System . arraycopy ( encryptedBytes , 0 , out , outOff + outLen , encryptedBytes . length ) ; outLen += encryptedBytes . length ; return outLen ; }
this method encrypts the provided bytes , creates the headers for the block , and assembles the block containing the headers and the encrypted bytes .
