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
(Swift 3,4,5...) Get Public Key from CSRDemonstrates how to get the public key from a CSR.
func chilkatTest() { // This requires the Chilkat API to have been previously unlocked. // See Global Unlock Sample for sample code. let pem = CkoPem()! // No password is required. Pass an empty password string.. var noPassword: String? = "" var success: Bool = pem.loadFile("qa_data/csr/csr2.pem", password: noPassword) if success != true { print("\(pem.lastErrorText!)") return } var strBase64: String? = pem.getEncodedItem("csr", itemSubType: "", encoding: "base64", index: 0) let asn = CkoAsn()! success = asn.loadEncoded(strBase64, encoding: "base64") if success != true { print("\(asn.lastErrorText!)") return } // Convert the ASN.1 to XML. let xml = CkoXml()! success = xml.load(asn.asnToXml()) print("\(xml.getXml()!)") print("----") var strModulusHex: String? = xml.getChildContent("bits") print("strModulusHex = \(strModulusHex!)") print("----") // We need the modulus as base64. let bd = CkoBinData()! bd.appendEncoded(strModulusHex, encoding: "hex") var modulus64: String? = bd.getEncoded("base64") print("modulus64 = \(modulus64!)") print("----") // Build the XML for the public key. let xmlPubKey = CkoXml()! xmlPubKey.tag = "RSAPublicKey" xmlPubKey.updateChildContent("Modulus", value: modulus64) // The RSA exponent will always be decimal 65537 (base64 = AQAB) xmlPubKey.updateChildContent("Exponent", value: "AQAB") print("RSA public key as XML:") print("\(xmlPubKey.getXml()!)") print("----") // Load the XML into a Chilkat public key object. let pubkey = CkoPublicKey()! success = pubkey.load(from: xmlPubKey.getXml()) if success != true { print("\(pubkey.lastErrorText!)") return } // Show the public key as PEM. var preferPkcs1: Bool = true print("\(pubkey.getPem(preferPkcs1)!)") } |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.