Sample code for 30+ languages & platforms
PureBasic

Get Public Key from CSR

See more CSR Examples

Demonstrates how to get the public key from a CSR.

Chilkat PureBasic Downloads

PureBasic
IncludeFile "CkBinData.pb"
IncludeFile "CkXml.pb"
IncludeFile "CkPublicKey.pb"
IncludeFile "CkAsn.pb"
IncludeFile "CkPem.pb"

Procedure ChilkatExample()

    success.i = 0

    ; 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 = 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