@Test
  public void createEncryptDecryptDeterministically()
      throws Exception {
    KeysetHandle handle = KeysetHandle.generateNew(KeyTemplates.get("AES256_SIV"));
    DeterministicAead daead = handle.getPrimitive(DeterministicAead.class);
    byte[] plaintext = "plaintext".getBytes(UTF_8);
    byte[] associatedData = "associatedData".getBytes(UTF_8);
    byte[] ciphertext = daead.encryptDeterministically(plaintext, associatedData);
    byte[] decrypted = daead.decryptDeterministically(ciphertext, associatedData);
    assertThat(decrypted).isEqualTo(plaintext);

    assertThat(daead.encryptDeterministically(plaintext, associatedData)).isEqualTo(ciphertext);

    KeysetHandle otherHandle = KeysetHandle.generateNew(KeyTemplates.get("AES256_SIV"));
    DeterministicAead otherAead = otherHandle.getPrimitive(DeterministicAead.class);
    assertThrows(
        GeneralSecurityException.class,
        () -> otherAead.decryptDeterministically(ciphertext, associatedData));

    byte[] invalid = "invalid".getBytes(UTF_8);
    byte[] empty = "".getBytes(UTF_8);
    assertThrows(
        GeneralSecurityException.class, () -> daead.decryptDeterministically(ciphertext, invalid));
    assertThrows(
        GeneralSecurityException.class,
        () -> daead.decryptDeterministically(invalid, associatedData));
    assertThrows(
        GeneralSecurityException.class,
        () -> daead.decryptDeterministically(empty, associatedData));
    assertThat(
            daead.decryptDeterministically(
                daead.encryptDeterministically(empty, associatedData), associatedData))
        .isEqualTo(empty);
    assertThat(
            daead.decryptDeterministically(daead.encryptDeterministically(plaintext, empty), empty))
        .isEqualTo(plaintext);
  }
Unit tests for the Daead package. Uses only the public API.
java_src/src/test/java/com/google/crypto/tink/daead/DaeadTest.java