2
votes

I recently bought a smart card reader (Gemplus USB Smart Card Reader) which came with some cards. I've been reading several tutorials about how to work with them but I have a couple of questions. I'm trying to comunicate (Sending APDU) with the smart card but with not success. I installed an applet into a Virtual smart card (using Netbeans).

My process method is like this:

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

    if (CLA != HW_CLA){
        ISOException.throwIt(ISO7816.SW_CLA_NOT_SUPPORTED);
    }        
    switch (INS) {
        case HW_INS_ADD:               
            add(apdu);
            break;
        case HW_INS_SUBTRACT:                
            subtract(apdu);
            break;
        default:
            ISOException.throwIt(ISO7816.SW_INS_NOT_SUPPORTED);
    }

The add and subtract method adds or substract 5 units to a declared variable (balance). I would like to save into the card the balance value after making an operation. Does anyone know how can i write that value into the card? And if possible, how can i read it before?

Appreciate any help. Thank you.

3

3 Answers

2
votes

The answer is simple: Just add field to your JavaCard applet class. Once "started" Javacard applets "run" until they are deinstalled.

Of course "run" is the wrong term - they keep their internal state and if the next APDU is received the state from the APDU before is still present, even if the card was removed from smart card reader after reception of the first APDU.

2
votes

Any state within persistent (persistent memory means EEPROM or flash) fields will be kept until the Applet instance is deleted. The same goes for state within non-transient array elements. Of course, you need to retain a persistent reference to the array for this to be the case. The contents of transient (transient means RAM) CLEAR_ON_DESELECT arrays will be cleared once a context switch is made, e.g. when another Applet is selected (and the current one deselected) or when a soft or hard reset happens. Of course CLEAR_ON_RESET arrays will only be wiped on a reset (including loss of power).

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. The APDU buffer itself will always be cleared each time the process method is called.

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

Note that many Java Card simulators do not keep any state when the process containing the simulator ends. This is not a good representation of how real Java Card implementations behave.

0
votes

Actually you are saving these values :) The values which are declared Transient or local variables are not stored in the EEPROM else all values are stored in the EEPROM and will be non-volatile. You can access them when you insert your card after removal.

But from the code you have posted i cannot say what type of variables you are using. If you want a precise answer just post the complete code then we will answer you accurately.