Search code examples
appletbytesmartcardjavacardapdu

How to store data larger than 128 byte in JavaCard


I can't write data at index above 128 in byte array. code is given below.

private void Write1(APDU apdu) throws ISOException
{
    apdu.setIncomingAndReceive();
    byte[] apduBuffer = apdu.getBuffer();
    byte j = (byte)apduBuffer[4];       // Return incoming bytes lets take 160
    Buffer1 = new byte[j];              // initialize a array with size 160
    for (byte i=0; i<j; i++)
        Buffer1[(byte)i] = (byte)apduBuffer[5+i];
}

It gives me error 6F 00 (It means reach End Of file).

I am using:

  • smart card type = contact card
  • using java card 2.2.2 with jcop using apdu

Solution

  • Your code contains several problems:

    1. As already pointed out by 'pst' you are using a signed byte value which works only up to 128 - use a short instead

    2. Your are creating a new buffer Buffer1 on every call of your Write1 method. On JavaCard there is usually no automatic garbage collection - therefore memory allocation should only be done once when the app is installed. If you only want to process the data in the adpu buffer just use it from there. And if you want to copy data from one byte array into another better use javacard.framework.Util.arrayCopy(..).

    3. You are calling apdu.setIncomingAndReceive(); but ignore the return value. The return value gives you the number of bytes of data you can read.

    The following code is from the API docs and shows the common way:

    short bytesLeft = (short) (buffer[ISO7816.OFFSET_LC] & 0x00FF);
    if (bytesLeft < (short)55) ISOException.throwIt( ISO7816.SW_WRONG_LENGTH );
    short readCount = apdu.setIncomingAndReceive();
    while ( bytesLeft > 0){
    
         // process bytes in buffer[5] to buffer[readCount+4];
    
         bytesLeft -= readCount;
         readCount = apdu.receiveBytes ( ISO7816.OFFSET_CDATA );
    }