Our Online Store have the new products: RFID antenna board. Currently it can work with JC10M24R and JCOP4 card chips.
Compared with normal cards, the antenna board module has a smaller size and fixed holes, which is easy to integrate in the IOT(Internet Of Things) project.

Two questions about a simple java card applet

JavaCard Applet Development Related Questions and Answers.
Danieken
Posts: 19
Joined: Thu Sep 17, 2015 10:28 pm
Points :101
Contact:

Two questions about a simple java card applet

Post by Danieken » Tue Sep 22, 2015 4:51 am

I am a newbie to java card. I have found a simple applet code by google and i understand its function.
Here is the applet.

Code: Select all

package helloworld;

import javacard.framework.*;

public class helloworld extends Applet
{
    private static final byte[] javacard = {(byte)'J',(byte)'a',(byte)'v',(byte)'a',(byte)' ',(byte)'C',(byte)'a',(byte)'r',(byte)'d',(byte)'!',};
    private static final byte JC_CLA = (byte)0x80;
    private static final byte JC_INS = (byte)0x00;
   
   public static void install(byte[] bArray, short bOffset, byte bLength)
   {
      new helloworld().register(bArray, (short) (bOffset + 1), bArray[bOffset]);
   }

   public void process(APDU apdu)
   {
      if (selectingApplet())
      {
         return;
      }

      byte[] buf = apdu.getBuffer();
      
      byte CLA = (byte) (buf[ISO7816.OFFSET_CLA] & 0xFF);
        byte INS = (byte) (buf[ISO7816.OFFSET_INS] & 0xFF);
       
        if (CLA != JC_CLA)
        {
            ISOException.throwIt(ISO7816.SW_CLA_NOT_SUPPORTED);
        }
       
      switch (buf[ISO7816.OFFSET_INS])
      {
      case (byte)0x00:
         OutPut(apdu);
         break;
      default:
         ISOException.throwIt(ISO7816.SW_INS_NOT_SUPPORTED);
      }
   }
   
   private void OutPut( APDU apdu)
        {
        byte[] buffer = apdu.getBuffer();
        short length = (short) javacard.length;
        Util.arrayCopyNonAtomic(javacard, (short)0, buffer, (short)0, (short) length);
        apdu.setOutgoingAndSend((short)0, length);
        }
}

If you send "80 00 00 00 00", it returns "Java Card!" , as shown in the figure.

The first question:
I don't understand why the programmer used &0xFF in lines :

Code: Select all

byte CLA = (byte) (buf[ISO7816.OFFSET_CLA] & 0xFF);
byte INS = (byte) (buf[ISO7816.OFFSET_INS] & 0xFF);


The second question: Why does the programmer use the expression "+ 1"?

Code: Select all

new helloworld().register(bArray, (short) (bOffset + 1), bArray[bOffset]);

Thanks.
You do not have the required permissions to view the files attached to this post. Please login first.

User avatar
marjkbadboy
Posts: 33
Joined: Fri Jul 31, 2015 2:47 am
Points :217
Contact:

Re: Two questions about a simple java card applet

Post by marjkbadboy » Thu Oct 29, 2015 6:49 am

Q1. As far as I know, this line
byte CLA = (byte) (buffer[ISO7816.OFFSET_CLA] & 0xFF)
is equal to the line
byte CLA = buffer[ISO7816.OFFSET_CLA]
It's hunting season!

ThePhoenyx
Posts: 54
Joined: Mon Jul 20, 2015 1:08 pm
Points :510
Contact:

Re: Two questions about a simple java card applet

Post by ThePhoenyx » Sat Oct 31, 2015 12:57 pm

The "& 0xFF" is ANDing the CLA byte with FF. It's normally used as a way to isolate one bit or set of bits to see if it's set or not, such as F1 AND checkbyte == 0 would check for any of bits 1 and 5-8 being set. Why in sample you showed they used FF I'm not sure, I'd have to see more of the code.
The world is full of stories;
And sometimes they permit themselves to be told.
~Cherokee Proverb

Post Reply Previous topicNext topic

Who is online

Users browsing this forum: Google [Bot] and 46 guests

JavaCard OS : Disclaimer