Our Online Store have the new products: RFID antenna board. Currently it can work with JC10M24R and JCOP4 card chips.
Compared with normal cards, the antenna board module has a smaller size and fixed holes, which is easy to integrate in the IOT(Internet Of Things) project.

e-purse sample problem

JavaCard Applet Development Related Questions and Answers.
Benli
Posts: 11
Joined: Tue May 03, 2016 3:40 am
Points :150
Contact:

e-purse sample problem

Post by Benli » Wed Jan 11, 2017 10:20 pm

I have finished a simple e-purse applet.
Here are my code.

Code: Select all

package cp.sampleapplet;


import javacard.framework.*;

public class Epurse extends Applet {
  private short balance;
  public static final byte CLA = (byte)0x80;
  public static final byte insCredit = (byte)0xA1;
  public static final byte insDebit = (byte)0xA2;
  public static final byte insGetBalance = (byte)0xA3;
  public static final byte insSetPin = (byte)0xB1;
  public static final byte insGetAuth = (byte)0xB2;
  public static final byte insDelog = (byte)0xB3;
 
  OwnerPIN pin;
 
  public boolean select () {
    return (true);
  }
 
  public void deselect(){     
    pin.reset();
  }

  public Epurse() {
    super();
    this.balance = 0;
    pin = new OwnerPIN((byte)3, (byte)8);
    pin.update( new byte[] {(byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00}, (byte)0, (byte)4); //default PIN is 00 00 00 00
  }

  public static void install (byte [] bArray, short bOffset, byte bLength  ) throws ISOException {
    Epurse = new Epurse();
    s.register();
  }

  public void process(APDU apdu) throws ISOException{
    byte[] buffer = apdu.getBuffer();
    if (selectingApplet()) return;
    if(buffer[0]!=CLA) ISOException.throwIt(ISO7816.SW_CLA_NOT_SUPPORTED);
    ISOException.throwIt(ISO7816.SW_FILE_INVALID);
 
    switch (buffer[ISO7816.OFFSET_INS]){
    case insCredit :
         if(pin.isValidated()) credit(apdu, buffer);
         else
          ISOException.throwIt(ISO7816.SW_COMMAND_NOT_ALLOWED);
         break;
    case insDebit :
         if(pin.isValidated())
           debit(apdu, buffer);
         else
           ISOException.throwIt(ISO7816.SW_COMMAND_NOT_ALLOWED);//6986
         break;
    case insGetBalance :
         if(pin.isValidated())
           getbal(apdu, buffer);
         else
           ISOException.throwIt(ISO7816.SW_COMMAND_NOT_ALLOWED);//6986
         break;
    case insGetAuth :
         checkPIN(apdu, buffer);
         break;
    case insSetPin :
         if(pin.isValidated())
           setPIN(apdu, buffer);
         else
           ISOException.throwIt(ISO7816.SW_COMMAND_NOT_ALLOWED);//6986
         break;
    case insDelog :
         pin.reset();
         break;     
    default:
         ISOException.throwIt(ISO7816.SW_COMMAND_NOT_ALLOWED);//6986
    }   
  }

  private void credit(APDU apdu, byte[] buffer ) {
   
  }
  private void debit(APDU apdu, byte[] buffer ) {
 
  }
  private void getbal(APDU apdu, byte[] buffer) {
 
  }

  private void checkPIN(APDU apdu, byte[] buffer){
    short Le = apdu.setIncomingAndReceive();
    byte[] data = new byte[Le];
    try{
      for (short i=0; i<Le; i++)
      {
        data[i] = buffer[(short)(i+ISO7816.OFFSET_CDATA)];
      }
      if (pin.check(data, (short)0, (byte)Le)) return;
      else
     ISOException.throwIt(ISO7816.SW_RECORD_NOT_FOUND);//6A83 //6985->SW_CONDITIONS_NOT_SATISFIED
    } catch(NullPointerException e) {
        ISOException.throwIt(ISO7816.SW_SECURITY_STATUS_NOT_SATISFIED); //6982
    } catch(ArrayIndexOutOfBoundsException a) {
        ISOException.throwIt(ISO7816.SW_FILE_FULL); //6A84
    }
  }

  private void setPIN(APDU apdu, byte[] buffer){

  }
}


When I sent APDU command 80 B2 00 00 04 01 02 03 04 to verify the PIN, it fails with status word 0x6983.
But I cant find where caused this problem. Does anyone help to figure out it ?

User avatar
mabel
Posts: 237
Joined: Mon May 18, 2015 3:09 am
Points :1705
Contact:

Re: e-purse sample problem

Post by mabel » Thu Jan 12, 2017 4:10 am

Your problem is caused by this code line

Code: Select all

if(buffer[0]!=CLA) ISOException.throwIt(ISO7816.SW_CLA_NOT_SUPPORTED); 
ISOException.throwIt(ISO7816.SW_FILE_INVALID);

It will always throw exception.

Benli
Posts: 11
Joined: Tue May 03, 2016 3:40 am
Points :150
Contact:

Re: e-purse sample problem

Post by Benli » Thu Jan 12, 2017 5:27 am

Oops, shame on me.
Thanks for your help.

Post Reply Previous topicNext topic

Who is online

Users browsing this forum: Bing [Bot] and 54 guests

JavaCard OS : Disclaimer