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

Sign and encrypt an email with S/MIME

See more Email Object Examples

Use the sender’s signing PFX and the recipient’s public RSA certificate with with-attachments.eml, which has one recipient. Supply your own signing-key password from a credential store. Render to bytes to apply both S/MIME operations without connecting to SMTP.

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: @"with-attachments.eml"];
if (success != YES) {
    NSLog(@"%@",email.LastErrorText);
    return;
}

CkoCert *signer = [[CkoCert alloc] init];
success = [signer LoadPfxFile: @"signer.pfx" password: @"SIGNER_PASSWORD"];
if (success != YES) {
    NSLog(@"%@",signer.LastErrorText);
    return;
}

CkoCert *recipient = [[CkoCert alloc] init];
success = [recipient LoadFromFile: @"recipient.cer"];
if (success != YES) {
    NSLog(@"%@",recipient.LastErrorText);
    return;
}

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

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

email.SigningHashAlg = @"sha256";
email.Pkcs7CryptAlg = @"aes";
email.Pkcs7KeyLength = [NSNumber numberWithInt:256];
email.OaepPadding = YES;
email.OaepHash = @"sha256";
email.OaepMgfHash = @"sha256";
email.SendSigned = YES;
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: @"signed-encrypted.eml"];
if (success != YES) {
    NSLog(@"%@",@"Failed: rendered.WriteFile.");
    return;
}

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