Chilkat HOME .NET Core C# Android™ AutoIt C C# C++ Chilkat2-Python CkPython Classic ASP DataFlex Delphi ActiveX Delphi DLL Go Java Lianja Mono C# Node.js Objective-C PHP ActiveX PHP Extension Perl PowerBuilder PowerShell PureBasic Ruby SQL Server Swift 2 Swift 3,4,5... Tcl Unicode C Unicode C++ VB.NET VBScript Visual Basic 6.0 Visual FoxPro Xojo Plugin
(Objective-C) Get Public Key from CSRDemonstrates how to get the public key from a CSR.
#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.