2

我对 Java Card 编程非常陌生。在我的代码中使用 javax.Smartcardio 时,尝试连接到 Gemalto PCSC Java 卡时出现错误。

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

输出给出以下信息:

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

我无法弄清楚我的卡的真正问题。来自供应商的 VB API 似乎工作正常。我暂时不允许发布图片。

根据 API,

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

任何帮助表示赞赏。

4

1 回答 1

0

检查junit测试以获取更多示例

jnasmartcardio

例如,我做了如下操作并设法检测到 SMARTCART 是否已连接。

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

}

于 2015-11-25T22:11:13.333 回答