Objective-C
Objective-C
Get Public Key from CSR
See more CSR Examples
Demonstrates how to get the public key from a CSR.Chilkat Objective-C Downloads
#import <CkoPem.h>
#import <NSString.h>
#import <CkoAsn.h>
#import <CkoXml.h>
#import <CkoBinData.h>
#import <CkoPublicKey.h>
BOOL success = NO;
// This requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
CkoPem *pem = [[CkoPem alloc] init];
// No password is required. Pass an empty password string..
NSString *noPassword = @"";
success = [pem LoadPemFile: @"qa_data/csr/csr2.pem" password: noPassword];
if (success != YES) {
NSLog(@"%@",pem.LastErrorText);
return;
}
NSString *strBase64 = [pem GetEncodedItem: @"csr" itemSubType: @"" encoding: @"base64" index: [NSNumber numberWithInt: 0]];
CkoAsn *asn = [[CkoAsn alloc] init];
success = [asn LoadEncoded: strBase64 encoding: @"base64"];
if (success != YES) {
NSLog(@"%@",asn.LastErrorText);
return;
}
// Convert the ASN.1 to XML.
CkoXml *xml = [[CkoXml alloc] init];
success = [xml LoadXml: [asn AsnToXml]];
NSLog(@"%@",[xml GetXml]);
NSLog(@"%@",@"----");
NSString *strModulusHex = [xml GetChildContent: @"bits"];
NSLog(@"%@%@",@"strModulusHex = ",strModulusHex);
NSLog(@"%@",@"----");
// We need the modulus as base64.
CkoBinData *bd = [[CkoBinData alloc] init];
[bd AppendEncoded: strModulusHex encoding: @"hex"];
NSString *modulus64 = [bd GetEncoded: @"base64"];
NSLog(@"%@%@",@"modulus64 = ",modulus64);
NSLog(@"%@",@"----");
// Build the XML for the public key.
CkoXml *xmlPubKey = [[CkoXml alloc] init];
xmlPubKey.Tag = @"RSAPublicKey";
[xmlPubKey UpdateChildContent: @"Modulus" value: modulus64];
// The RSA exponent will always be decimal 65537 (base64 = AQAB)
[xmlPubKey UpdateChildContent: @"Exponent" value: @"AQAB"];
NSLog(@"%@",@"RSA public key as XML:");
NSLog(@"%@",[xmlPubKey GetXml]);
NSLog(@"%@",@"----");
// Load the XML into a Chilkat public key object.
CkoPublicKey *pubkey = [[CkoPublicKey alloc] init];
success = [pubkey LoadFromString: [xmlPubKey GetXml]];
if (success != YES) {
NSLog(@"%@",pubkey.LastErrorText);
return;
}
// Show the public key as PEM.
BOOL preferPkcs1 = YES;
NSLog(@"%@",[pubkey GetPem: preferPkcs1]);