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
(PureBasic) Get Public Key from CSRDemonstrates how to get the public key from a CSR.
IncludeFile "CkBinData.pb" IncludeFile "CkXml.pb" IncludeFile "CkPublicKey.pb" IncludeFile "CkAsn.pb" IncludeFile "CkPem.pb" Procedure ChilkatExample() ; This requires the Chilkat API to have been previously unlocked. ; See Global Unlock Sample for sample code. pem.i = CkPem::ckCreate() If pem.i = 0 Debug "Failed to create object." ProcedureReturn EndIf ; No password is required. Pass an empty password string.. noPassword.s = "" success.i = CkPem::ckLoadPemFile(pem,"qa_data/csr/csr2.pem",noPassword) If success <> 1 Debug CkPem::ckLastErrorText(pem) CkPem::ckDispose(pem) ProcedureReturn EndIf strBase64.s = CkPem::ckGetEncodedItem(pem,"csr","","base64",0) asn.i = CkAsn::ckCreate() If asn.i = 0 Debug "Failed to create object." ProcedureReturn EndIf success = CkAsn::ckLoadEncoded(asn,strBase64,"base64") If success <> 1 Debug CkAsn::ckLastErrorText(asn) CkPem::ckDispose(pem) CkAsn::ckDispose(asn) ProcedureReturn EndIf ; Convert the ASN.1 to XML. xml.i = CkXml::ckCreate() If xml.i = 0 Debug "Failed to create object." ProcedureReturn EndIf success = CkXml::ckLoadXml(xml,CkAsn::ckAsnToXml(asn)) Debug CkXml::ckGetXml(xml) Debug "----" strModulusHex.s = CkXml::ckGetChildContent(xml,"bits") Debug "strModulusHex = " + strModulusHex Debug "----" ; We need the modulus as base64. bd.i = CkBinData::ckCreate() If bd.i = 0 Debug "Failed to create object." ProcedureReturn EndIf CkBinData::ckAppendEncoded(bd,strModulusHex,"hex") modulus64.s = CkBinData::ckGetEncoded(bd,"base64") Debug "modulus64 = " + modulus64 Debug "----" ; Build the XML for the public key. xmlPubKey.i = CkXml::ckCreate() If xmlPubKey.i = 0 Debug "Failed to create object." ProcedureReturn EndIf CkXml::setCkTag(xmlPubKey, "RSAPublicKey") CkXml::ckUpdateChildContent(xmlPubKey,"Modulus",modulus64) ; The RSA exponent will always be decimal 65537 (base64 = AQAB) CkXml::ckUpdateChildContent(xmlPubKey,"Exponent","AQAB") Debug "RSA public key as XML:" Debug CkXml::ckGetXml(xmlPubKey) Debug "----" ; Load the XML into a Chilkat public key object. pubkey.i = CkPublicKey::ckCreate() If pubkey.i = 0 Debug "Failed to create object." ProcedureReturn EndIf success = CkPublicKey::ckLoadFromString(pubkey,CkXml::ckGetXml(xmlPubKey)) If success <> 1 Debug CkPublicKey::ckLastErrorText(pubkey) CkPem::ckDispose(pem) CkAsn::ckDispose(asn) CkXml::ckDispose(xml) CkBinData::ckDispose(bd) CkXml::ckDispose(xmlPubKey) CkPublicKey::ckDispose(pubkey) ProcedureReturn EndIf ; Show the public key as PEM. preferPkcs1.i = 1 Debug CkPublicKey::ckGetPem(pubkey,preferPkcs1) CkPem::ckDispose(pem) CkAsn::ckDispose(asn) CkXml::ckDispose(xml) CkBinData::ckDispose(bd) CkXml::ckDispose(xmlPubKey) CkPublicKey::ckDispose(pubkey) ProcedureReturn EndProcedure |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.