private int writeEncryptedFrame(final byte[] input,final int off,final int len,final byte[] out,final int outOff) throws BadCiphertextException, AwsCryptoException {
  if (frameNumber_ > Constants.MAX_FRAME_NUMBER) {
    throw new AwsCryptoException("Frame number exceeded the maximum allowed value.");
  }
  if (out.length == 0) {
    return 0;
  }
  int outLen=0;
  byte[] contentAad;
  if (isFinalFrame_ == true) {
    contentAad=Utils.generateContentAad(messageId_,Constants.FINAL_FRAME_STRING_ID,(int)frameNumber_,len);
  }
 else {
    contentAad=Utils.generateContentAad(messageId_,Constants.FRAME_STRING_ID,(int)frameNumber_,frameSize_);
  }
  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 CipherFrameHeaders cipherFrameHeaders=new CipherFrameHeaders((int)frameNumber_,nonce,encryptedContentLen,isFinalFrame_);
  final byte[] cipherFrameHeaderBytes=cipherFrameHeaders.toByteArray();
  System.arraycopy(cipherFrameHeaderBytes,0,out,outOff + outLen,cipherFrameHeaderBytes.length);
  outLen+=cipherFrameHeaderBytes.length;
  System.arraycopy(encryptedBytes,0,out,outOff + outLen,encryptedBytes.length);
  outLen+=encryptedBytes.length;
  frameNumber_++;
  return outLen;
}

private int writeEncryptedFrame ( final byte [ ] input , final int off , final int len , final byte [ ] out , final int outOff ) throws BadCiphertextException , AwsCryptoException { if ( frameNumber_ > Constants . MAX_FRAME_NUMBER ) { throw new AwsCryptoException ( "Frame number exceeded the maximum allowed value." ) ; } if ( out . length == 0 ) { return 0 ; } int outLen = 0 ; byte [ ] contentAad ; if ( isFinalFrame_ == true ) { contentAad = Utils . generateContentAad ( messageId_ , Constants . FINAL_FRAME_STRING_ID , ( int ) frameNumber_ , len ) ; } else { contentAad = Utils . generateContentAad ( messageId_ , Constants . FRAME_STRING_ID , ( int ) frameNumber_ , frameSize_ ) ; } 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 CipherFrameHeaders cipherFrameHeaders = new CipherFrameHeaders ( ( int ) frameNumber_ , nonce , encryptedContentLen , isFinalFrame_ ) ; final byte [ ] cipherFrameHeaderBytes = cipherFrameHeaders . toByteArray ( ) ; System . arraycopy ( cipherFrameHeaderBytes , 0 , out , outOff + outLen , cipherFrameHeaderBytes . length ) ; outLen += cipherFrameHeaderBytes . length ; System . arraycopy ( encryptedBytes , 0 , out , outOff + outLen , encryptedBytes . length ) ; outLen += encryptedBytes . length ; frameNumber_ ++ ; return outLen ; }
we encrypt the bytes , create the headers for the block , and assemble the frame containing the headers and the encrypted bytes .
