In the example below, java card ” Hello World” applet is demonstrated. The applet flow is that when a user sends a spacific APDU to the applet then it will return ‘Hello Word’ to the off-card application or CAD. Firstly, we will test it on JCOP shell and later will write a off-card application which will display word ‘Hello World’.
package helloWorldPackage;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); }}
Output on JcopShell:
First we need to select the newly installed applet => 00 A4 04 00 0E 48 65 6C 6C 6F 57 6F 72 6C 64 2E .....HelloWorld. 61 70 70 00 app. (491682 nsec) <= 90 00 .. Status: No Error Sending command with correct CLA and INS which will return Hellow World in hex format cm> /send 8000000000 => 80 00 00 00 00 ..... (556774 nsec) <= 48 65 6C 6C 6F 20 57 6F 72 6C 64 90 00 Hello World.. Status: No Error Sending APDU with wrong CLA which results in 6E00 (CLA value not support ed) cm> /send 0000000000 => 00 00 00 00 00 ..... (2417 usec) <= 6E 00 n. Status: CLA value not supported