Sample code for 30+ languages & platforms
Unicode C

Convert a PFX to a Java KeyStore

See more PFX/P12 Examples

Demonstrates Pfx.ToJksObj, which populates a JavaKeyStore with a JKS representation of the PFX, then writes it to a file.

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.

Background. One private-key entry is created for each private key, with certificate chains built from the PFX certificates. The password protects the generated Java KeyStore.

Chilkat Unicode C Downloads

Unicode C
#include <C_CkPfxW.h>
#include <C_CkJavaKeyStoreW.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkPfxW pfx;
    const wchar_t *password;
    const wchar_t *jksPassword;
    HCkJavaKeyStoreW jks;

    success = FALSE;

    pfx = CkPfxW_Create();

    //  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 = L"myPfxPassword";
    success = CkPfxW_LoadPfxFile(pfx,L"qa_data/identity.pfx",password);
    if (success == FALSE) {
        wprintf(L"%s\n",CkPfxW_lastErrorText(pfx));
        CkPfxW_Dispose(pfx);
        return;
    }

    //  Populate a Java KeyStore (JKS) representation of this PFX.  One private-key entry is created for
    //  each private key, and the PFX certificates are used to build the certificate chains.  The 1st
    //  argument is the alias for the first private-key entry, and the 2nd protects the generated JKS.
    //  The JKS password should come from a secure source rather than being hard-coded.
    jksPassword = L"myJksPassword";
    jks = CkJavaKeyStoreW_Create();
    success = CkPfxW_ToJksObj(pfx,L"myalias",jksPassword,jks);
    if (success == FALSE) {
        wprintf(L"%s\n",CkPfxW_lastErrorText(pfx));
        CkPfxW_Dispose(pfx);
        CkJavaKeyStoreW_Dispose(jks);
        return;
    }

    //  The Java KeyStore object can then be written to a .jks file.
    success = CkJavaKeyStoreW_ToFile(jks,jksPassword,L"qa_output/identity.jks");
    if (success == FALSE) {
        wprintf(L"%s\n",CkJavaKeyStoreW_lastErrorText(jks));
        CkPfxW_Dispose(pfx);
        CkJavaKeyStoreW_Dispose(jks);
        return;
    }

    wprintf(L"Wrote a Java KeyStore with %d private key(s).\n",CkJavaKeyStoreW_getNumPrivateKeys(jks));


    CkPfxW_Dispose(pfx);
    CkJavaKeyStoreW_Dispose(jks);

    }