在 javacard 中选择小程序时,是否可以 return 一些数据以及状态字 9000?

is it possible to return some data along with the status word 9000 on selecting an applet in javacard?

我想知道是否可以在 selected 时从 javacard 小程序发回一些数据。

因为 select() 方法 return 是一个布尔值,我不知道如何从中 return 数据字节。

谁能帮我解决这个问题?当我向卡发送 select 命令时,我希望小程序 return 一个简单的字节数组以及状态字 9000(默认为成功)。

即,当我发送以下命令时

00A4040006010203040506

我想要这样的回复,

010203049000

(前四个字节是从小程序 return 编辑的数据) TIA。 提前致谢..

我猜你正在做 "if selectingApplet() then return" 的 "good practice"?您需要处理传入的 APDU 而不是简单的 return.

您可以 return 数据到 select 正常方式,但如果 select 成功,请注意 return 0x9000。

是的,可以在小程序选择期间 return 数据。

select()方法一般在小程序选择时由平台调用。你可以在这个方法中做一些逻辑,如果你想让你的小程序被选中, return true 或者 false 如果不是。调用此方法后,如果您的小程序被成功选中,平台将调用 APDU.process 方法,您可以在该方法中像处理小程序中的任何其他 APDU 命令一样处理 Select 命令。

但是,如果您需要响应数据,您的命令 APDU 应该指示一个 Le 字段。您可以将命令 APDU 更改为 00 A4 04 00 06 01 02 03 04 05 06 00 到 return 所有可用的响应数据。

至于 returning 9000,只需确保退出 APDU.process 方法而不抛出异常,或者您可以抛出 ISOException9000 价值。我更喜欢前者。

查看下面的代码

if (selectingApplet())
    {
        byte[] apduBuffer = apdu.getBuffer();
        
        apduBuffer[0] = 0x01;
        apduBuffer[1] = 0x02;
        apduBuffer[2] = 0x03;
        apduBuffer[3] = 0x04;
        
        apdu.setOutgoingAndSend((short)0, (short)4);
        return;
    }

小程序上传安装后,尝试select小程序,AID为112233445566

>> /send 00A4040006112233445566
>> 00 A4 04 00 06 11 22 33 44 55 66
<< 01 02 03 04 90 00

当select小程序

时return数据的另一种方式
private static final byte[] STACK_OVERFLOW = {'S','T','A','C','K',' ','O','V','E','R','F','L','O','W'};

if (selectingApplet())
    {
        byte[] apduBuffer = apdu.getBuffer();
        //copy array STACK_OVERFLOW to apduBuffer
        Util.arrayCopyNonAtomic(STACK_OVERFLOW,(short)0,apduBuffer,(short)0,(short)STACK_OVERFLOW.length);
        //set and set buffer with STACK_OVERFLOW array length
        apdu.setOutgoingAndSend((short)0, (short)STACK_OVERFLOW.length);
        return;
    }