@Theory
  public void createEncryptDecrypt(@FromDataPoints("templates") String templateName)
      throws Exception {
    if (TestUtil.isTsan()) {
      // KeysetHandle.generateNew is too slow in Tsan.
      return;
    }
    KeysetHandle privateHandle = KeysetHandle.generateNew(KeyTemplates.get(templateName));
    KeysetHandle publicHandle = privateHandle.getPublicKeysetHandle();

    HybridEncrypt encrypter = publicHandle.getPrimitive(HybridEncrypt.class);
    HybridDecrypt decrypter = privateHandle.getPrimitive(HybridDecrypt.class);

    byte[] plaintext = "plaintext".getBytes(UTF_8);
    byte[] contextInfo = "contextInfo".getBytes(UTF_8);
    byte[] ciphertext = encrypter.encrypt(plaintext, contextInfo);
    assertThat(decrypter.decrypt(ciphertext, contextInfo)).isEqualTo(plaintext);

    KeysetHandle otherPrivateHandle = KeysetHandle.generateNew(KeyTemplates.get(templateName));
    HybridDecrypt otherDecrypter = otherPrivateHandle.getPrimitive(HybridDecrypt.class);
    assertThrows(
        GeneralSecurityException.class, () -> otherDecrypter.decrypt(ciphertext, contextInfo));

    byte[] invalid = "invalid".getBytes(UTF_8);
    byte[] empty = "".getBytes(UTF_8);
    assertThrows(GeneralSecurityException.class, () -> decrypter.decrypt(ciphertext, invalid));
    assertThrows(GeneralSecurityException.class, () -> decrypter.decrypt(invalid, contextInfo));
    assertThrows(GeneralSecurityException.class, () -> decrypter.decrypt(empty, contextInfo));
    assertThat(decrypter.decrypt(encrypter.encrypt(empty, contextInfo), contextInfo))
        .isEqualTo(empty);
    assertThat(decrypter.decrypt(encrypter.encrypt(plaintext, empty), empty)).isEqualTo(plaintext);
  }
Unit tests for the Hybrid package. Uses only the public API.
java_src/src/test/java/com/google/crypto/tink/hybrid/HybridTest.java