Java
Java
Find a JWE Recipient by Header Value
See more JSON Web Encryption (JWE) Examples
Demonstrates Jwe.FindRecipient, which searches the per-recipient unprotected headers for a member with a given name and string value, and returns the matching recipient index.
The file path is relative to the application's current working directory. An absolute path may also be used. Supply the path appropriate to your own environment.
Background. This locates the recipient that a particular key belongs to (for example by
kid) before decrypting. The method returns -1 when no match is found.Chilkat Java Downloads
import com.chilkatsoft.*;
public class ChilkatExample {
static {
try {
System.loadLibrary("chilkat");
} catch (UnsatisfiedLinkError e) {
System.err.println("Native code library failed to load.\n" + e);
System.exit(1);
}
}
public static void main(String argv[])
{
boolean success = false;
CkJwe jwe = new CkJwe();
// The file path is relative to the application's current working directory. An absolute path
// may also be used. Supply the path appropriate to your own environment.
// Load a multi-recipient JWE (JSON serialization) from a file.
CkStringBuilder sbJwe = new CkStringBuilder();
boolean bLoaded = sbJwe.LoadFile("qa_data/message.jwe","utf-8");
if (bLoaded != true) {
System.out.println("Failed to load the JWE file.");
return;
}
success = jwe.LoadJweSb(sbJwe);
if (success == false) {
System.out.println(jwe.lastErrorText());
return;
}
// Search the per-recipient unprotected headers for a member named "kid" whose value equals the
// given string. Returns the recipient index, or -1 if not found.
boolean bCaseSensitive = true;
int index = jwe.FindRecipient("kid","recipient-key-1",bCaseSensitive);
if (index < 0) {
System.out.println("Recipient not found.");
return;
}
System.out.println("Found recipient at index " + index);
}
}