【发布时间】:2012-07-27 10:25:41
【问题描述】:
我正在创建一个通过 PC/SC 非接触式读卡器和 javax.smartcardio API 与 Mifare DESFire 卡通信的 java 应用程序。我设法发送常规 ISO 7816 APDU(CLA、INS、P1-P2、Lc、命令数据、Le)。
我在Ridrix's Blog 上读到 DESFire 卡(至少我正在使用的 EV1 版本)支持 APDU 和 本机命令,其中大多数命令只有 1 个字节长。 p>
例如,“获取版本”命令:
Command: 60
Response: af 04 01 01 00 02 18 05
我使用 SpringCard (available here) 的 PC/SC Diag 程序测试了该命令,得到了正确的响应。
但我无法使用 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)
我尝试了发送命令的唯一(AFAIK)其他方式:
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 或其他方式发送此类命令的任何方法吗?
我知道可以包装这些命令,但我更喜欢使用(更简单的)本机命令。
谢谢。
【问题讨论】:
标签: java native smartcard mifare apdu