Smartcardio.Terminal connection error in PCSC Gemplus Javacard

99封情书 提交于 2019-12-02 03:33:31

问题


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.


回答1:


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");
    }
}
}

}



来源:https://stackoverflow.com/questions/21511312/smartcardio-terminal-connection-error-in-pcsc-gemplus-javacard

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!