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.