21 October 2014

Java card Helloworld Applet

This article I will describe how to create java card hello world applet.You have to have
   1.Contact or contact reader
   2.Java card
   3.Eclipse with java card development kit.


First you have to knowledge about how the APDU commands are working in java card.This is the Apdu standard format.




In my example I used CLA value as 80 and INS value as 00 and P1 and P2 are 00,00.The requested format is 80000000.


import javacard.framework.APDU;

import javacard.framework.Applet;

import javacard.framework.ISO7816;

import javacard.framework.ISOException;

import javacard.framework.Util;

public class HelloworldApplet extends Applet {

private static final byte[] helloWorld = { (byte) 'H', (byte) 'e',

(byte) 'l', (byte) 'l', (byte) 'o', (byte) ' ', (byte) 'W',

(byte) 'o', (byte) 'r', (byte) 'l', (byte) 'd', };

private static final byte HW_CLA = (byte) 0x80;

private static final byte HW_INS = (byte) 0x00;

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();

  byte CLA = (byte) (buffer[ISO7816.OFFSET_CLA] & 0xFF);

  byte INS = (byte) (buffer[ISO7816.OFFSET_INS] & 0xFF);

if (CLA != HW_CLA) {

    ISOException.throwIt(ISO7816.SW_CLA_NOT_SUPPORTED);

}

switch (INS) {

  case HW_INS:

   getHelloWorld(apdu);

   break;

  default:

   ISOException.throwIt(ISO7816.SW_INS_NOT_SUPPORTED);

  }
             }

private void getHelloWorld(APDU apdu) {

      byte[] buffer = apdu.getBuffer();

      short length = (short) helloWorld.length;

     Util.arrayCopyNonAtomic(helloWorld, (short) 0, buffer, (short) 0,

     (short) length);

     apdu.setOutgoingAndSend((short) 0, length);

       }

}

2 comments: