APDU在方法上抛出6F00

tho*_*olh 1 apdu smartcard javacard

我试图从传入的智能卡APDU读取数据并返回相同的传入消息,同时还将ASCII字"响应"附加到消息的前面.我正在获得一个6F00地位.我该如何修复代码?

我的代码:

private void repeat(APDU apdu) {
    byte[] buffer = apdu.getBuffer();
    apdu.setIncomingAndReceive();
    byte[] incomingMsg = getData(buffer);
    if ((short) incomingMsg.length != (short) 0) {
        apdu.setOutgoing();

        // Send back the "respond " + "<incoming message" back
        byte[] respMsg = {0x72, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x64, 0x20};
        byte[] outgoingMsg = new byte[(short) (incomingMsg.length + respMsg.length)];
        ArrayLogic.arrayCopyRepack(respMsg, (short) 0, (short) respMsg.length, outgoingMsg, (short) 0);
        ArrayLogic.arrayCopyRepack(incomingMsg, (short) 0, (short) incomingMsg.length, outgoingMsg, (short) respMsg.length);
        buffer = outgoingMsg;
        apdu.setOutgoingAndSend((short) 0, (short) outgoingMsg.length);
    }
}

private byte[] getData(byte[] buffer) {
    short msgLen = (short) (buffer[ISO7816.OFFSET_LC] & 0x00FF);
    short readPos = 5;
    short msgPos = 0;
    byte[] msg = new byte[msgLen];
    while (msgPos < msgLen) {
        msg[msgPos] = buffer[readPos];
        readPos++;
        msgPos++;
    }
    return msg;
}
Run Code Online (Sandbox Code Playgroud)

Mic*_*and 7

通常,状态字6F00表示您的代码抛出未处理的异常.在你的情况,这可以通过使用两个原因引起apdu.setOutgoing()apdu.setOutgoingAndSend().您只能切换到出站数据方向一次.因此,使用apdu.setOutgoing()apdu.setOutgoingAndSend()相互排斥.实际上,您可能只使用其中一种apdu.setOutgoing*()方法.

如果您想使用apdu.setOutgoing(),您稍后将使用apdu.sendBytes()或发送数据apdu.sendBytesLong().

代码中的其他问题

您的程序中还有其他一些严重的编码问题.

  1. 请注意,Java Card智能卡不会执行自动垃圾回收.因此,在每次调用处理代码时分配新的字节数组(并删除对它们的引用)通常会导致内存泄漏(即,即使您不再使用字节数组仍然分配,您也会耗尽卡的所有内存)参考他们).

  2. 代码

    buffer = outgoingMsg;
    apdu.setOutgoingAndSend((short) 0, (short) outgoingMsg.length);
    
    Run Code Online (Sandbox Code Playgroud)

    不会做你所期望的.setOutgoingAndSend()(就像(sendBytes())()将从全局APDU缓冲区发送字节(即从引用的字节数组开始apdu.getBuffer().简单地设置本地buffer变量以引用另一个字节数组(outgoingMsg)不会更改全局APDU缓冲区.相反,您需要复制您的传出数据进入APDU缓冲区(请参阅参考资料Util.arrayCopy*()).或者,您可以使用apdu.sendBytesLong()指定包含要发送的数据的字节数组.

  • 谢谢您的帮助.它已经解决了.在做我错过的sendBytesLong之前需要设置GoingLength. (2认同)