@Theory
  public void multipleKeysReadKeysetWithEncryptDecrypt()
      throws Exception {
    KeysetHandle privateHandle =
        TinkJsonProtoKeysetFormat.parseKeyset(
            JSON_PRIVATE_KEYSET_WITH_MULTIPLE_KEYS, InsecureSecretKeyAccess.get());
    KeysetHandle publicHandle =
        TinkJsonProtoKeysetFormat.parseKeyset(
            JSON_PUBLIC_KEYSET_WITH_MULTIPLE_KEYS, InsecureSecretKeyAccess.get());

    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);

    // Also test that decrypter can decrypt ciphertext of a non-primary key. We use
    // JSON_PUBLIC_KEYSET to create a ciphertext with the first key.
    KeysetHandle publicHandle1 =
        TinkJsonProtoKeysetFormat.parseKeyset(
            JSON_PUBLIC_KEYSET, InsecureSecretKeyAccess.get());
    HybridEncrypt encrypter1 = publicHandle1.getPrimitive(HybridEncrypt.class);
    byte[] ciphertext1 = encrypter1.encrypt(plaintext, contextInfo);
    assertThat(decrypter.decrypt(ciphertext1, contextInfo)).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