Android™
Android™
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 Android™ Downloads
// Important: Don't forget to include the call to System.loadLibrary
// as shown at the bottom of this code sample.
package com.test;
import android.app.Activity;
import com.chilkatsoft.*;
import android.widget.TextView;
import android.os.Bundle;
public class SimpleActivity extends Activity {
private static final String TAG = "Chilkat";
// Called when the activity is first created.
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
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) {
Log.i(TAG, "Failed to load the JWE file.");
return;
}
success = jwe.LoadJweSb(sbJwe);
if (success == false) {
Log.i(TAG, 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) {
Log.i(TAG, "Recipient not found.");
return;
}
Log.i(TAG, "Found recipient at index " + String.valueOf(index));
}
static {
System.loadLibrary("chilkat");
// Note: If the incorrect library name is passed to System.loadLibrary,
// then you will see the following error message at application startup:
//"The application <your-application-name> has stopped unexpectedly. Please try again."
}
}