Smart Card Solution
User Manual
- R502 Manual
JavaCard API Samples
- Algorithm
Java Card Specification
Knowledge Sharing
Smart Card Solution
User Manual
JavaCard API Samples
Java Card Specification
Knowledge Sharing
This is an old revision of the document!
Download Sample Code Discussion
/* * @file Utility.java * @brief The Class of Util Sample Code in JavaCard API Specification * @comment The purpose of this example is only used to show the usage of API functions and there is no practical significance. * @copyright Copyright(C) 2016 JavaCardOS Technologies Co., Ltd. All rights reserved. */ package JavaCardOS.Sample.Utility; import javacard.framework.*; public class Utility extends Applet { public static final byte INS_ARRAY_COPY_APDU = 0x01; public static final byte INS_ARRAY_COMPARE_APDU = 0x02; public static final byte INS_SHORT_VALUE_APDU = 0x03; //Redefine the SW1,SW2 error codes short SW_DATA_CONVERT_ERROR = 0x6F80; public static void install(byte[] bArray, short bOffset, byte bLength) { new Utility().register(bArray, (short) (bOffset + 1), bArray[bOffset]); } public void process(APDU apdu) { if (selectingApplet()) { return; } //Get the apdu buffer datas byte[] buf = apdu.getBuffer(); byte ins = buf[ISO7816.OFFSET_INS]; switch (ins) { case (byte)INS_ARRAY_COPY_APDU: apdu.setIncomingAndReceive(); //Create a transient byte array with the specified array length byte[] tmpMemory = JCSystem.makeTransientByteArray((short)10, JCSystem.CLEAR_ON_DESELECT); //Fill the byte array 'tmpMemory' (non-atomically) with the value 0x66, beginning from the 0 position, the filling length is 10. Util.arrayFillNonAtomic(tmpMemory, (short)0, (short)10, (byte)0x66); //Copy an array from the 'tmpMemory' array to APDU Buffer. Util.arrayCopy(tmpMemory, (short)0, buf, (short)0, (short)10); //Send the length of 10 bytes from the APDU buffer apdu.setOutgoingAndSend((short)0, (short)10); break; case INS_ARRAY_COMPARE_APDU: //Calling this method indicates that this APDU has incoming data. apdu.setIncomingAndReceive(); //Get the incoming data length(Lc). short lc = apdu.getIncomingLength(); if (lc != 0x04) { ISOException.throwIt(ISO7816.SW_WRONG_LENGTH); } //Define a byte array named 'cmpArray' byte[] cmpArray = {(byte) 0x01, (byte) 0x02, (byte) 0x03, (byte) 0x04}; //Compare an array from the specified source array, byte cmpResult = Util.arrayCompare(cmpArray, (short)0, buf, (short)ISO7816.OFFSET_CDATA, (short)4); //if the comparison of the results is same, set the value of 'buf' byte array to "01", if not, set to "02" if (cmpResult == (byte)0) { buf[0] = (byte)1; } else { buf[0] = (byte)2; } apdu.setOutgoingAndSend((short)0, (short)1); break; case INS_SHORT_VALUE_APDU: apdu.setIncomingAndReceive(); //Get the incoming data length(Lc). short len = apdu.getIncomingLength(); if (len != 0x02) { ISOException.throwIt(ISO7816.SW_WRONG_LENGTH); } //Concatenates the two parameter bytes to form a short value. short short_value1 = Util.makeShort(buf[ISO7816.OFFSET_CDATA], buf[ISO7816.OFFSET_CDATA+1]); //Concatenates two bytes in a byte array to form a short value. short short_value2 = Util.getShort(buf, ISO7816.OFFSET_CDATA); //compare the result value that two methods converted if (short_value1 != short_value2) { ISOException.throwIt(SW_DATA_CONVERT_ERROR); } //Deposits the short value as two successive bytes at the specified offset in the byte array. Util.setShort(buf, (short)0, short_value1); apdu.setOutgoingAndSend((short)0, (short)2); break; default: ISOException.throwIt(ISO7816.SW_INS_NOT_SUPPORTED); } } }