Sample code for 30+ languages & platforms
Xbase++ Requires Chilkat v11.0.0+

Export Digital Certificate's Public Key

See more Certificates Examples

The ExportPublicKey method can be called to get a certificate's public key. It can then be saved to any of a number of formats: (1) OpenSSL DER, (2) OpenSSL PEM, (3) RSA DER, (4) XML.

Chilkat Xbase++ Downloads

Xbase++
LOCAL nSuccess
LOCAL oCert
LOCAL oPubkey

nSuccess := 0

oCert := CreateObject("Chilkat.Cert")

//  LoadFromFile will load virtually any certificate format file.
//  It will auto-recognize the format and load appropiately.
nSuccess := oCert:LoadFromFile("/Users/chilkat/testData/cer/chilkat.cer")
IF (nSuccess == 0)
    ? oCert:LastErrorText
    oCert:destroy()
    RETURN
ENDIF

//  Get the public key:
oPubkey := CreateObject("Chilkat.PublicKey")
oCert:GetPublicKey(oPubkey)

//  Save to various formats:
nSuccess := oPubkey:SaveDerFile(0, "/Users/chilkat/testData/pubkeys/chilkat_pkcs8.der")
IF (nSuccess != 1)
    ? oPubkey:LastErrorText
    oCert:destroy()
    oPubkey:destroy()
    RETURN
ENDIF

nSuccess := oPubkey:SavePemFile(0, "/Users/chilkat/testData/pubkeys/chilkat.pem")
IF (nSuccess != 1)
    ? oPubkey:LastErrorText
    oCert:destroy()
    oPubkey:destroy()
    RETURN
ENDIF

nSuccess := oPubkey:SaveDerFile(1, "/Users/chilkat/testData/pubkeys/chilkat_pkcs1.der")
IF (nSuccess != 1)
    ? oPubkey:LastErrorText
    oCert:destroy()
    oPubkey:destroy()
    RETURN
ENDIF

nSuccess := oPubkey:SaveXmlFile("/Users/chilkat/testData/pubkeys/chilkat.xml")
IF (nSuccess != 1)
    ? oPubkey:LastErrorText
    oCert:destroy()
    oPubkey:destroy()
    RETURN
ENDIF

? "Public key exported to all file formats."

oCert:destroy()
oPubkey:destroy()