Android™
Android™
Load PKCS12 / PFX and Access Contents
See more PFX/P12 Examples
Loads a PKCS12 / PFX file and iterates over the contents which include private keys and certificates.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();
// Load the PKCS12 from a file
success = pfx.LoadPfxFile("/someDir/my.p12","pfxFilePassword");
if (success == false) {
Log.i(TAG, pfx.lastErrorText());
return;
}
int numPrivateKeys = pfx.get_NumPrivateKeys();
CkPrivateKey privKey = new CkPrivateKey();
Log.i(TAG, "Private Keys:");
int i = 0;
while (i < numPrivateKeys) {
pfx.PrivateKeyAt(i,privKey);
// Do something with the private key ...
i = i + 1;
}
CkCert cert = new CkCert();
int numCerts = pfx.get_NumCerts();
Log.i(TAG, "Certs:");
i = 0;
while (i < numCerts) {
pfx.CertAt(i,cert);
Log.i(TAG, cert.subjectDN());
// If the certificate has a private key (one of the private keys within the PFX)
// then it can also be obtained via the certificate object:
if (cert.HasPrivateKey() == true) {
Log.i(TAG, "Has private key!");
success = cert.GetPrivateKey(privKey);
// ...
}
i = i + 1;
}
}
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."
}
}