public byte[] encrypt(final byte[] nonce, final byte[] plaintext, final byte[] associatedData)
      throws GeneralSecurityException {
    if (plaintext == null) {
      throw new NullPointerException("plaintext is null");
    }
    if (nonce.length != NONCE_SIZE_IN_BYTES) {
      throw new GeneralSecurityException("nonce length must be " + NONCE_SIZE_IN_BYTES + " bytes.");
    }
    AlgorithmParameterSpec params = new IvParameterSpec(nonce);
    Cipher cipher = ChaCha20Poly1305Jce.getThreadLocalCipherOrNull();
    cipher.init(Cipher.ENCRYPT_MODE, keySpec, params);
    if (associatedData != null && associatedData.length != 0) {
      cipher.updateAAD(associatedData);
    }
    return cipher.doFinal(plaintext);
  }
Implements ChaCha20Poly1305, as described in <a
href="https://tools.ietf.org/html/rfc8439#section-2.8">RFC 8439, section 2.8</a>.

<p>It is similar to {@link ChaCha20Poly1305Jce}, but it offers an interface for the user to
choose the nonce, which is needed in HPKE.

<p>It uses the JCE, and requires that algorithm "ChaCha20-Poly1305" is present.
java_src/src/main/java/com/google/crypto/tink/aead/internal/InsecureNonceChaCha20Poly1305Jce.java