2

I am extremely new to Java Card programming. While using javax.Smartcardio in my code, I am getting an error while trying to connect to Gemalto PCSC Java card.

import java.util.List;
import javax.smartcardio.*;

public class App 
{
   public static void main(String[] args) {
  try {
   // Display the list of terminals
   TerminalFactory factory = TerminalFactory.getDefault();
   List<CardTerminal> terminals = factory.terminals().list();
   System.out.println("Terminals: " + terminals);

   // Use the first terminal
   CardTerminal terminal = terminals.get(0);

   if (terminal.isCardPresent()) {
            System.out.println("Card present!");
        }

   // Connect with the card

   Card card = terminal.connect("*");
   System.out.println("card: " + card);
   CardChannel channel = card.getBasicChannel();

   // Send Select Applet command
   byte[] aid = {(byte)0xA0, 0x00, 0x00, 0x00, 0x62, 0x03, 0x01, 0x0C, 0x06, 0x01};
  ResponseAPDU answer = channel.transmit(new CommandAPDU(0x00, 0xA4, 0x04, 0x00, aid));
  System.out.println("answer: " + answer.toString());

  // Send test command
  answer = channel.transmit(new CommandAPDU(0x00, 0x00, 0x00, 0x00));
   System.out.println("answer: " + answer.toString());
   byte r[] = answer.getData();
   for (int i=0; i<r.length; i++)
    System.out.print((char)r[i]);
    System.out.println();

   // Disconnect the card
   card.disconnect(false);
  } catch(Exception e) {
   System.out.println("Ouch: " + e.toString());
  }
 }
}

The output gives the following:

Terminals: [PC/SC terminal Gemplus USB Smart Card Reader 0]
Gemplus USB Smart Card Reader 0
Card present!
Ouch: javax.smartcardio.CardException: connect() failed

I am not able to figure out the real issue with my card. The VB API that has come from vendor seems to be working fine. I am not allowed to post image at the moment.

As per API,

ATR: A2 13 10 91   
APDU Send Data: 20 12 01 01 00  
APDU Reply Data: A2 13 10 91 90 00  

Any help is appreciated.

Mathias Müller
  • 22,203
  • 13
  • 58
  • 75
rahul kumar
  • 23
  • 1
  • 5
  • According to [TerminalImpl source](http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/7u40-b43/sun/security/smartcardio/TerminalImpl.java/#84), the CardException’s cause should be a PCSCException. Try printing e.getCause() as well so we know what error code was given. Also, it may be helpful to view the debug log of the PCSC daemon if it is available. See Ludovic Rousseau’s [instructions for Linux](http://ludovicrousseau.blogspot.com/2011/07/pcscd-debug-output.html) and [instructions for OSX](http://ludovicrousseau.blogspot.com/2011/07/pcscd-debug-output-on-mac-os-x.html). – yonran Feb 03 '14 at 17:47
  • Thanks for the clue. The getCause() gives- SCARD_W_UNRESPONSIVE_CARD error. I believe my card is dead. I have asked for a replacement. I will update once I am through. – rahul kumar Feb 04 '14 at 22:45
  • Did you solve this issue? Unfortunately I don't see any hint in the information you've (well) provided to be able to answer it. – Maarten Bodewes Feb 11 '14 at 11:20
  • Yes, this issue got resolved after replacing my smartcard. Thanks. – rahul kumar Feb 16 '14 at 23:13

1 Answers1

0

Check junit test for more example

jnasmartcardio

For example I did as follows and manages to detect whether the SMARTCART is connected.

public class Prueba {

public static void main(String[] args) throws Exception
{        
    TerminalFactory context;

    Security.addProvider(new Smartcardio());
    context = TerminalFactory.getInstance("PC/SC", null, Smartcardio.PROVIDER_NAME);

    // Display the list of terminals
    List<CardTerminal> terminals_list = context.terminals().list();

    if (terminals_list.isEmpty())
    {
        System.err.println("No existen dispositivos conectados...");
        return;
    }

    // Use the first terminal
    CardTerminal terminal = terminals_list.get(0);

    if (terminal.isCardPresent())
    {
        System.out.println("La tarjeta electronica esta presente!: "+terminal.getName());
    }
    else
    {
        System.out.println("La tarjeta electronica NO esta presente");
    }
}
}

}

Ronald Coarite
  • 4,460
  • 27
  • 31