gpt4 book ai didi

javax.smartcardio : how to send native commands to Desfire card?

转载 作者:行者123 更新时间:2023-11-30 05:52:26 33 4
gpt4 key购买 nike

我正在创建一个 java 应用程序,通过 PC/SC 非接触式读卡器和 javax.smartcardio API 与 Mifare DESFire 卡通信。我设法发送常规 ISO 7816 APDU(CLA、INS、P1-P2、Lc、命令数据、Le)。

我已阅读 Ridrix's Blog DESFire 卡(至少我使用的 EV1 版本)支持 APDU 和本地命令,其中大多数命令只有 1 个字节长。

例如,“获取版本”命令:

Command: 60
Response: af 04 01 01 00 02 18 05

我使用 SpringCard (available here) 中的 PC/SC Diag 程序测试了该命令,我得到了正确的响应。

但我无法使用 javax.smartcardio 发送此命令:此 API 似乎是为真实 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 或其他东西发送这种命令吗?

我知道可以包装这些命令,但我更愿意使用(更简单的) native 命令。

谢谢。

最佳答案

javax.smartcardio 是为使用 ISO 7816-4 命令而编写的 API。因此不可能发送“本地”命令。基本上, native 命令可以是任何东西,因此很难支持它们。

要么恢复到 JNI,要么尝试找到使用 transmitControlCommand 的东西.但恐怕没有额外的库就没有使用 DESFire 的真正方法。

我个人认为使用包裹层要容易得多。

关于javax.smartcardio : how to send native commands to Desfire card?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11685959/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com