Android™
Android™
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 Android™ Downloads
// Important: Don't forget to include the call to System.loadLibrary
// as shown at the bottom of this code sample.
package com.test;
import android.app.Activity;
import com.chilkatsoft.*;
import android.widget.TextView;
import android.os.Bundle;
public class SimpleActivity extends Activity {
private static final String TAG = "Chilkat";
// Called when the activity is first created.
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
boolean success = false;
CkPfx pfx = new CkPfx();
// 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.
String password = "myPfxPassword";
success = pfx.LoadPfxFile("qa_data/identity.pfx",password);
if (success == false) {
Log.i(TAG, pfx.lastErrorText());
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.
String jksPassword = "myJksPassword";
CkJavaKeyStore jks = new CkJavaKeyStore();
success = pfx.ToJksObj("myalias",jksPassword,jks);
if (success == false) {
Log.i(TAG, pfx.lastErrorText());
return;
}
// The Java KeyStore object can then be written to a .jks file.
success = jks.ToFile(jksPassword,"qa_output/identity.jks");
if (success == false) {
Log.i(TAG, jks.lastErrorText());
return;
}
Log.i(TAG, "Wrote a Java KeyStore with " + String.valueOf(jks.get_NumPrivateKeys()) + " private key(s).");
}
static {
System.loadLibrary("chilkat");
// Note: If the incorrect library name is passed to System.loadLibrary,
// then you will see the following error message at application startup:
//"The application <your-application-name> has stopped unexpectedly. Please try again."
}
}