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_linoleic = ( 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 ){ If (Selectingapplet ()){ Return ;} Byte [] Buffer = APDU. getbuffer (); Byte Linoleic = ( Byte ) (Buffer [iso7816.offset _ linoleic] & 0xff ); Byte INS = ( Byte ) (Buffer [iso7816.offset _ ins] & 0xff ); If (CIA! = Hw_linoleic) {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 ){ 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 6f72 6c 64 2e ...... helloworld. 61 70 70 00 App .( 491682 Nsec) <= 90 00 .. Status: no error sending command With Correct CIAAnd 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 linoleic which results In 6e00 (linoleic value not support ed) cm>/send 0000000000 => 00 00 00 00 00 .....( 2417 USEC) <= 6e 00 N. Status: linoleic value not supported