Page 1 of 1

Array of arrays in javacard

Posted: Sun May 21, 2017 8:15 pm
by Penneke
Hi, I need to save several messages into my Javacard (lets say 40 or 50 of 500 bytes aprox. variable sized -less than 30KB in total-). After been verified by PIN I need to randomly response one of the messages that hasn't been already used so that I thougth i could store all byte array messages in another array or structure to manage them.
Is it possible in Javacard and how should i do this?

If not and I need to create 40 or 50 byte array variables... Is it possible to save a reference to each array or something to let me choose one of them randomly or refer to it?

Thanks in advance.

Re: Array of arrays in javacard

Posted: Tue May 23, 2017 3:34 am
by UNKNwYSHSA
Refer to code following:

Code: Select all

package testObjectArray;

import javacard.framework.*;

public class testObjectArray extends Applet
{
   Object[] messages;
   
   testObjectArray() {
      messages = new Object[3];
      messages[0] = new byte[] { 'w', 'w', 'w', '.', 'J', 'a', 'v', 'a', 'C', 'a', 'r', 'd', 'O', 'S', '.', 'c', 'o', 'm' };
      messages[1] = new byte[] { 'J', 'a', 'v', 'a', 'C', 'a', 'r', 'd', 'O', 'S', '@', 'g', 'm', 'a', 'i', 'l', '.', 'c', 'o', 'm' };
      messages[2] = new byte[] { 'J', 'a', 'v', 'a', 'C', 'a', 'r', 'd', 'F', 'o', 'r', 'u', 'm' };
      
   }
   
   public static void install(byte[] bArray, short bOffset, byte bLength)
   {
      new testObjectArray().register(bArray, (short) (bOffset + 1), bArray[bOffset]);
   }

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

      byte[] buf = apdu.getBuffer();
      switch (buf[ISO7816.OFFSET_INS])
      {
      case (byte) 0x00:
         JCSystem.requestObjectDeletion();
         break;
      case (byte) 0x01:
         short message_index = (short) (buf[ISO7816.OFFSET_P1] & 0xFF);
         if (message_index >= (short) messages.length) {
            ISOException.throwIt(ISO7816.SW_INCORRECT_P1P2);
         }
         byte[] message = (byte[]) messages[message_index];
         apdu.setOutgoing();
         apdu.setOutgoingLength((short) message.length);
         apdu.sendBytesLong(message, (short) 0, (short) message.length);
         break;
      default:
         ISOException.throwIt(ISO7816.SW_INS_NOT_SUPPORTED);
      }
   }

}

Re: Array of arrays in javacard

Posted: Tue May 23, 2017 3:59 pm
by Penneke
That's exactly what I was looking for. I'm very grateful :D

Re: Array of arrays in javacard

Posted: Thu May 25, 2017 4:54 am
by UNKNwYSHSA
Penneke wrote:That's exactly what I was looking for. I'm very grateful :D

:D