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.
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_DESELECTarrays 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 courseCLEAR_ON_RESETarrays 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 theprocessmethod 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.