Sample code for 30+ languages & platforms
DataFlex

Export a Public Key as DER into a BinData

Demonstrates the Chilkat PublicKey.GetDerBd method, which writes the public key as binary DER into a BinData. The first argument selects the representation (as for GetPem) and the second is the BinData that receives the bytes (its contents are replaced).

Note: The file paths are relative to the application's current working directory. Absolute paths may also be used. Supply the paths appropriate to your own environment.

Background: The in-memory binary export: the raw DER lands in a BinData ready to hash, transmit, or hand to another API without a temporary file. It is the BinData counterpart to the text exporters, and the first argument picks the same PKCS #1 versus SubjectPublicKeyInfo structure.

Chilkat DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoPubKey
    Boolean iPreferPkcs1
    Variant vBd
    Handle hoBd
    String sTemp1
    Integer iTemp1

    Move False To iSuccess

    //  Load a public key to export.  LoadFromFile inspects the content, so it accepts DER, PEM, XML,
    //  JWK, and other supported representations.
    Get Create (RefClass(cComChilkatPublicKey)) To hoPubKey
    If (Not(IsComObjectCreated(hoPubKey))) Begin
        Send CreateComObject of hoPubKey
    End
    Get ComLoadFromFile Of hoPubKey "qa_data/public.pem" To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoPubKey To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    //  Write the public key as binary DER into a BinData.  The 1st argument selects the representation
    //  (as for GetPem) and the 2nd is the BinData that receives the bytes (its contents are replaced).
    Move False To iPreferPkcs1
    Get Create (RefClass(cComChilkatBinData)) To hoBd
    If (Not(IsComObjectCreated(hoBd))) Begin
        Send CreateComObject of hoBd
    End
    Get pvComObject of hoBd to vBd
    Get ComGetDerBd Of hoPubKey iPreferPkcs1 vBd To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoPubKey To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComNumBytes Of hoBd To iTemp1
    Showln "Exported " iTemp1 " DER bytes."


End_Procedure