首页 > 解决方案 > 如果缓冲区大小 >7,则 Javacard 错误 APDU

问题描述

我正在尝试使用字节数组设置 CommandAPDU 缓冲区。但是,如果长度>7,则会引发以下错误:

线程“主”java.lang.IllegalArgumentException 中的异常:APDU 无效:长度 = 8,b1 = 1 在 javax.smartcardio.CommandAPDU.parse(CommandAPDU.java:318) 在 javax.smartcardio.CommandAPDU.(CommandAPDU.java:98 ) 在终端.Main.main(Main.java:78)

我的代码:

byte terminal = 0x00;
byte instruction = 0x01;
byte [] msg = {0x01,0x00,0x01,0x00};
byte [] fullmsg = new byte[msg.length + 4];
System.arraycopy(new byte []{terminal}, 0, fullmsg, 0, 1);
System.arraycopy(new byte [] {instruction}, 0, fullmsg, 1, 1);
System.arraycopy(new byte [] {0,0}, 0, fullmsg, 2, 2);
System.arraycopy(msg, 0, fullmsg, 4, msg.length);
CommandAPDU cmdapdu = new CommandAPDU(fullmsg);

有人可以帮我吗?

标签: smartcardjavacardapdujcop

解决方案


考虑使用CommandAPDU(int cla, int ins, int p1, int p2, byte[] data)(如果您不希望从卡返回任何数据——即命令是 ISO-7816 案例 3)或CommandAPDU(int cla, int ins, int p1, int p2, byte[] data, int ne)(如果您希望从卡返回一些数据——即命令是 ISO-7816 案例 4)来创建您的 CommandAPDU 对象。

有关命令 APDU 格式的更多详细信息,请参见 ISO 7816-3 的第 12.1 节“应用程序协议数据单元” (此处部分提供)。

例如:

CommandAPDU cmdapdu = new CommandAPDU(terminal, instruction, 0, 0, msg);

或(随意将 256 替换为任何其他预期长度的响应数据):

CommandAPDU cmdapdu = new CommandAPDU(terminal, instruction, 0, 0, msg, 256);

祝你好运!


推荐阅读