JavaCard Wiki
一个刚起步的 Java Card 知识库:先把最小的 Applet 跑通,再慢慢补齐周边。
这个站点还在起步阶段,目前只有这一页。它想先把 Java Card 的最小闭环说清楚:写一个 Applet、装到卡上、发一条 APDU、拿到响应。环境搭建、APDU 细节、状态字排错这些章节等真正需要时再加。
最小的 Applet
Section titled “最小的 Applet”一个只做一件事的 Applet:INS = 0x01 时返回 Hello World,其余指令返回 6D00。
package com.example.hello;
import javacard.framework.APDU;import javacard.framework.Applet;import javacard.framework.ISO7816;import javacard.framework.ISOException;
public class HelloWorldApplet extends Applet {
private static final byte[] HELLO = { 'H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd', };
public static void install(byte[] bArray, short bOffset, byte bLength) { new HelloWorldApplet().register(bArray, (short) (bOffset + 1), bArray[bOffset]); }
public void process(APDU apdu) { if (selectingApplet()) { return; }
byte[] buffer = apdu.getBuffer(); if (buffer[ISO7816.OFFSET_INS] != (byte) 0x01) { ISOException.throwIt(ISO7816.SW_INS_NOT_SUPPORTED); }
for (short i = 0; i < HELLO.length; i++) { buffer[i] = HELLO[i]; } apdu.setOutgoingAndSend((short) 0, (short) HELLO.length); }}假设 Applet 的 AID 是 F0 00 00 00 01:
| 步骤 | APDU | 期望结果 |
|---|---|---|
| 选择 Applet | 00 A4 04 00 05 F0 00 00 00 01 |
9000 |
| 调用命令 | 00 01 00 00 00 |
48 65 6C 6C 6F 20 57 6F 72 6C 64 90 00 |
响应末尾的 9000 是状态字,表示成功;6D00 表示这张卡不认识该 INS。