1

我正在用 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());

客户端提供的参数有:

  • CLA = 00
  • INS = A4
  • P1 = 00
  • P2 = 0℃
  • LC = 02
  • Data = XXXX(这里传递的数据是文件标识符),因为我要选择EF文件所以客户端给的文件的EFID是0002
4

1 回答 1

1
CommandAPDU commandAPDU = new CommandAPDU(0x00, 0xA4, 0x00, 0x0C, new byte[]{0002},0,0x01);

不会做你期望它做的事情。

new byte[]{0002}将为您提供一个字节数组,其中一个字节的值为 2。此外,,0,0x01);(最后两个参数)将使构造函数仅从 DATA 数组中选择一个字节。所以你的 APDU 看起来像这样:

+------+------+------+------+------+------+------+
| 共轭亚油酸 | INS | P1 | P2 | LC | 数据 | 乐 |
| 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);Lc 是从 DATA 数组的大小自动推断出来的):

+------+------+------+------+------+------------+-- ----+
| 共轭亚油酸 | INS | P1 | P2 | LC | 数据 | 乐 |
| 0x00 | 0xA4 | 0x00 | 0x0C | 0x02 | 0x00 0x02 | 0x00 |
+------+------+------+------+------+------------+-- ----+

或使用

CommandAPDU commandAPDU = new CommandAPDU(0x00, 0xA4, 0x00, 0x0C, new byte[]{0, 2})

将产生以下 APDU(注意 Le 不存在(Ne = 0);Lc 是从 DATA 数组的大小自动推断出来的):

+------+------+------+------+------+------------+-- ----+
| 共轭亚油酸 | INS | P1 | P2 | LC | 数据 | 乐 |
| 0x00 | 0xA4 | 0x00 | 0x0C | 0x02 | 0x00 0x02 | --- |
+------+------+------+------+------+------------+-- ----+
于 2018-11-30T09:22:33.373 回答