public byte[] encrypt(FacesContext facesContext,byte[] bytes){
  byte[] securedata=null;
  try {
    SecureRandom rand=new SecureRandom();
    byte[] iv=new byte[16];
    rand.nextBytes(iv);
    IvParameterSpec ivspec=new IvParameterSpec(iv);
    Cipher encryptCipher=Cipher.getInstance(CIPHER_CODE);
    SecretKey secKey=getSecretKey(facesContext);
    encryptCipher.init(Cipher.ENCRYPT_MODE,secKey,ivspec);
    Mac encryptMac=Mac.getInstance(MAC_CODE);
    encryptMac.init(secKey);
    encryptMac.update(iv);
    byte[] encdata=encryptCipher.doFinal(bytes);
    byte[] macBytes=encryptMac.doFinal(encdata);
    byte[] tmp=concatBytes(macBytes,iv);
    securedata=concatBytes(tmp,encdata);
  }
 catch (  NoSuchAlgorithmException|NoSuchPaddingException|InvalidKeyException|InvalidAlgorithmParameterException|IllegalStateException|IllegalBlockSizeException|BadPaddingException e) {
    if (LOGGER.isLoggable(Level.SEVERE)) {
      LOGGER.log(Level.SEVERE,"Unexpected exception initializing encryption." + "  No encryption will be performed.",e);
    }
    return null;
  }
  return securedata;
}

public byte [ ] encrypt ( FacesContext facesContext , byte [ ] bytes ) { byte [ ] securedata = null ; try { SecureRandom rand = new SecureRandom ( ) ; byte [ ] iv = new byte [ 16 ] ; rand . nextBytes ( iv ) ; IvParameterSpec ivspec = new IvParameterSpec ( iv ) ; Cipher encryptCipher = Cipher . getInstance ( CIPHER_CODE ) ; SecretKey secKey = getSecretKey ( facesContext ) ; encryptCipher . init ( Cipher . ENCRYPT_MODE , secKey , ivspec ) ; Mac encryptMac = Mac . getInstance ( MAC_CODE ) ; encryptMac . init ( secKey ) ; encryptMac . update ( iv ) ; byte [ ] encdata = encryptCipher . doFinal ( bytes ) ; byte [ ] macBytes = encryptMac . doFinal ( encdata ) ; byte [ ] tmp = concatBytes ( macBytes , iv ) ; securedata = concatBytes ( tmp , encdata ) ; } catch ( NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | InvalidAlgorithmParameterException | IllegalStateException | IllegalBlockSizeException | BadPaddingException e ) { if ( LOGGER . isLoggable ( Level . SEVERE ) ) { LOGGER . log ( Level . SEVERE , "Unexpected exception initializing encryption." + "  No encryption will be performed." , e ) ; } return null ; } return securedata ; }
this method : encrypts bytes using a cipher . generates mac for intialization vector of the cipher generates mac for encrypted data returns a byte array consisting of the following concatenated together : | mac for cnrypted data | mac for init vector | encrypted data |
