private byte[] encryptInternal(final byte[] plaintext, final byte[] associatedData)
      throws GeneralSecurityException {
    // Check that ciphertext is not longer than the max. size of a Java array.
    if (plaintext.length > Integer.MAX_VALUE - IV_SIZE_IN_BYTES - TAG_SIZE_IN_BYTES) {
      throw new GeneralSecurityException("plaintext too long");
    }
    byte[] ciphertext = new byte[IV_SIZE_IN_BYTES + plaintext.length + TAG_SIZE_IN_BYTES];
    Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding");
    cipher.init(Cipher.ENCRYPT_MODE, key);
    cipher.updateAAD(associatedData);
    int unusedWritten =
        cipher.doFinal(plaintext, 0, plaintext.length, ciphertext, IV_SIZE_IN_BYTES);
    // Copy the IV that is randomly generated by Android Keystore.
    System.arraycopy(cipher.getIV(), 0, ciphertext, 0, IV_SIZE_IN_BYTES);
    return ciphertext;
  }
An {@link Aead} that does AES-GCM encryption with a key stored in <a
href="https://developer.android.com/training/articles/keystore.html">Android Keystore</a>.

<p>This class requires Android M (API level 23) or newer.

@since 1.0.0
java_src/src/main/java/com/google/crypto/tink/integration/android/AndroidKeystoreAesGcm.java