Sample code for 30+ languages & platforms
PureBasic

Serialize a PFX to BinData

See more PFX/P12 Examples

Demonstrates Pfx.ToBd, which serializes the current PFX contents as PKCS#12 data into a BinData object, using the supplied output password.

Background. This is the in-memory equivalent of ToFile, producing the PKCS#12 bytes for further processing or transport.

Chilkat PureBasic Downloads

PureBasic
IncludeFile "CkBinData.pb"
IncludeFile "CkPfx.pb"

Procedure ChilkatExample()

    success.i = 0

    pfx.i = CkPfx::ckCreate()
    If pfx.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    ;  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.
    password.s = "myPfxPassword"
    success = CkPfx::ckLoadPfxFile(pfx,"qa_data/identity.pfx",password)
    If success = 0
        Debug CkPfx::ckLastErrorText(pfx)
        CkPfx::ckDispose(pfx)
        ProcedureReturn
    EndIf

    ;  Serialize the PFX contents as PKCS#12 data into a BinData, using the given output password.
    outputPassword.s = "myOutputPassword"
    bd.i = CkBinData::ckCreate()
    If bd.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkPfx::ckToBd(pfx,outputPassword,bd)
    If success = 0
        Debug CkPfx::ckLastErrorText(pfx)
        CkPfx::ckDispose(pfx)
        CkBinData::ckDispose(bd)
        ProcedureReturn
    EndIf

    Debug "Serialized " + Str(CkBinData::ckNumBytes(bd)) + " bytes of PKCS#12 data."


    CkPfx::ckDispose(pfx)
    CkBinData::ckDispose(bd)


    ProcedureReturn
EndProcedure