Sample code for 30+ languages & platforms
C

Convert PKCS12 / PFX to Java Keystore (JKS)

See more PFX/P12 Examples

Loads a PKCS12 / PFX file and saves it to a Java keystore (JKS) file.

Chilkat C Downloads

C
#include <C_CkPfx.h>
#include <C_CkJavaKeyStore.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkPfx pfx;
    const char *jksPassword;
    const char *alias;
    HCkJavaKeyStore jks;

    success = FALSE;

    // This example requires the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

    pfx = CkPfx_Create();

    // Load the PKCS12 from a file
    success = CkPfx_LoadPfxFile(pfx,"/someDir/my.p12","myPfxPassword");
    if (success == FALSE) {
        printf("%s\n",CkPfx_lastErrorText(pfx));
        CkPfx_Dispose(pfx);
        return;
    }

    jksPassword = "myJksPassword";
    alias = "firstPrivateKeyAlias";

    jks = CkJavaKeyStore_Create();

    // Convert to a Java keystore object.
    // The jksPassword is the password to be used for the JKS private key entries. 
    // It may be the same as the PFX password, but can also be different if desired.
    success = CkPfx_ToJksObj(pfx,alias,jksPassword,jks);
    if (success == FALSE) {
        printf("%s\n",CkPfx_lastErrorText(pfx));
        CkPfx_Dispose(pfx);
        CkJavaKeyStore_Dispose(jks);
        return;
    }

    // Save the Java keystore to a file.
    success = CkJavaKeyStore_ToFile(jks,jksPassword,"/myKeystores/my.jks");
    if (success != TRUE) {
        printf("%s\n",CkJavaKeyStore_lastErrorText(jks));
        CkJavaKeyStore_Dispose(jks);
        CkPfx_Dispose(pfx);
        CkJavaKeyStore_Dispose(jks);
        return;
    }

    printf("Successfully converted PFX to JKS.\n");


    CkPfx_Dispose(pfx);
    CkJavaKeyStore_Dispose(jks);

    }