Sample code for 30+ languages & platforms
DataFlex

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 DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoCert
    Variant vPubkey
    Handle hoPubkey
    String sTemp1

    Move False To iSuccess

    Get Create (RefClass(cComChilkatCert)) To hoCert
    If (Not(IsComObjectCreated(hoCert))) Begin
        Send CreateComObject of hoCert
    End

    // LoadFromFile will load virtually any certificate format file.
    // It will auto-recognize the format and load appropiately.
    Get ComLoadFromFile Of hoCert "/Users/chilkat/testData/cer/chilkat.cer" To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoCert To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    // Get the public key:
    Get Create (RefClass(cComChilkatPublicKey)) To hoPubkey
    If (Not(IsComObjectCreated(hoPubkey))) Begin
        Send CreateComObject of hoPubkey
    End
    Get pvComObject of hoPubkey to vPubkey
    Get ComGetPublicKey Of hoCert vPubkey To iSuccess

    // Save to various formats:
    Get ComSaveDerFile Of hoPubkey False "/Users/chilkat/testData/pubkeys/chilkat_pkcs8.der" To iSuccess
    If (iSuccess <> True) Begin
        Get ComLastErrorText Of hoPubkey To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComSavePemFile Of hoPubkey False "/Users/chilkat/testData/pubkeys/chilkat.pem" To iSuccess
    If (iSuccess <> True) Begin
        Get ComLastErrorText Of hoPubkey To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComSaveDerFile Of hoPubkey True "/Users/chilkat/testData/pubkeys/chilkat_pkcs1.der" To iSuccess
    If (iSuccess <> True) Begin
        Get ComLastErrorText Of hoPubkey To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComSaveXmlFile Of hoPubkey "/Users/chilkat/testData/pubkeys/chilkat.xml" To iSuccess
    If (iSuccess <> True) Begin
        Get ComLastErrorText Of hoPubkey To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Showln "Public key exported to all file formats."


End_Procedure