Chilkat Examples

ChilkatHOME.NET Core C#Android™AutoItCC#C++Chilkat2-PythonCkPythonClassic ASPDataFlexDelphi ActiveXDelphi DLLGoJavaLianjaMono C#Node.jsObjective-CPHP ActiveXPHP ExtensionPerlPowerBuilderPowerShellPureBasicRubySQL ServerSwift 2Swift 3,4,5...TclUnicode CUnicode C++VB.NETVBScriptVisual Basic 6.0Visual FoxProXojo Plugin

Objective-C Examples

Web API Categories

ASN.1
AWS KMS
AWS Misc
Amazon EC2
Amazon Glacier
Amazon S3
Amazon S3 (new)
Amazon SES
Amazon SNS
Amazon SQS
Async
Azure Cloud Storage
Azure Key Vault
Azure Service Bus
Azure Table Service
Base64
Bounced Email
Box
CAdES
CSR
CSV
Certificates
Cloud Signature CSC
Code Signing
Compression
DKIM / DomainKey
DNS
DSA
Diffie-Hellman
Digital Signatures
Dropbox
Dynamics CRM
EBICS
ECC
Ed25519
Email Object
Encryption
FTP
FileAccess
Firebase
GMail REST API
GMail SMTP/IMAP/POP
Geolocation
Google APIs
Google Calendar
Google Cloud SQL
Google Cloud Storage
Google Drive
Google Photos
Google Sheets
Google Tasks
Gzip
HTML-to-XML/Text
HTTP

HTTP Misc
IMAP
JSON
JSON Web Encryption (JWE)
JSON Web Signatures (JWS)
JSON Web Token (JWT)
Java KeyStore (JKS)
MHT / HTML Email
MIME
MS Storage Providers
Microsoft Graph
Misc
NTLM
OAuth1
OAuth2
OIDC
Office365
OneDrive
OpenSSL
Outlook
Outlook Calendar
Outlook Contact
PDF Signatures
PEM
PFX/P12
PKCS11
POP3
PRNG
REST
REST Misc
RSA
SCP
SCard
SFTP
SMTP
SSH
SSH Key
SSH Tunnel
ScMinidriver
Secrets
SharePoint
SharePoint Online
Signing in the Cloud
Socket/SSL/TLS
Spider
Stream
Tar Archive
ULID/UUID
Upload
WebSocket
XAdES
XML
XML Digital Signatures
XMP
Zip
curl
uncategorized

 

 

 

(Objective-C) Get Public Key from CSR

Demonstrates how to get the public key from a CSR.

Chilkat Objective-C Library Downloads

MAC OS X (Cocoa) Libs

iOS Libs

#import <CkoPem.h>
#import <NSString.h>
#import <CkoAsn.h>
#import <CkoXml.h>
#import <CkoBinData.h>
#import <CkoPublicKey.h>

// 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 = @"";
BOOL 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]);
 

© 2000-2024 Chilkat Software, Inc. All Rights Reserved.