Page 1 of 1

How to read and write value into smart card

Posted: Wed Dec 23, 2015 5:06 am
by ordane
I bought IFD R502 smart card reader which came with two A40CR cards. And I am trying to write my own applet and load it into my cards. But I have some questions.

As you see below, add method adds 5 units to a declared variable. I would like to save the variable value into the card after making an operation. Does anyone know how can i write that value into the card? And if possible, how can i read it?

Code: Select all

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

    if (CLA != DE_CLA){
        ISOException.throwIt(ISO7816.SW_CLA_NOT_SUPPORTED);
    }       
    switch (INS) {
        case DE_INS_ADD:               
            add(apdu);
            break;
        default:
            ISOException.throwIt(ISO7816.SW_INS_NOT_SUPPORTED);
    }

Re: How to read and write value into smart card

Posted: Wed Dec 30, 2015 11:00 pm
by mabel
Any state in EEPROM fields will be kept until the Applet instance is deleted. The same goes for state within non-transient array elements.
You need to retain a persistent reference to the array for your goal.

Basically all object instances live in persistent memory, this means any object instance created with new. This is also the case for any object created by the platform where the API does not explicitly specify that the result is transient.

If you want to store something, you need to know about the Java Card transaction mechanism, especially if your operations are not atomic.