Sample code for 30+ languages & platforms
Android™

Open Smartcard Certificate Store (or from USB Token)

See more Certificates Examples

Demonstrates how to open the certificate store of the smart card currently in the reader (or the USB token). Iterates over the certs found on the smartcard.

Chilkat Android™ Downloads

Android™
// 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;

    CkCertStore certStore = new CkCertStore();

    // Access the certificates on the smart card or USB token via the Chilkat certificate store class.
    // Note: Always pass the empty string to OpenSmartcard.

    // ---------------------------------------------------------------------------------------------------------
    // The following is true only for Chilkat v10.1.1 and earlier:
    // Also, the Chilkat CertStore class can only use MS CNG or CryptoAPI.
    // Some smartcard/USB token drivers only support PKCS11 or ScMinidriver.
    // You may get better results using Chilkat.Cert.LoadFromSmartcard because
    // Cert.LoadFromSmartcard can automatically detect and utilize PKCS11, ScMinidriver, CNG, and CryptoAPI.
    // ---------------------------------------------------------------------------------------------------------
    // Starting in Chilkat versions after v10.1.1, OpenSmartcard also works with
    // Apple Keychain and PKCS11 drivers on Windows, Linux, and MacOS.
    // ---------------------------------------------------------------------------------------------------------

    success = certStore.OpenSmartcard("");
    if (success == false) {
        Log.i(TAG, certStore.lastErrorText());
        return;
        }

    Log.i(TAG, certStore.lastErrorText());

    // Iterate over certificates on the smartcard.
    CkCert cert = new CkCert();
    int i = 0;
    int numCerts = certStore.get_NumCertificates();
    Log.i(TAG, "numCerts = " + String.valueOf(numCerts));

    while ((i < numCerts)) {
        certStore.GetCert(i,cert);
        Log.i(TAG, cert.subjectCN());
        Log.i(TAG, cert.serialNumber());
        if (cert.IsRsa() == true) {
            Log.i(TAG, "key type is RSA");
            }

        if (cert.IsEcdsa() == true) {
            Log.i(TAG, "key type is ECDSA");
            }

        Log.i(TAG, "has private key: " + String.valueOf(cert.HasPrivateKey()));
        Log.i(TAG, "----");
        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."
  }
}