1.new->Java Card Project
2.new->Java Card Applet Class->AID Settings
编写卡内程序
public class HelloApplet extends Applet {
public static void install(byte[] bArray, short bOffset, byte bLength) {
// GP-compliant JavaCard applet registration
new HelloApplet().register(bArray, (short) (bOffset + 1),
bArray[bOffset]);
}
public void process(APDU apdu) {
// Good practice: Return 9000 on SELECT
if (selectingApplet()) {
return;
}
byte[] buf = apdu.getBuffer();
short offset = ISO7816.OFFSET_CDATA;
switch (buf[ISO7816.OFFSET_INS]) {
case (byte) 0xE2:
buf[offset++] = 0x01;
buf[offset++] = 0x02;
buf[offset++] = 0x03;
buf[offset++] = 0x04;
buf[offset++] = 0x05;
buf[offset++] = 0x06;
apdu.setOutgoingAndSend(ISO7816.OFFSET_CDATA, (short)(offset-ISO7816.OFFSET_CDATA));
break;
case (byte) 0xE3:
buf[offset++] = 0x11;
buf[offset++] = 0x12;
buf[offset++] = 0x13;
buf[offset++] = 0x14;
buf[offset++] = 0x15;
buf[offset++] = 0x16;
apdu.setOutgoingAndSend(ISO7816.OFFSET_CDATA,(short) (offset-ISO7816.OFFSET_CDATA));
break;
default:
ISOException.throwIt(ISO7816.SW_INS_NOT_SUPPORTED);
}
}
}
3.右键项目->Run Configurations...->Java Card Application右键选择new->选择新建的Applet->package Upload->aid
4.新建卡外项目java project
编写卡内程序如下:
public class TestHelloWorld {
protected static RFCSMXIO smxio = null;
@Before
public void setUp() throws Exception {
smxio = SMXIOFactory.createJDKSMXIO();
}
@Test
public void test() {
try {
byte[] aid = ByteUtil.hexToByteArray("5200413120");
RFCIOResult result = smxio.selectApplet(aid);
int offset = 0;
byte[] apdu = new byte[5];
apdu[offset] = (byte)0x80;
apdu[offset+1] = (byte)0xE2;
apdu[offset+2] = 0;
apdu[offset+3] = 0;
apdu[offset+4] = 0;
result = smxio.exchange(apdu);
System.out.println("result: "+ByteUtil.byteArrayToHex(result.getResult()));
boolean res = RFCSMXIOHelper.processCardIOResult(result);
byte[] b = result.getResult();
if(res){
System.out.println(ByteUtil.byteArrayToHex(b));
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
6.安装卡内程序
jcop debug->upload package->install applet
7.运行卡外程序
posted on 2014-01-23 11:38
Terry Zou 阅读(183)
评论(0) 编辑 收藏 所属分类:
JavaCard