Page 1 of 1

Java Card Applet Development Guide ——Applet Demo

Posted: Mon Aug 31, 2015 10:51 pm
by product
This shows you how to create the famous "Hello World" application.

Code: Select all

//define package name.
package appletDemo;

//import using java card API interface.
import javacard.framework.*;


/*
 * Package: appletDemo
 * Filename: appletDemo.java
 * Class: appletDemo
 */
public class appletDemo extends Applet
{
   //Define the value of CLA/INS in APDU, you can also define P1, P2.
   private static final byte CLA_DEMO_TEST            = (byte)0xB0;
    private static final byte INS_DEMO_TMP1            = (byte)0x11;
    private static final byte INS_DEMO_TMP2            = (byte)0x12;
   
   //Create an instance of the Applet subclass using its constructor, and to register the instance.
   public static void install(byte[] bArray, short bOffset, byte bLength)
   {
      //Only one applet instance can be successfully registered each time the JCRE calls the Applet.install method.
      new appletDemo().register(bArray, (short) (bOffset + 1), bArray[bOffset]);
   }
   //Process the command APDU, All APDUs are received by the JCRE and preprocessed.
   public void process(APDU apdu)
   {
      //Select the Applet, through the select method, this applet is selectable, After successful selection, all APDUs are delivered to the currently selected applet via the process method.
      if (selectingApplet())
      {
         return;
      }
      //Get the APDU buffer byte array.
      byte[] buf = apdu.getBuffer();
      //Calling this method indicates that this APDU has incoming data.
      apdu.setIncomingAndReceive();
      
      //If the CLA is not equal to 0xB0(CLA_DEMO_TEST),  throw an exception.
      if(buf[ISO7816.OFFSET_CLA] != CLA_DEMO_TEST)
      {
         ISOException.throwIt(ISO7816.SW_CLA_NOT_SUPPORTED);
      }
      //Dispatch INS in APDU.
      switch (buf[ISO7816.OFFSET_INS])
      {
         //The APDU format can be "B0 11 P1 P2 Lc Data Le", such as "B0110000" or "B01101020311223300".
      case INS_DEMO_TMP1:
         SendData(apdu);
         break;
         //The APDU format can be "B0 12 P1 P2 Lc Data Le", such as "B0125566" or "B012000002112200".
      case INS_DEMO_TMP2:
         //Set 0x5555 into the 'buf' array,  the offset is 0.
         Util.setShort(buf, (short)0, (short)0x5555);
         //Send the first 2 bytes data in 'buf', the hex of JCRE sending data is "55 55 90 00".
         apdu.setOutgoingAndSend((short)0, (short)2);
         break;
      default:
         //If you don't know the INS, throw an exception.
         ISOException.throwIt(ISO7816.SW_INS_NOT_SUPPORTED);
      }
   }
   
   //Define a function named 'SendData'
   private void SendData(APDU apdu)
   {
      //Get the apdu buffer datas again.
      byte [] buffer = apdu.getBuffer();
      
      //Define a byte string with the message "Hello World"
      byte sendStr[] = {'H','E','L','L','O',' ','W','O','R','L','D'};
      short len = (short) sendStr.length;
      //Copy "Hello world" character to APDU Buffer.
      Util.arrayCopyNonAtomic(sendStr, (short)0, buffer, (short)0, (short)len);
         
      //Set the data transfer direction to outbound.
      apdu.setOutgoing();
      //Set the actual length of response data.
      apdu.setOutgoingLength(len);
      //Sends the data of APDU buffer 'buf', the length is 'len' bytes,  the offset is 0.
      apdu.sendBytes((short) 0, (short)len);
   }

}



You can also download it .

Re: Java Card Applet Development Guide ——Applet Demo

Posted: Wed Dec 02, 2015 2:48 am
by Ellisun
A very basic and interesting helloworld applet for java card beginners. :) :)

Re: Java Card Applet Development Guide ——Applet Demo

Posted: Mon Dec 07, 2015 11:05 pm
by predators
Ellisun wrote:A very basic and interesting helloworld applet for java card beginners. :) :)


Yes, the comments of code are very detailed.

Re: Java Card Applet Development Guide ——Applet Demo

Posted: Mon Feb 06, 2017 12:38 pm
by edejongh
Hi all, how do I test/call this class from the debugger or the pyApdutool?

I would like to invoke the send method?

Re: Java Card Applet Development Guide ——Applet Demo

Posted: Wed Feb 15, 2017 6:03 am
by UNKNwYSHSA
edejongh wrote:Hi all, how do I test/call this class from the debugger or the pyApdutool?

I would like to invoke the send method?


Debugger (JCIDE):
1 Launch debugger
2 /select <Applet AID>
3 /send B011000000

pyApduTool:
1 Connect to the card
2 Send APDU: 00A40400<LC><Applet AID>
3 Send APDU: B011000000

Re: Java Card Applet Development Guide ——Applet Demo

Posted: Wed Feb 15, 2017 10:42 am
by edejongh
Thanks for your response. I figured it out, but it really wasn't pretty as it's virtually impossible to debug and playing with HEX is just painful. I am now focusing on the JC 3connected edition and it's a whole world better. No RMIC stub compilation nightmares and much easier to test and demo.

Anyone know what percentage of JC 3 enabled SIM's are out ther? Just a ball park figure?

regards

ed

Re: Java Card Applet Development Guide ——Applet Demo

Posted: Wed Feb 15, 2017 10:00 pm
by UNKNwYSHSA
What is the "RMIC"?
and
"Anyone know what percentage of JC 3 enabled SIM's are out ther? Just a ball park figure?" ??

Re: Java Card Applet Development Guide ——Applet Demo

Posted: Mon Jul 23, 2018 10:37 pm
by dongho1492
Hello.
I am studying Java card technology fundamentally, and thanks to this article, I really know a lot. Again, thank you.

Re: Java Card Applet Development Guide ——Applet Demo

Posted: Mon Jul 23, 2018 10:41 pm
by dongho1492
Hello.
I am studying Java card technology fundamentally, and thanks to this article, I really know a lot. Again, thank you.