我正在用Java读取智能卡。当我在下面执行以下代码时,该卡将因此返回6985(不满足使用条件)。
TerminalFactory factory = TerminalFactory.getDefault();
List<CardTerminal> terminals = factory.terminals().list();
System.out.println("Terminals: " + terminals);
if (terminals != null && !terminals.isEmpty()) {
// Use the first terminal
CardTerminal terminal = terminals.get(0);
// Connect with the card
Card card = terminal.connect("*");
System.out.println("card: " + card);
CardChannel channel = card.getBasicChannel();
CommandAPDU commandAPDU = new CommandAPDU(0x00, 0xA4, 0x00, 0x0C,
new byte[]{0002},0,0x01);
ResponseAPDU responseCheck = channel.transmit(commandApdu);
System.out.println(responseCheck.getSW1()+":"+responseCheck.getSW2()+":"+
commandApdu.toString());
客户端提供的参数是:
答案 0 :(得分:1)
CommandAPDU commandAPDU = new CommandAPDU(0x00, 0xA4, 0x00, 0x0C, new byte[]{0002},0,0x01);
不会按照您的预期去做。
new byte[]{0002}
将为您提供一个字节数组,其中一个字节的值为2。此外,,0,0x01);
(最后两个参数)将使构造函数仅从DATA数组中选取该字节。因此,您的APDU如下所示:
+------+------+------+------+------+------+------+ | CLA | INS | P1 | P2 | Lc | DATA | Le | | 0x00 | 0xA4 | 0x00 | 0x0C | 0x01 | 0x02 | --- | +------+------+------+------+------+------+------+
这可能不是您所期望的。您要使用new byte[]{0, 2}
吗?使用
CommandAPDU commandAPDU = new CommandAPDU(0x00, 0xA4, 0x00, 0x0C, new byte[]{0, 2}, 256)
将产生以下APDU(请注意,存在Le并将其设置为0(Ne = 256);从DATA数组的大小自动推断出Lc):
+------+------+------+------+------+-----------+------+ | CLA | INS | P1 | P2 | Lc | DATA | Le | | 0x00 | 0xA4 | 0x00 | 0x0C | 0x02 | 0x00 0x02 | 0x00 | +------+------+------+------+------+-----------+------+
或使用
CommandAPDU commandAPDU = new CommandAPDU(0x00, 0xA4, 0x00, 0x0C, new byte[]{0, 2})
将产生以下APDU(请注意,Le不存在(Ne = 0;从DATA数组的大小自动推断出Lc):
+------+------+------+------+------+-----------+------+ | CLA | INS | P1 | P2 | Lc | DATA | Le | | 0x00 | 0xA4 | 0x00 | 0x0C | 0x02 | 0x00 0x02 | --- | +------+------+------+------+------+-----------+------+