Sample code for 30+ languages & platforms
Objective-C

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 Objective-C Downloads

Objective-C
#import <CkoJwe.h>
#import <CkoStringBuilder.h>

BOOL success = NO;

CkoJwe *jwe = [[CkoJwe alloc] init];

//  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.
CkoStringBuilder *sbJwe = [[CkoStringBuilder alloc] init];
BOOL bLoaded = [sbJwe LoadFile: @"qa_data/message.jwe" charset: @"utf-8"];
if (bLoaded != YES) {
    NSLog(@"%@",@"Failed to load the JWE file.");
    return;
}

success = [jwe LoadJweSb: sbJwe];
if (success == NO) {
    NSLog(@"%@",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.
BOOL bCaseSensitive = YES;
int index = [[jwe FindRecipient: @"kid" paramValue: @"recipient-key-1" caseSensitive: bCaseSensitive] intValue];
if (index < 0) {
    NSLog(@"%@",@"Recipient not found.");
    return;
}

NSLog(@"%@%d",@"Found recipient at index ",index);