我正在创建一个 java 应用程序,通过 PC/SC 非接触式读卡器和 javax.smartcardio API 与 Mifare DESFire 卡进行通信。我设法发送常规 ISO 7816 APDU(CLA、INS、P1-P2、Lc、命令数据、Le)。
我已经读过里德里克斯的博客DESFire 卡(至少是我使用的 EV1 版本)支持 APDU 和本机命令其中大多数命令只有 1 个字节长。
例如,“获取版本“ 命令:
Command: 60
Response: af 04 01 01 00 02 18 05
我用以下命令测试了该命令PC/SC 诊断节目来自春卡 (可以在这里找到),我得到了正确的回应。
但我无法使用 javax.smartcardio 发送此命令:此 API 似乎是为创建的real因此 APDU 不允许 1 字节长的命令。
这是我所做的:
public static void main(String[] args){
TerminalFactory factory = TerminalFactory.getDefault();
CardTerminals terminalList = factory.terminals();
try {
CardTerminal ct = terminalList.list().get(0);
ct.waitForCardPresent(0);
Card card = ct.connect("*");
CardChannel channel = card.getBasicChannel();
byte[] command = { 0x60 };
channel.transmit(new CommandAPDU(command));
} catch (CardException e) {
e.printStackTrace();
}
}
它给了我以下错误:
Exception in thread "main" java.lang.IllegalArgumentException: apdu must be at least 4 bytes long
at javax.smartcardio.CommandAPDU.parse(Unknown Source)
at javax.smartcardio.CommandAPDU.<init>(Unknown Source)
我尝试了唯一的(据我所知)其他方式发送命令:
ByteBuffer command = ByteBuffer.allocate(1);
command.put((byte) 0x60);
ByteBuffer response = ByteBuffer.allocate(512);
channel.transmit(command, response);
并得到类似的错误:
Exception in thread "main" java.lang.IllegalArgumentException: Command APDU must be at least 4 bytes long
at sun.security.smartcardio.ChannelImpl.checkManageChannel(Unknown Source)
at sun.security.smartcardio.ChannelImpl.doTransmit(Unknown Source)
at sun.security.smartcardio.ChannelImpl.transmit(Unknown Source)
您知道使用 javax.smartcardio 或其他方式发送此类命令的任何方法吗?
我知道有可能wrap这些命令,但我更喜欢使用(更简单的)本机命令。
Thanks.