Android™
Android™
Read a PFX Safe-Bag Attribute
See more PFX/P12 Examples
Demonstrates Pfx.GetSafeBagAttr, which reads a recognized safe-bag attribute from a private key or certificate inside the PFX.
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. A safe-bag attribute is metadata attached to an item inside a PKCS#12/PFX container; it does not change the certificate or private-key material. Recognized attributes include friendlyName, keyContainerName, keyName, and localKeyId.
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;
}
// Read a recognized safe-bag attribute from an item inside the PFX. The 1st argument is true for a
// private key or false for a certificate, and the 2nd is the zero-based index within that
// collection.
boolean bForPrivateKey = true;
String friendlyName = pfx.getSafeBagAttr(bForPrivateKey,0,"friendlyName");
if (pfx.get_LastMethodSuccess() == false) {
Log.i(TAG, pfx.lastErrorText());
return;
}
Log.i(TAG, "friendlyName: " + friendlyName);
}
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."
}
}