Sample code for 30+ languages & platforms
Objective-C Requires Chilkat v11.0.0+

Encrypt with RSA-OAEP and AES-128-CBC

See more Email Object Examples

Use this profile when the receiver requires AES-128-CBC content encryption and RSA-OAEP with SHA-256. The SHA-256 settings apply to OAEP and its mask generation function. Use the matching decryption example to inspect the result.

Chilkat Objective-C Downloads

Objective-C
#import <CkoEmail.h>
#import <CkoCert.h>
#import <CkoMailMan.h>
#import <CkoBinData.h>

BOOL success = NO;

//  Assumes Chilkat is unlocked. See Global Unlock Sample.

CkoEmail *email = [[CkoEmail alloc] init];
success = [email LoadEml: @"message.eml"];
if (success != YES) {
    NSLog(@"%@",email.LastErrorText);
    return;
}

//  Use the recipient's public encryption certificate. Its private key stays with the recipient.
CkoCert *cert = [[CkoCert alloc] init];
success = [cert LoadFromFile: @"recipient.cer"];
if (success != YES) {
    NSLog(@"%@",cert.LastErrorText);
    return;
}

success = [email SetEncryptCert: cert];
if (success != YES) {
    NSLog(@"%@",email.LastErrorText);
    return;
}

email.Pkcs7CryptAlg = @"aes";
email.Pkcs7KeyLength = [NSNumber numberWithInt:128];
email.OaepPadding = YES;
email.OaepHash = @"sha256";
email.OaepMgfHash = @"sha256";
email.SendEncrypted = YES;
CkoMailMan *mail = [[CkoMailMan alloc] init];
CkoBinData *rendered = [[CkoBinData alloc] init];
success = [mail RenderToMimeBd: email renderedMime: rendered];
if (success != YES) {
    NSLog(@"%@",mail.LastErrorText);
    return;
}

success = [rendered WriteFile: @"oaep-encrypted.eml"];
if (success != YES) {
    NSLog(@"%@",@"Failed: rendered.WriteFile.");
    return;
}

NSLog(@"%@",@"Saved oaep-encrypted.eml.");