Sample code for 30+ languages & platforms
DataFlex

Export Selected PFX Contents as PEM

See more PFX/P12 Examples

Demonstrates Pfx.ToPemEx, which exports selected PFX contents as PEM-formatted text with control over which parts are included and how private keys are encrypted.

Background. Private keys are written in PKCS #8 form. Supported key-encryption algorithms include aes128, aes192, aes256, and 3des; leaving the algorithm empty produces unencrypted keys.

Chilkat DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoPfx
    String sPassword
    Boolean iBExtendedAttrs
    Boolean iBNoKeys
    Boolean iBNoCerts
    Boolean iBNoCaCerts
    String sKeyPassword
    String sPem
    String sTemp1
    Boolean bTemp1

    Move False To iSuccess

    Get Create (RefClass(cComChilkatPfx)) To hoPfx
    If (Not(IsComObjectCreated(hoPfx))) Begin
        Send CreateComObject of hoPfx
    End

    //  The file path is relative to the application's current working directory.  An absolute path
    //  may also be used.  Supply the path appropriate to your own environment.
    //  The PFX password should come from a secure source rather than being hard-coded.
    Move "myPfxPassword" To sPassword
    Get ComLoadPfxFile Of hoPfx "qa_data/identity.pfx" sPassword To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoPfx To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    //  Export selected PFX contents as PEM-formatted text.  The boolean arguments control what is
    //  included: extended attributes, and whether to omit private keys, certificates, or CA certificates.
    Move False To iBExtendedAttrs
    Move False To iBNoKeys
    Move False To iBNoCerts
    Move False To iBNoCaCerts

    //  Encrypt the exported private keys.  Supported algorithms include aes128, aes192, aes256, and 3des;
    //  leave the algorithm empty for unencrypted keys.  The key password should come from a secure source.
    Move "myKeyPassword" To sKeyPassword
    Get ComToPemEx Of hoPfx iBExtendedAttrs iBNoKeys iBNoCerts iBNoCaCerts "aes256" sKeyPassword To sPem
    Get ComLastMethodSuccess Of hoPfx To bTemp1
    If (bTemp1 = False) Begin
        Get ComLastErrorText Of hoPfx To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Showln sPem


End_Procedure