Objective-C
Objective-C
Encrypt a JWE with a Recipient Public Key
See more JSON Web Encryption (JWE) Examples
Demonstrates Jwe.SetPublicKey, which sets the public key used to encrypt for a recipient. The example uses RSA-OAEP-256 key management with AES-256-GCM content encryption.
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. In public-key JWE, the content-encryption key is wrapped for the recipient using their public key, so only the holder of the matching private key can decrypt.
Chilkat Objective-C Downloads
#import <CkoJwe.h>
#import <CkoJsonObject.h>
#import <CkoPublicKey.h>
#import <NSString.h>
BOOL success = NO;
CkoJwe *jwe = [[CkoJwe alloc] init];
// Protected header: RSA-OAEP-256 key management with AES-256-GCM content encryption.
CkoJsonObject *header = [[CkoJsonObject alloc] init];
[header UpdateString: @"alg" value: @"RSA-OAEP-256"];
[header UpdateString: @"enc" value: @"A256GCM"];
success = [jwe SetProtectedHeader: header];
if (success == NO) {
NSLog(@"%@",jwe.LastErrorText);
return;
}
// 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 the recipient's public key.
CkoPublicKey *pubKey = [[CkoPublicKey alloc] init];
success = [pubKey LoadFromFile: @"qa_data/recipient_pubkey.pem"];
if (success == NO) {
NSLog(@"%@",pubKey.LastErrorText);
return;
}
// Set the public key used to encrypt for recipient 0.
success = [jwe SetPublicKey: [NSNumber numberWithInt: 0] pubKey: pubKey];
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);