Sample code for 30+ languages & platforms
Objective-C

Set Cert and Private Key for S/MIME Decryption

See more IMAP Examples

Demonstrates the Chilkat Imap.SetDecryptCert2 method, which specifies a certificate together with a separately supplied private key for decrypting S/MIME messages. The first argument is the Cert and the second is the PrivateKey. This example loads the certificate and key from separate files.

Note: The certificate path is relative to the application's current working directory. Supply the path to your own certificate file.

Background: Sometimes the certificate (the public part) and the private key are stored separately — a .cer alongside a PEM key file, for instance. This method pairs them explicitly, whereas SetDecryptCert expects a single Cert that already provides its private key. After the pairing, subsequently downloaded S/MIME messages are decrypted automatically.

Chilkat Objective-C Downloads

Objective-C
#import <CkoImap.h>
#import <CkoCert.h>
#import <CkoPrivateKey.h>

BOOL success = NO;

//  Demonstrates the Imap.SetDecryptCert2 method, which specifies a certificate together with a
//  separately supplied private key for decrypting S/MIME messages.  The 1st argument is the
//  Cert and the 2nd is the PrivateKey.

CkoImap *imap = [[CkoImap alloc] init];

imap.Ssl = YES;
imap.Port = [NSNumber numberWithInt:993];

success = [imap Connect: @"imap.example.com"];
if (success == NO) {
    NSLog(@"%@",imap.LastErrorText);
    return;
}

success = [imap Login: @"user@example.com" password: @"myPassword"];
if (success == NO) {
    NSLog(@"%@",imap.LastErrorText);
    return;
}

//  Load the certificate and its private key from separate files.
CkoCert *cert = [[CkoCert alloc] init];
success = [cert LoadFromFile: @"qa_data/smime.cer"];
if (success == NO) {
    NSLog(@"%@",cert.LastErrorText);
    return;
}

CkoPrivateKey *privKey = [[CkoPrivateKey alloc] init];
success = [privKey LoadPemFile: @"qa_data/smime_privkey.pem"];
if (success == NO) {
    NSLog(@"%@",privKey.LastErrorText);
    return;
}

//  Use the cert and key to automatically decrypt subsequently downloaded S/MIME messages.
success = [imap SetDecryptCert2: cert key: privKey];
if (success == NO) {
    NSLog(@"%@",imap.LastErrorText);
    return;
}

success = [imap Disconnect];
if (success == NO) {
    NSLog(@"%@",imap.LastErrorText);
    return;
}