title: Using Java-8 AES/GCM to authenticate / encrypt parts of a data block
body: <p>My task is to use a special feature of AES/GCM to authenticate part A and encrypt part B of a single data block. I have problems with implementing the solution using Java-8.</p>
  
  <p>The following example uses a data block of 256 bit. The first 128 bit shall be authenticated only. The following 128 bit shall be encrypted. The resulting tag for the combined operation is expected to be 128 bit.</p>
  
  <p>I believe I was able to implement an encryption-only variant which encrypts both 128 bit data blocks.</p>
  
  <pre><code>SecureRandom random = new SecureRandom();
  byte[] initVector   = new BigInteger(96, random).toByteArray();
  byte[] data         = new BigInteger(255, random).toByteArray();
  byte[] key          = new BigInteger(255, random).toByteArray();
  byte[] encrypted    = new byte[data.length];
  
  final Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding");
  cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(key, "AES"), new GCMParameterSpec(16 * Byte.SIZE, initVector));
  cipher.update(data, 0, data.length, encrypted, 0);
  byte[] tag = cipher.doFinal();
  </code></pre>
  
  <p>Could anybody give instructions on how to modify the code so that the first 128 bit of data are only authenticated?</p>