APDU在方法上抛出6F00

时间:2015-09-28 04:28:34

标签: smartcard javacard apdu

我正在尝试从传入的智能卡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;
}

1 个答案:

答案 0 :(得分: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);
    

    不会做你所期望的。 setOutgoingAndSend()(就像(sendBytes())将从全局APDU缓冲区发送字节(即通过apdu.getBuffer()引用的字节数组。只需设置本地buffer变量以引用另一个字节数组(outgoingMsg)不会更改全局APDU缓冲区。而是需要将传出数据复制到APDU缓冲区(请参阅Util.arrayCopy*())。或者,您可以使用apdu.sendBytesLong()来指定包含要发送的数据的字节数组。

相关问题