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.

java card program problem: how to make code simpler and more efficient

JavaCard Applet Development Related Questions and Answers.
User avatar
mabel
Posts: 237
Joined: Mon May 18, 2015 3:09 am
Points :1705
Contact:

java card program problem: how to make code simpler and more efficient

Post by mabel » Thu Sep 17, 2015 1:46 am

I am writing code for java card encryption.
Here is part of the code.

Code: Select all

switch (buf[ISO7816.OFFSET_P1]){
    case (byte) 0x01:
        switch (buf[ISO7816.OFFSET_P2]){
            case (byte) 0x01:
                doSingleDES(apdu, DESKey1);
                return;
            case (byte) 0x02:
                doSingleDES(apdu, DESKey2);
                return;
            case (byte) 0x03:
                doSingleDES(apdu, DESKey3);
                return;
            case (byte) 0x04:
                doSingleDES(apdu, DESKey3);
                return;
            default:
                ISOException.throwIt(ISO7816.SW_WRONG_P1P2);
        }return;
    case (byte) 0x02:
        switch (buf[ISO7816.OFFSET_P2]){
            case (byte) 0x01:
                doEncrypt(apdu, DESKey1);
                return;
            case (byte) 0x02:
                doEncrypt(apdu, DESKey2);
                return;
            case (byte) 0x03:
                doEncrypt(apdu, DESKey3);
                return;
            case (byte) 0x04:
                doEncrypt(apdu, DESKey4);
                return;
            default:
                ISOException.throwIt(ISO7816.SW_INCORRECT_P1P2);
    }return;
    default:
            ISOException.throwIt(ISO7816.SW_INCORRECT_P1P2);

I want to use P1 to select an algorithm and use P2 to select a key for P1.

Q: I want to simplify this nested switch. How can I do that?
Note that I appreciate any suggestion to make my program more efficient.

monter001
Posts: 5
Joined: Thu Aug 13, 2015 3:56 am
Points :2
Contact:

Re: java card program problem: how to make code simpler and more efficient

Post by monter001 » Mon Nov 02, 2015 9:44 am

Maybe, you can set the value of DESKey into an array , so you can use such as:

Code: Select all

     DESKeyTypeArray[]  arrayDemo = {DESKey1, DESKey2, DESKey3};
     
     switch (buf[ISO7816.OFFSET_P1])
     {
        case (byte) 0x01:
             doSingleDES(apdu, arrayDemo[ISO7816.OFFSET_P2]);
        case (byte) 0x02:
             doEncrypt(apdu, arrayDemo[ISO7816.OFFSET_P2]);
       ......
      }

Post Reply Previous topicNext topic

Who is online

Users browsing this forum: No registered users and 39 guests

JavaCard OS : Disclaimer