Sample code for 30+ languages & platforms
Objective-C

Encrypt a JWE with a Password (PBES2)

See more JSON Web Encryption (JWE) Examples

Demonstrates Jwe.SetPassword, which sets the PBES2 password for a recipient. The example uses PBES2-HS256+A128KW key management with AES-128-GCM content encryption.

Background. PBES2 derives a key-wrapping key from a password using a salt and iteration count, allowing password-based JWE encryption. The password should come from a secure source.

Chilkat Objective-C Downloads

Objective-C
#import <CkoJwe.h>
#import <CkoJsonObject.h>
#import <NSString.h>

BOOL success = NO;

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

//  Protected header: PBES2 password-based key management with AES-128-GCM content encryption.
CkoJsonObject *header = [[CkoJsonObject alloc] init];
[header UpdateString: @"alg" value: @"PBES2-HS256+A128KW"];
[header UpdateString: @"enc" value: @"A128GCM"];
success = [jwe SetProtectedHeader: header];
if (success == NO) {
    NSLog(@"%@",jwe.LastErrorText);
    return;
}

//  Set the PBES2 password for recipient 0.  The password should come from a secure source rather than
//  being hard-coded.
NSString *password = @"myPassword";
success = [jwe SetPassword: [NSNumber numberWithInt: 0] password: password];
if (success == NO) {
    NSLog(@"%@",jwe.LastErrorText);
    return;
}

NSString *jweCompact = [jwe Encrypt: @"This is the secret content." charset: @"utf-8"];
if (jwe.LastMethodSuccess == NO) {
    NSLog(@"%@",jwe.LastErrorText);
    return;
}

NSLog(@"%@",jweCompact);