Sample code for 30+ languages & platforms
Swift

Convert PKCS12 / PFX to Java KeyStore

See more Java KeyStore (JKS) Examples

Converts a PKCS12 / PFX file to a Java keystore (JKS) file.

Chilkat Swift Downloads

Swift

func chilkatTest() {
    var success: Bool = false

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

    let jks = CkoJavaKeyStore()!

    let pfx = CkoPfx()!

    var pfxPassword: String? = "secret"

    // Load a PKCS12 from a file.
    success = pfx.loadFile(path: "/someDir/my.p12", password: pfxPassword)
    if success != true {
        print("\(pfx.lastErrorText!)")
        return
    }

    var alias: String? = "someAlias"
    var jksPassword: String? = "jksSecret"

    // Add the PKCS12 to the empty Java keystore object:
    success = jks.addPfx(pfx: pfx, alias: alias, password: jksPassword)
    if success != true {
        print("\(jks.lastErrorText!)")
        return
    }

    // Write the Java keystore to a file:
    success = jks.toFile(password: jksPassword, path: "/jksFiles/my.jks")
    if success != true {
        print("\(jks.lastErrorText!)")
    }
    else {
        print("Successfully converted PKCS12 to JKS")
    }


}