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 2) 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.LoadPemFile("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.LoadXml(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.LoadFromString(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.