使用javax.smartcardio阅读NFC Mifare Ultralight卡

时间:2017-03-14 06:42:14

标签: java nfc smartcard mifare apdu

我尝试用ACR1252U和javax.smartcardio Java库以这种方式阅读NFC Mifare Ultralight卡(第4页):

TerminalFactory factory = TerminalFactory.getDefault();
List<CardTerminal> terminals = factory.terminals().list();
System.out.println("Terminals: " + terminals);

CardTerminal terminal = terminals.get(0);

System.out.println("Waiting for a card..");

if (terminal == null)
    return;
terminal.waitForCardPresent(0);

Card card = terminal.connect("T=1");
System.out.println("Card: " + card);
System.out.println("ATR: " + bytesToHex(card.getATR().getBytes()));
System.out.println("Protocol: " + card.getProtocol());

CardChannel channel = card.getBasicChannel();

CommandAPDU command = new CommandAPDU(new byte[]{(byte) 0xFF, 
    (byte) 0xB0, (byte) 0x00, (byte) 0x04, (byte) 0x04});
ResponseAPDU response = channel.transmit(command);

if (response.getSW1() == 0x90) {
    // success command
    byte[] data = response.getData();
    System.out.println(new String(data));
}

有时它有效,有时不用(使用同一张卡)

当阅读工作时,我得到这些值:

  • ATR = 0x3B8F8001804F0CA0000003060300030000000068
  • SW1 = 0x90
  • SW2 = 0x00

当它不起作用时,我会得到这些:

  • ATR = 0x3B80800101
  • SW1 = 0x63
  • SW2 = 0x00

知道我做错了吗?

1 个答案:

答案 0 :(得分:2)

第二种情况下的ATR 3B80800101(它不起作用)表示读卡器没有检测到(或没有正确检测到)卡。 ACR1252U似乎模拟此ATR只允许连接PC / SC API(例如javax.smartcardio),即使没有实际存在卡。如果读者明确表示不存在卡,则无法做到这一点。

更可靠的方法来检查读卡器是否检测到您的卡(并将其识别为MIFARE Ultralight)将根据PC / SC规范解析ATR(请参阅有关非接触式存储卡的ATR部分):

3B 8F 80 01 80 4F 0C A000000306 03 0003 00000000 68
                     |          |  |    |
                     |          |  |    \--> RR = reserved for future use
                     |          |  \-------> NN = MIFARE Ultralight
                     |          \----------> SS = ISO 14443 Type A part 3
                     \---------------------> PC/SC RID