package com.hellogv.testIO;
import javacard.framework.APDU;
import javacard.framework.Applet;
import javacard.framework.ISO7816;
import javacard.framework.ISOException;
import javacard.framework.Util;
public class mytest extends Applet {
private static final byte[] strHello= { (byte) 'H', (byte) 'e',
(byte) 'l', (byte) 'l', (byte) 'o'};
private static final byte[] strWorld = {(byte) 'W',
(byte) 'o', (byte) 'r', (byte) 'l', (byte) 'd', };
private static final byte CMD_CLA = (byte) 0x80;
private static final byte CMD_INS_1 = (byte) 0x10;
private static final byte CMD_INS_2 = (byte) 0x20;
public static void install(byte[] bArray, short bOffset, byte bLength) {
// GP-compliant JavaCard applet registration
new mytest().register(bArray, (short) (bOffset + 1), bArray[bOffset]);
}
/*
* 当Java卡Applet被选中时,由JCRE调用。Java卡Applet可以定义select()完成初始化,
* 否则,JCRE调用父类的select()。
* @see javacard.framework.Applet#select()
*/
public boolean select() {
short debug=100;
debug++;//用于断点调试,当被select时触发。
return super.select();
}
/*
* 当Java卡Applet被放弃时,由JCRE调用。Java卡Applet可以定义deselect()完成清除,
* 否则,JCRE调用父类的deselect()。
* @see javacard.framework.Applet#deselect()
*/
public void deselect() {
short debug=100;
debug++;//用于断点调试
super.deselect();
}
/*
* 每次收到APDU命令,都会执行
* @see javacard.framework.Applet#process(javacard.framework.APDU)
*/
public void process(APDU apdu) {
if (selectingApplet()) {
return;
}
//获取外部终端发过来的数据
byte[] buffer = apdu.getBuffer();
//获取第一位数据
byte CLA = (byte) (buffer[ISO7816.OFFSET_CLA] & 0xFF);
//获取第二位数据
byte INS = (byte) (buffer[ISO7816.OFFSET_INS] & 0xFF);
if (CLA != CMD_CLA) {//格式不对
ISOException.throwIt(ISO7816.SW_CLA_NOT_SUPPORTED);
}
switch (INS) {
case CMD_INS_1:
sendBytes(apdu,strHello);
break;
case CMD_INS_2:
sendBytes(apdu,strWorld);
break;
default:
ISOException.throwIt(ISO7816.SW_INS_NOT_SUPPORTED);
}
}
private void sendBytes(APDU apdu,byte[] arrays) {
byte[] buffer = apdu.getBuffer();
short length = (short) arrays.length;
Util.arrayCopyNonAtomic(arrays, (short) 0, buffer, (short) 0,
(short) length);
apdu.setOutgoingAndSend((short) 0, length);
}
}
- 1
- 2
前往页