Sample code for 30+ languages & platforms
Objective-C

Generate RSA Public/Private Key Pair and Export to PEM

See more RSA Examples

_LANGUAGE_ example code showing how to generate an RSA public/private key pair and export to PEM files.

Chilkat Objective-C Downloads

Objective-C
#import <CkoRsa.h>
#import <CkoPrivateKey.h>
#import <CkoPublicKey.h>

BOOL success = NO;

// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.

CkoRsa *rsa = [[CkoRsa alloc] init];

// Generate a 2048-bit key.  Chilkat RSA supports
// key sizes ranging from 512 bits to 8192 bits.
CkoPrivateKey *privKey = [[CkoPrivateKey alloc] init];
success = [rsa GenKey: [NSNumber numberWithInt: 2048] privKey: privKey];
if (success == NO) {
    NSLog(@"%@",rsa.LastErrorText);
    return;
}

CkoPublicKey *pubKey = [[CkoPublicKey alloc] init];
[privKey ToPublicKey: pubKey];

// Save the private key in PEM format:
success = [privKey SavePemFile: @"privateKey.pem"];
if (success == NO) {
    NSLog(@"%@",privKey.LastErrorText);
    return;
}

// Save the public key in PEM format:
success = [pubKey SavePemFile: NO path: @"publicKey.pem"];
if (success == NO) {
    NSLog(@"%@",pubKey.LastErrorText);
    return;
}

NSLog(@"%@",@"Success.");