Here is my code.
Server Applet
Code: Select all
package hcl.demo.server;
import javacard.framework.AID;
import javacard.framework.APDU;
import javacard.framework.Applet;
import javacard.framework.ISO7816;
import javacard.framework.ISOException;
import javacard.framework.JCSystem;
import javacard.framework.Shareable;
import javacard.framework.Util;
public class PurseApplet extends Applet implements PersonalizeInterface {
public static byte[] client_bytes = {0x04,0x04,0x04,0x04,0x04};
private static byte[] data = {0x02,0x03,0x04,0x06,0x07,0x08,0x09,0x01};
private static byte[] rdata = null;
public static void install(byte[] bArray, short bOffset, byte bLength) {
// GP-compliant JavaCard applet registration
new PurseApplet().register(bArray, (short) (bOffset + 1),
bArray[bOffset]);
}
public Shareable getShareableInterfaceObject(AID client_AID, byte parameter)
{
AID clientAID = JCSystem.lookupAID(client_bytes, (short)0, (byte)client_bytes.length);
if(!clientAID.equals(client_AID))
{
return null;
}
getByte();
return (PersonalizeInterface)this;
}
public void process(APDU apdu) {
// Good practice: Return 9000 on SELECT
if (selectingApplet()) {
return;
}
byte[] buf = apdu.getBuffer();
switch (buf[ISO7816.OFFSET_INS]) {
case (byte) 0x00:
break;
default:
// good practice: If you don't know the INStruction, say so:
ISOException.throwIt(ISO7816.SW_INS_NOT_SUPPORTED);
}
}
public byte[] getByte() {
// TODO Auto-generated method stub
rdata = data;
return rdata;
}
}
Shareable Interface
Code: Select all
package hcl.demo.server;
import javacard.framework.Shareable;
public interface PersonalizeInterface extends Shareable{
public byte[] getByte();
}
Client Applet
Code: Select all
package hcl.demo.shareObject;
import hcl.demo.server.PersonalizeInterface;
import javacard.framework.AID;
import javacard.framework.APDU;
import javacard.framework.Applet;
import javacard.framework.ISO7816;
import javacard.framework.ISOException;
import javacard.framework.JCSystem;
import javacard.framework.Util;
public class Client extends Applet {
private static byte[] purse_AID_bytes = {0x06,0x06,0x06,0x06,0x06};
private static byte[] data = {0x02,0x03,0x04,0x06,0x07,0x08,0x09,0x01};
public static void install(byte[] bArray, short bOffset, byte bLength) {
// GP-compliant JavaCard applet registration
new Client().register(bArray, (short) (bOffset + 1), bArray[bOffset]);
}
public static void uninstall(byte[] bArray, short bOffset, byte bLength) {
// GP-compliant JavaCard applet registration
purse_AID_bytes=null;
data=null;
//new Client().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();
switch (buf[ISO7816.OFFSET_INS]) {
case (byte) 0x00:getServerByte(apdu,buf);
break;
default:
// good practice: If you don't know the INStruction, say so:
ISOException.throwIt(ISO7816.SW_INS_NOT_SUPPORTED);
}
}
public void getServerByte(APDU apdu,byte[] buff) {
// TODO Auto-generated method stub
PersonalizeInterface personalizeInterface = null;
// Obtain the server AID
AID purse_AID = JCSystem.lookupAID(purse_AID_bytes, (short)0, (byte)purse_AID_bytes.length);
if(purse_AID != null)
{
personalizeInterface = (PersonalizeInterface) (JCSystem.getAppletShareableInterfaceObject(purse_AID, (byte)0));
}
if(personalizeInterface != null)
{
Util.arrayCopy(personalizeInterface.getByte(),(short) 0, buff, (short) 0, (short)8);
apdu.setOutgoingAndSend((byte)0, (short)8);
}
else
{
ISOException.throwIt(ISO7816.SW_APPLET_SELECT_FAILED);
}
}
}