我是Java卡编程的新手。在我的代码中使用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
任何帮助都将不胜感激。
检查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");
}
}
}
}