Sample code for 30+ languages & platforms
Java

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 Java Downloads

Java
import com.chilkatsoft.*;

public class ChilkatExample {

  static {
    try {
        System.loadLibrary("chilkat");
    } catch (UnsatisfiedLinkError e) {
      System.err.println("Native code library failed to load.\n" + e);
      System.exit(1);
    }
  }

  public static void main(String argv[])
  {
    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) {
        System.out.println(certStore.lastErrorText());
        return;
        }

    System.out.println(certStore.lastErrorText());

    // Iterate over certificates on the smartcard.
    CkCert cert = new CkCert();
    int i = 0;
    int numCerts = certStore.get_NumCertificates();
    System.out.println("numCerts = " + numCerts);

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

        if (cert.IsEcdsa() == true) {
            System.out.println("key type is ECDSA");
            }

        System.out.println("has private key: " + cert.HasPrivateKey());
        System.out.println("----");
        i = i+1;
        }
  }
}