Java Card Data Memory

Hi,
i am using java card applet for get data & Put data. Here, how can i get storage data memory , available data memory & Free memory.....
Thanks in advance...
Edited by: user12298678 on May 26, 2011 2:22 AM

What is your use case? Do you want to be able to know how much space is available at runtime or is it for development purposes?
If it is at runtime, you should be allocating all of your memory up front. This way you know your card will always work and you wont just run out of memory at some point and have an operation fail.
Cheers,
Shane

Similar Messages

  • Java Card Data Memory - how can a 16k array fit on my 8k card?

    Hi all,
    I tried to guess the memory size I can use in my applets by allocating arrays of bytes.
    I noticed that I am allowed to allocate a lot more memory than my physical RAM. For instance my card is 8KB ram but in my install method I create an array of 16384 bytes. How can it be possible ? Is there a swap mechanism or a virtual memory mechanism like classic computers ? I am working with a Javacard 3.0 Classic.
    Thanks a lot for your explanation.

    lexdabear wrote:
    >
    If you want to create a buffer in RAM you need to use: byte[] buffer = JCSystem.createTransientByteArray(16384);
    Oh, I didn't see this method before :).Thanks for picking that up. That is what I get for typing code into a text box when tired :(
    Correction:
    JCSystem.makeTransientByteArray(length, event)Shane

  • How to find out memory of a java card

    is there anyone could tell me that how can we check the size of the memory of a card? thank you in advance:D

    If the card is a Java Card 2.2.1, create an applet that returns
    JCSystem.getAvailableMemory(), install and queryit.
    Some other ideas for getting the free memory on card were presented in the thread "Java Forums - Free memory on card"
    http://forum.java.sun.com/thread.jspa?threadID=647894&tstart=30
    Jan

  • Please ..give me some example APDU for writing data to java card

    morning everybody .. :)
    I need some example APDUs for writing data to java card. (CLA || INS || P1 || P2 etc)..
    please..thank for your attention .. god blessing u.

    Hi,
    did you check some articles about JavaCards:
    [Understanding Java Card 2.0|http://www.javaworld.com/cgi-bin/mailto/x_java.cgi?pagetosend=/export/home/httpd/javaworld/javaworld/jw-03-1998/jw-03-javadev.html&pagename=/javaworld/jw-03-1998/jw-03-javadev.html&pageurl=http://www.javaworld.com/javaworld/jw-03-1998/jw-03-javadev.html&site=jw_core]
    [How to write a Java Card applet: A developer's guide|http://www.javaworld.com/cgi-bin/mailto/x_java.cgi?pagetosend=/export/home/httpd/javaworld/javaworld/jw-07-1999/jw-07-javacard.html&pagename=/javaworld/jw-07-1999/jw-07-javacard.html&pageurl=http://www.javaworld.com/javaworld/jw-07-1999/jw-07-javacard.html&site=jw_core]
    Hope it helps,
    Tex

  • How to get a java card memory size info ?

    Hi everyone ,
    I want to get my card's memory size and free memory size .I appreciate it if anyone could help me with these questions :
    1 . Is there any way to find out card's defferrent memories size (EEPROM , ROM , RAM , FLASH) from Get Data command or any other apdu commands ?
    2 . is there any function in the API which we could use to reach memory size within an applet ? I found a function named GetMemorryAccessInstance in JCRE 2.2.2 API but I need something more global for all API versions .
    3 . Which Memories size information do we have access to ?
    Best Regards,
    Shemeine

    Hi Shemeine,
    Some cards have proprietary APDU's to find the free memory on a card. Even on cards that have such an APDU, you may have to have it enabled when the card is produced.
    As for a way to find it programatically, you can use:
    JCSystem.getAvailableMemory(JCSystem.MEMORY_TYPE_PERSISTENT);
    JCSystem.getAvailableMemory(JCSystem.MEMORY_TYPE_TRANSIENT_DESELECT);
    JCSystem.getAvailableMemory(JCSystem.MEMORY_TYPE_TRANSIENT_RESET);The problem with these is they return a signed short. This will only show up to 32KB of free memory. If you have more than this, you will have to allocate some memory using new byte[32*1024] until you start seeing results less than 32KB. You then work out how many blocks of 32KB you had to allocate to reach this state. The types of memory you can find are self explanatory.
    Cheers,
    Shane

  • Java Card Memory Managament: How do you free-up allocated memory?

    I have a problem with java card memory management, that causes the applet to hang. I think the java card runs out of RAM when my applet runs for several iterations or process() calls. My program requires significant size of bytes for each APDU command to be sent. (around 100-250 bytes, for each apdu.sendBytes() call).
    I use a temporary byte array buffer that will contain the APDU command to be sent, declared as:
    private byte [] tmpBuff;Before each process() call, the tmBuff is initialized and contains a new byte array containing the APDU command to be sent. (array size around 100-250 bytes).
    tmpBuff = new byte[] {0x00, ... } On the process() call, the tmpBuff is copied to APDU and sendBytes() function is called. The tmpBuff byte array is reinitialized after every process() call to contain the next command. After about 20 successful commands by the process() call, the applet seems to ran out of RAM and hangs.
    I suspect, that when tmpBuff is reinitialized before each process() call, the Java Card garbage collector does now free-up the memory that was used for the previous tmpBuff byte array initialization.
    Is there a way to reclaim the memory allocated for tmpBuff after it has been initialized?
    Or maybe call the Java card garbage collector?

    Cenon,
    Generally speaking, the new keywork is a bad idea outside the install method or constructors as the JCRE is not guarenteed to have a garbage collector (and if it does, has to be called explicitly by JCSystem.requestObjectDeletion(); This however is slow.
    A better option may be to use memory more efficiently than to rely on garbage collection. A way of doing this would be to have an instance variable that is initialised once and reused. This allows you to use either a transient or persistent array.
    public class TestApplet extends Applet {
        private final static short BUFFER_SIZE = (short) 300;
        private final byte[] buffer;
        private TestApplet() {
            // only have one of the following not commented out
            /* persistent buffer */
            // buffer = new byte[BUFFER_SIZE];
            /* transient buffer (much faster) */
            buffer = JCSystem.makeTransientByteArray(BUFFER_SIZE, JCSystem.CLEAR_ON_DESELECT);
        public static void install(byte[] bArray, short bOffset, byte bLength) {
            // GP-compliant JavaCard applet registration
            new TestApplet().register(bArray, (short) (bOffset + 1), bArray[bOffset]);
        public void process(APDU apdu) {
            // Good practice: Return 9000 on SELECT
            if (selectingApplet()) {
                return;
            // do some work here with buffer
    }In the above code you would be able to use the buffer to build the command and you will not have a memory leak.
    Cheers,
    Shane
    Edited by: safarmer on Jul 8, 2008 12:25 PM

  • Java Card and Random Data

    Hello,
    i�m a student working with smart cards in my spare time. I�ve never
    worked with Java Cards before. As far as i know, instead of
    applications there are Java Applets on the Java Card. And instead of a
    native smart card OS there is the Java Card VM from Sun, with all its
    libraries.
    What i would like to know now is, how can i write a simple applet to
    generate for example a random number. According to Sun�s JC API 2.1.1
    there is a class called RandomData (in library javacard.security)
    which allows me to generate a random number. But this class is an
    abstract class. That means, i have to overwrite its abstract class
    functions setSeed(...) and generateData(...).
    Do i have to add the implementation code for these functions? How are
    these function calls handled to the JavaCard VM and to the hard ware
    of the Java Card? Does it depend on the possibilities of the hardware
    functionality of a Java Card?
    I would be very greatful if you could help me out with this.
    Sami

    You can call:
    RandomData r = RandomData.getInstance(RandomData.ALG_SECURE_RANDOM);
    Usually, the chips have hardware RNG and thus provide true random.

  • Give me *.scr script to simulate java card class

    this is the java file
    ++++++++++++++++++++++++++++++++
    //Package AID: 'D2 76 00 00 60 50 02'
    // Applet AID: 'D2 76 00 00 60 41 02'
    // Specification of the proprietary command READ
    // command APDU CLA = '80' || INS = '02' ||
    // P1 (= '00') || P2 (= offset [byte]) ||
    // Le (= number of bytes to read = DATA)
    // response APDU (good case) DATA (with length Le) || SW1 || SW2
    // response APDU (bad case) SW1 || SW2
    // Specification of the proprietary command WRITE
    // command APDU CLA = '80' || INS = '04' ||
    // P1 (= '00') || P2 (= offset [byte]) ||
    // Lc (= number of bytes to write) // DATA (bytes to write)
    // response APDU (all cases) SW1 || SW2
    package packmini; // this is the package name
    import javacard.framework.*; // import all neccessary packages for java card
    public class ReadWrite extends Applet {
    final static byte CLASS = (byte) 0x80; // class of the APDU commands
    final static byte INS_READ = (byte) 0x02; // instruction for the READ APDU command
    final static byte INS_WRITE = (byte) 0x04; // instruction for the WRITE APDU command
    final static short SIZE_MEMORY = (short) 9; // size of the data storage area
    static byte[] memory; // this is the data memory for the application
    //----- installation and registration of the applet -----
    public static void install(byte[] buffer, short offset, byte length) {
    memory = new byte[SIZE_MEMORY]; // this is the data storage area
    new ReadWrite().register();
    } // install
    //----- this is the command dispatcher -----
    public void process(APDU apdu) {
    byte[] cmd_apdu = apdu.getBuffer();
    if (cmd_apdu[ISO7816.OFFSET_CLA] == CLASS) {  // it is the rigth class
    switch(cmd_apdu[ISO7816.OFFSET_INS]) {      // check the instruction byte
    case INS_READ: // it is a READ instruction
    cmdREAD(apdu);
    break;
    case INS_WRITE: // it is a WRITE instruction
    cmdWRITE(apdu);
    break;
    default : // the instruction in the command apdu is not supported
    ISOException.throwIt(ISO7816.SW_INS_NOT_SUPPORTED);
    } // switch
    } // if
    else {                                        // the class in the command apdu is not supported
    ISOException.throwIt(ISO7816.SW_CLA_NOT_SUPPORTED);
    } // else
    } // process
    //----- program code for the APDU command READ -----
    private void cmdREAD(APDU apdu) {
    byte[] cmd_apdu = apdu.getBuffer();
    //----- check the preconditions -----
    // check if P1=0
    if (cmd_apdu[ISO7816.OFFSET_P1] != 0) ISOException.throwIt(ISO7816.SW_WRONG_P1P2);
    // check if offset P2 is inside the bound of the memory array
    short offset = (short) (cmd_apdu[ISO7816.OFFSET_P2] & 0x00FF); // calculate offset
    if (offset >= SIZE_MEMORY) ISOException.throwIt(ISO7816.SW_WRONG_P1P2);
    // check if offset P2 and expected length Le is inside the bounds of the memory array
    short le = (short)(cmd_apdu[ISO7816.OFFSET_LC] & 0x00FF); // calculate Le (expected length)
    if ((offset + le) > SIZE_MEMORY) ISOException.throwIt(ISO7816.SW_WRONG_LENGTH);
    // check if expected length Le is 0
    if (le == 0) ISOException.throwIt(ISO7816.SW_WRONG_LENGTH);
    //----- now all preconditions are fulfilled, the data can be send to the IFD -----
    apdu.setOutgoing(); // set transmission to outgoing data
    apdu.setOutgoingLength((short)le); // set the number of bytes to send to the IFD
    apdu.sendBytesLong(memory, (short)offset, (short)le); // send the requested number of bytes to the IFD
    } // cmdREAD
    //----- program code for the APDU command WRITE -----
    private void cmdWRITE(APDU apdu) {
    byte[] cmd_apdu = apdu.getBuffer();
    //----- check the preconditions -----
    // check if P1=0
    if (cmd_apdu[ISO7816.OFFSET_P1] != 0) ISOException.throwIt(ISO7816.SW_WRONG_P1P2);
    // check if offset P2 is inside the bound of the memory array
    short offset = (short) (cmd_apdu[ISO7816.OFFSET_P2] & 0x00FF); // calculate offset
    if (offset >= SIZE_MEMORY) ISOException.throwIt(ISO7816.SW_WRONG_P1P2);
    // check if offset P2 and expected length Le is inside the bounds of the memory array
    short lc = (short)(cmd_apdu[ISO7816.OFFSET_LC] & 0x00FF); // calculate Lc (expected length)
    if ((offset + lc) > SIZE_MEMORY) ISOException.throwIt(ISO7816.SW_WRONG_LENGTH);
    // check if command length Lc is 0
    if (lc == 0) ISOException.throwIt(ISO7816.SW_WRONG_LENGTH);
    receiveAPDUBody(apdu); // receive now the rest of the APDU
    //----- now all preconditions are fulfilled, the data can be copied to the memory -----
    Util.arrayCopy(cmd_apdu, (short)((ISO7816.OFFSET_CDATA) & 0x00FF), memory, offset, lc); // this copy precedure is atomic
    ISOException.throwIt(ISO7816.SW_NO_ERROR); // command proper executed
    } // cmdWRITE
    //----- receive the body of the command APDU
    public void receiveAPDUBody(APDU apdu) {
    byte[] buffer = apdu.getBuffer();
    short lc = (short)(buffer[ISO7816.OFFSET_LC] & 0x00FF); // calculate Lc (expected length)
    // check if Lc != number of received bytes of the command APDU body
    if (lc != apdu.setIncomingAndReceive()) ISOException.throwIt(ISO7816.SW_WRONG_LENGTH);
    } // receiveAPDUBody
    ++++++++++++++++++++++++++++++++
    I hope u can give me sollution for my problem..
    Thank u all..

    need help ... somebody can help me ??
    I have java file..(on this below)..I want to run it with JCWDE...can you give me script APDU to run it on JCWDE ???
    thank for your attention .
    package packmini;
    import javacard.framework.APDU;
    import javacard.framework.ISO7816;
    import javacard.framework.Applet;
    import javacard.framework.ISOException;
    import javacard.framework.Util;
    public class IdCard extends Applet {
         //The Cla Value
         final static byte claValue = (byte) 0x08;
         //The Ins Value
         final static byte readName = (byte)0x01;
         final static byte readPhone = (byte) 0x02;
         final static byte writeName = (byte) 0x03;
         final static byte writePhone = (byte) 0x04;
         byte[] nameID;;
         byte[] phone ;
         public IdCard()
              nameID = new byte[12];
              phone = new byte[12];
         public static void install(byte[] bArray, short bOffset, byte bLength) {
              // GP-compliant JavaCard applet registration
              new packmini.IdCard().register(bArray, (short) (bOffset + 1),
                        bArray[bOffset]);
         public void process(APDU apdu) {
              // Good practice: Return 9000 on SELECT
              if (selectingApplet()) {
                   return;
              byte[] buf = apdu.getBuffer();
              if(buf[ISO7816.OFFSET_CLA]!= claValue)
                   ISOException.throwIt(ISO7816.SW_CLA_NOT_SUPPORTED);
              switch (buf[ISO7816.OFFSET_INS]) {
              case writeName :     
                        writeNameIn(apdu);
                        break;
              case writePhone:
                        writePhoneIn(apdu);
                        break;
              case readName:
                        readNameOut(apdu);
                        break;
              case readPhone:
                        readPhoneOut(apdu);
                        break;
              default:
                   // good practice: If you don't know the INStruction, say so:
                   ISOException.throwIt(ISO7816.SW_INS_NOT_SUPPORTED);
         public void writeNameIn(APDU apdu)
              byte[] buff = apdu.getBuffer();
              short lcLength = buff[ISO7816.OFFSET_LC];
              short readCount = apdu.setIncomingAndReceive();
              if(readCount== 0 || lcLength == 0)
                   ISOException.throwIt(ISO7816.SW_WRONG_LENGTH);
              Util.arrayCopy(buff,(short) ISO7816.OFFSET_CDATA, nameID, (short)0, readCount);     
         public void writePhoneIn(APDU apdu)
              byte [] buff = apdu.getBuffer();
              short readCount = apdu.setIncomingAndReceive();
              short lcLength = buff[ISO7816.OFFSET_LC];
              if(readCount== 0 || lcLength == 0)
                   ISOException.throwIt(ISO7816.SW_WRONG_LENGTH);
              Util.arrayCopy(buff, ISO7816.OFFSET_CDATA, phone, (short)0, readCount);     
         public void readNameOut(APDU apdu)
              byte [] buff = apdu.getBuffer();
              short le = apdu.setOutgoing();
              apdu.setOutgoingLength((short) nameID.length);
              Util.arrayCopy(nameID,(short) 0, buff,(short) 0,(short) nameID.length);
              apdu.sendBytes((short)0, (short) nameID.length);
         public void readPhoneOut(APDU apdu)
              byte [] buff = apdu.getBuffer();
              short le = apdu.setOutgoing();
              apdu.setOutgoingLength((short) phone.length);
              Util.arrayCopy(phone,(short) 0, buff,(short) 0,(short) phone.length);
              apdu.sendBytes((short)0, (short) phone.length);
    }

  • How to improve the performance of java card OS?

    Hi All,
    we are into Java card OS developement. We need some tips for performance improvement of OS.
    1. what are the possible calculations we can do during loading so that we show good performance during operational mode.
    2. Tips in bytecode implimentation.
    Thanks

    Dear Friend,
    Performance term has relative character in Java Card implementation because there is no etalon. CREF is PC simulated software and can not be used for measurements. So for determining card performance only another implementation comparison can be suggested. Few things should be taken in account while card implementation comparison.
    1. Measurement is done by analysis of time consumption by the card to perfrom a command starting from data transfer and ending by status code.
    2. Same bytecode should be used in both samples.
    3. Same transmission rate should be used.
    4. Analysis of similarity of hardware including CPU, persistent memory programming time
    5. To perform that kind of measurement special tool listeting to terminal-smartcard interface is required
    I have no tips to bytecode implementation because bytecode is a thing that is determined by same Sun compiler and Java Card converter. So all are in the same conditions.
    Yours sincerely
    Dmitri

  • Java card cap file install error in jcop tools

    When i try to run java card applet in real java card it gives this error.i used jcop tools and java version 1.5  and eclipse 3.2.when i run the applet in simulater it works fine.i saw some document it says allocate memory for the installation process.i don't know how to do that.
    /term "winscard:4|SCM Microsystems Inc. SCL010 Contactless Reader 0"
    --Opening terminal
    >  /card -a a000000003000000 -c com.ibm.jc.CardManager
    resetCard with timeout: 0 (ms)
    --Waiting for card...
    ATR=3B 8A 80 01 4A 43 4F 50 33 31 56 32 33 32 7A       ;...JCOP31V232z
    ATR: T=0, T=1, Hist="JCOP31V232"
    => 00 A4 04 00 08 A0 00 00 00 03 00 00 00 00          ..............
    (23807 usec)
    <= 6F 65 84 08 A0 00 00 00 03 00 00 00 A5 59 9F 65    oe...........Y.e
        01 FF 9F 6E 06 47 91 73 51 2E 00 73 4A 06 07 2A    ...n.G.sQ..sJ..*
        86 48 86 FC 6B 01 60 0C 06 0A 2A 86 48 86 FC 6B    .H..k.`...*.H..k
        02 02 01 01 63 09 06 07 2A 86 48 86 FC 6B 03 64    ....c...*.H..k.d
        0B 06 09 2A 86 48 86 FC 6B 04 02 15 65 0B 06 09    ...*.H..k...e...
        2B 85 10 86 48 64 02 01 03 66 0C 06 0A 2B 06 01    +...Hd...f...+..
        04 01 2A 02 6E 01 02 90 00                         ..*.n....
    Status: No Error
    cm>  set-key 255/1/DES-ECB/404142434445464748494a4b4c4d4e4f 255/2/DES-ECB/404142434445464748494a4b4c4d4e4f 255/3/DES-ECB/404142434445464748494a4b4c4d4e4f
    cm>  init-update 255
    => 80 50 00 00 08 AC B1 90 01 BF 2D 24 A0 00          .P........-$..
    (49906 usec)
    <= 00 00 91 18 01 39 93 95 05 59 FF 02 00 2C BE 39    .....9...Y...,.9
        5E A5 07 55 87 B8 C3 A8 A6 93 66 2B 90 00          ^..U......f+..
    Status: No Error
    cm>  ext-auth plain
    => 84 82 00 00 10 0C AE 50 3E C8 7E 1D 92 29 E2 59    .......P>.~..).Y
        08 D9 DA 02 16                                     .....
    (57276 usec)
    <= 90 00                                              ..
    Status: No Error
    cm>  delete 060504030201
    => 80 E4 00 00 08 4F 06 06 05 04 03 02 01 00          .....O........
    (40041 usec)
    <= 6A 88                                              j.
    Status: Reference data not found
    jcshell: Error code: 6a88 (Reference data not found)
    jcshell: Wrong response APDU: 6A88
    Ignoring expected error
    cm>  delete 010203040506
    => 80 E4 00 00 08 4F 06 01 02 03 04 05 06 00          .....O........
    (17392 usec)
    <= 6A 88                                              j.
    Status: Reference data not found
    jcshell: Error code: 6a88 (Reference data not found)
    jcshell: Wrong response APDU: 6A88
    Ignoring expected error
    cm>  upload -d -b 250 "Cap File location"
    => 80 E6 02 00 13 06 01 02 03 04 05 06 08 A0 00 00    ................
        00 03 00 00 00 00 00 00 00                         .........
    (32303 usec)
    <= 00 90 00                                           ...
    Status: No Error
    => 80 E8 00 00 FA C4 81 F2 01 00 23 DE CA FF ED 02    ..........#.....
        02 04 00 01 06 01 02 03 04 05 06 12 68 6D 73 2F    ............hms/
        6A 61 76 61 63 61 72 64 2F 68 65 6C 6C 6F 02 00    javacard/hello..
        21 00 23 00 21 00 0A 00 0B 00 1E 00 0E 00 3D 00    !.#.!.........=.
        0A 00 0B 00 00 00 4D 01 BE 00 00 00 00 00 00 01    ......M.........
        01 00 04 00 0B 01 02 01 07 A0 00 00 00 62 01 01    .............b..
        03 00 0A 01 06 06 05 04 03 02 01 00 08 06 00 0E    ................
        00 00 00 80 03 00 FF 00 07 01 00 00 00 1C 07 00    ................
        3D 00 01 10 18 8C 00 05 7A 05 30 8F 00 00 3D 8C    =.......z.0...=.
        00 01 18 1D 04 41 18 1D 25 8B 00 02 7A 02 21 18    .....A..%...z.!.
        8B 00 03 60 03 7A 19 8B 00 04 2D 1A 04 25 73 00    ...`.z....-..%s.
        09 00 00 00 00 00 0F 11 6D 00 8D 00 06 7A 08 00    ........m....z..
        0A 00 00 00 00 00 00 00 00 00 00 05 00 1E 00 07    ................
        01 00 02 00 06 00 00 01 03 80 03 02 03 80 03 03    ................
        03 80 0A 01 06 80 03 00 06 80 07 01 09 00 0B 00    ................
        00 00 07 05 06 04 0A 07 07 13 0B 00 4D 01 00 00    ............M...
    (764519 usec)
    <= 6A 80                                              j.
    Status: Wrong data
    jcshell: Error code: 6a80 (Wrong data)
    jcshell: Wrong response APDU: 6A80
    Unexpected error; aborting execution

    Thanks ReNa
    i am using nxp jcop 31 contact-less card.and it will support for java card 2.2.1 and global-platform card specification 2.1.1 .so i am using nxp jcop training 2007 tools and it has java card 2.2.1 and global-platform card specification 2.1.1.Inside my project there are three jar files these are jc221.jar and gp211.jar and bio10.jar
    i will appreciate your help
    Thanks

  • Is it possible to simulate whole java card application on simulator?

    Hello all,
    currently i dont have any real java card yet,not either reader . I already made a simple applet and check its working by CJCRE (net beans simulator), it is working fine,
    now i want to move to host application which will communicate with java card via card reader. Is this possible to simulate complete application ( host application + java card applets)?
    I want to check it out that:- which line of java program would be helpful to know that:-
    1-"how real host application will talk to reader"
    2- how it connect and disconnect the reader
    3- how it send apdu to java card by reader
    and much more.
    I am using netbeans 7.1 and it is using CJCRE as a simulator....

    you mean to say this
    http://askra.de/software/jcdocs/app-notes-2.2.2/apduio.html
    I am quite confused now, i read this but still can not imagine the concept ,
    an applet code is :-
    public class GetName extends Applet
         final static byte CLASS     = (byte) 0x80;  // Class of the APDU commands
         final static byte INS_READ  = (byte) 0x02;  // instruction for the READ APDU command
         final static byte INS_WRITE = (byte) 0x03;  // instruction for the READ APDU command
         final static byte INS_DY_CO = (byte) 0x04;  // instruction for the READ APDU command
         final static byte[] text    = {(byte) 'A', (byte) 'M', (byte) 'I', (byte) 'T'};
         public static byte[] holder;
         public static void install(byte[] bArray, short bOffset, byte bLength)
            new GetName();
        protected GetName()
            holder = new byte[5];// allocation of memory in runtime
            register();
        public void process(APDU apdu)
              if(selectingApplet())
                   return;
            byte[] cmd_apdu = apdu.getBuffer();         
             if (cmd_apdu[ISO7816.OFFSET_CLA] == CLASS)
                   switch(cmd_apdu[ISO7816.OFFSET_INS])
                        case INS_READ:  
                        if ((cmd_apdu[ISO7816.OFFSET_P1] != 0) || (cmd_apdu[ISO7816.OFFSET_P2] != 0))
                        ISOException.throwIt(ISO7816.SW_WRONG_P1P2);
                        short le = (short)(cmd_apdu[ISO7816.OFFSET_LC] & 0x00FF); 
                        short len_text = (short)text.length;                      
                        if (le != len_text)
                        ISOException.throwIt((short)(ISO7816.SW_CORRECT_LENGTH_00 + len_text)); 
                        apdu.setOutgoing();                      
                        apdu.setOutgoingLength((short)len_text);
                        apdu.sendBytesLong(text, (short)0, (short)len_text);
                        break;
                    // here we save data from apdu and will keep inside the data byte                   
                        case INS_WRITE:
                    short lc = (short)(cmd_apdu[ISO7816.OFFSET_LC] & 0x00FF); 
                    Util.arrayCopy(cmd_apdu, (short) ((ISO7816.OFFSET_CDATA) & 0xff), holder, (short) 0, lc);
                        short len_holder_inside_write= (short) holder.length;
                       apdu.setOutgoing();
                    apdu.setOutgoingLength((short)len_holder_inside_write);
                    apdu.sendBytesLong(holder, (short) 0, (short) len_holder_inside_write);
                    break;
                    case INS_DY_CO:
                        short len_holder= (short) holder.length;
                    apdu.setOutgoing();
                    apdu.setOutgoingLength((short)len_holder);
                    apdu.sendBytesLong(holder, (short) 0, (short) len_holder);
                    break;
                        default : 
                        ISOException.throwIt(ISO7816.SW_INS_NOT_SUPPORTED);
             else
                    ISOException.throwIt(ISO7816.SW_CLA_NOT_SUPPORTED);
    }now i think i need to make a another java file which would contain a main class, Is this both file would be inside a single java card project. and how these both would be connect to each other.
    for manually run a applet- there were two steps 1-
    C:\java_card_kit-2_2\samples\src\demo>jcwde jcwde-getname.app
    Java Card 2.2 Workstation Development Environment (version 0.18).
    Copyright 2002 Sun Microsystems, Inc. All rights reserved.
    jcwde is listening for T=0 Apdu's on TCP/IP port 9,025.
    and in second terminal we wrote-
    C:\java_card_kit-2_2\samples\src\demo>apdutool -nobanner -noatr getname.scr > ge
    tname.scr.jcwde.out
    Here it is clear that, if i will use javacardio then no need to pass the apdu from a file .....but i am confuse about how it possible, ie. is both file lie in same project , because i m using simulator so i need to run these both file simoltaneously........give me some roughly steps to implement this.

  • Security in each byte of Java card's EEPROM

    as i undrestand until now in my applet I define a variable and store data in that variable,
    Is it a way to know where these data are stored, I mean I wanna define the memory address of that data by myself,
    caz my card application is multi-app, and maybe in future I want to let someone else load his/her applet in that card beside my applets to do other application
    but from know I wanna think of security that in future let that person to have the memory address from i.e 0x01 up to 0x05
    and have no right to read or write in other memory bytes
    and also each part of memory address should have a security code for authentication...
    what's your idea about this post and what do u sudggest?
    Regards
    Hana

    I'm sorry for my ignorance, but i think that you do not want to mess around with the Card Issuer Keys unless you are the Card Issuer which does not seems like the case.
    I think that you want to use the Secure Channel Protocol inside your own applet(which is what i want to do also) and use your own issued keys.
    Why? Because in a real situation you will not want that your Java Card stuck with only one App, you want it to have as many as the user wants to. For that to happen, the card issuer keeps a keyset to load&install applets, but, when the applet is installed you want to maintain your privacy from the card issuer(and everyone else), so you will need an extra keyset for your own Secure Channel Protocol.
    What i'm saying is that:
    When you enter the Cards Security Domain, you need the cards security domain keys.
    When you select your applet, all your comunications become "plain-text" and you will not have any transaction security.
    I would like to know how to open this SCP channel from my app, but unfortunatelly i cannot help you any further.
    Edited by: rochajoel on Aug 31, 2009 9:48 AM

  • Novice in Java Card

    Hi! I'm a novice in Java Card and I'm trying to study with the examples that I find in the Internet. I get this code, that I compile with successful. But, now, what I have to do? how can I debbug and test?
    Thanks.
    package bank;
    import javacard.framework.*;
    //import javacardx.framework.*;
    public class Wallet extends Applet {
    /* constants declaration */
    // code of CLA byte in the command APDU header
    final static byte Wallet_CLA =(byte)0xB0;
    // codes of INS byte in the command APDU header
    final static byte VERIFY = (byte) 0x20;
    final static byte CREDIT = (byte) 0x30;
    final static byte DEBIT = (byte) 0x40;
    final static byte GET_BALANCE = (byte) 0x50;
    // maximum balance
    final static short MAX_BALANCE = 0x7FFF;
    // maximum transaction amount
    final static byte MAX_TRANSACTION_AMOUNT = 127;
    // maximum number of incorrect tries before the
    // PIN is blocked
    final static byte PIN_TRY_LIMIT =(byte)0x03;
    // maximum size PIN
    final static byte MAX_PIN_SIZE =(byte)0x08;
    // signal that the PIN verification failed
    final static short SW_VERIFICATION_FAILED = 0x6300;
    // signal the PIN validation is required
    // for a credit or a debit transaction
    final static short SW_PIN_VERIFICATION_REQUIRED = 0x6301;
    // signal invalid transaction amount
    // amount > MAX_TRANSACTION_MAOUNT or amount < 0
    final static short SW_INVALID_TRANSACTION_AMOUNT = 0x6A83;
    // signal that the balance exceed the maximum
    final static short SW_EXCEED_MAXIMUM_BALANCE = 0x6A84;
    // signal the balance becomes negative
    final static short SW_NEGATIVE_BALANCE = 0x6A85;
    /* instance variables declaration */
    OwnerPIN pin;
    short balance;
    private Wallet(byte[] bArray, short bOffset, byte bLength){
    // It is good programming practice to allocate
    // all the memory that an applet needs during
    // its lifetime inside the constructor
    pin = new OwnerPIN(PIN_TRY_LIMIT, MAX_PIN_SIZE);
    // The installation parameters contain the PIN
    // initialization value
    pin.update(bArray, bOffset, bLength);
    register();
    public static void install(byte[] bArray, short bOffset, byte bLength) {
    // create a Wallet applet instance
    new Wallet(bArray, bOffset, bLength);
    public boolean select() {
    // The applet declines to be selected
    // if the pin is blocked.
    if (pin.getTriesRemaining() == 0) return false;
    return true;
    public void deselect() {
    // reset the pin value
    pin.reset();
    public void process(APDU apdu) {
    // APDU object carries a byte array (buffer) to
    // transfer incoming and outgoing APDU header
    // and data bytes between card and CAD
    // At this point, only the first header bytes
    // [CLA, INS, P1, P2, P3] are available in
    // the APDU buffer.
    // The interface javacard.framework.ISO7816
    // declares constants to denote the offset of
    // these bytes in the APDU buffer
    byte[] buffer = apdu.getBuffer();
    // check SELECT APDU command
    if ((buffer[ISO7816.OFFSET_CLA] == 0) &&
    (buffer[ISO7816.OFFSET_INS] == (byte)(0xA4))) return;
    // verify the reset of commands have the
    // correct CLA byte, which specifies the
    // command structure
    if (buffer[ISO7816.OFFSET_CLA] != Wallet_CLA)
    ISOException.throwIt(ISO7816.SW_CLA_NOT_SUPPORTED);
    switch (buffer[ISO7816.OFFSET_INS]) {
    case GET_BALANCE: getBalance(apdu);
    return;
    case DEBIT: debit(apdu);
    return;
    case CREDIT: credit(apdu);
    return;
    case VERIFY: verify(apdu);
    return;
    default: ISOException.throwIt(ISO7816.SW_INS_NOT_SUPPORTED);
    } // end of process method
    private void credit(APDU apdu) {
    // access authentication
    if ( !pin.isValidated()) ISOException.throwIt(SW_PIN_VERIFICATION_REQUIRED);
    byte[] buffer = apdu.getBuffer();
    byte Lc; //denotes the number of bytes in the
    // data field of the command APDU
    byte numBytes = buffer[ISO7816.OFFSET_LC];
    // indicate that this APDU has incoming data
    // and receive data starting at the offset
    // ISO7816.OFFSET_CDATA following the 5 header
    // bytes.
    byte byteRead = (byte)(apdu.setIncomingAndReceive());
    // it is an error if the number of data bytes
    // read does not match the number in Lc byte
    if (byteRead != 1) ISOException.throwIt(ISO7816.SW_WRONG_LENGTH);
    // get the credit amount
    byte creditAmount = buffer[ISO7816.OFFSET_CDATA];
    // check the credit amount
    if ( ( creditAmount > MAX_TRANSACTION_AMOUNT) || ( creditAmount < 0 ) )
    ISOException.throwIt(SW_INVALID_TRANSACTION_AMOUNT);
    // check the new balance
    if ( ( balance + creditAmount) > MAX_BALANCE ) ISOException.throwIt(SW_EXCEED_MAXIMUM_BALANCE);
    // credit the amount
    balance = (short)(balance + creditAmount);
    } // end of deposit method
    private void debit(APDU apdu) {
    // access authentication
    if ( ! pin.isValidated()) ISOException.throwIt(SW_PIN_VERIFICATION_REQUIRED);
    byte[] buffer = apdu.getBuffer();
    byte numBytes = (byte)(buffer[ISO7816.OFFSET_LC]);
    byte byteRead = (byte)(apdu.setIncomingAndReceive());
    if (byteRead != 1) ISOException.throwIt(ISO7816.SW_WRONG_LENGTH);
    // get debit amount
    byte debitAmount = buffer[ISO7816.OFFSET_CDATA];
    // check debit amount
    if ((debitAmount > MAX_TRANSACTION_AMOUNT) || (debitAmount < 0 ) )
    ISOException.throwIt(SW_INVALID_TRANSACTION_AMOUNT);
    // check the new balance
    if ((balance - debitAmount) < 0) ISOException.throwIt(SW_NEGATIVE_BALANCE);
    balance = (short) (balance - debitAmount);
    } // end of debit method
    private void getBalance(APDU apdu) {
    byte[] buffer = apdu.getBuffer();
    // inform system that the applet has finished
    // processing the command and the system should
    // now prepare to construct a response APDU
    // which contains data field
    short le = apdu.setOutgoing();
    if ( le < 2 ) ISOException.throwIt(ISO7816.SW_WRONG_LENGTH);
    //informs the CAD the actual number of bytes
    //returned
    apdu.setOutgoingLength((byte)2);
    // move the balance data into the APDU buffer
    // starting at the offset 0
    buffer[0] = (byte)(balance >> 8);
    buffer[1] = (byte)(balance & 0xFF);
    // send the 2-balance byte at the offset
    // 0 in the apdu buffer
    apdu.sendBytes((short)0, (short)2);
    } // end of getBalance method
    private void verify(APDU apdu) {
    byte[] buffer = apdu.getBuffer();
    // retrieve the PIN data for validation.
    byte byteRead = (byte)(apdu.setIncomingAndReceive());
    // check pin
    // the PIN data is read into the APDU buffer
    // at the offset ISO7816.OFFSET_CDATA
    // the PIN data length = byteRead
    if ( pin.check(buffer, ISO7816.OFFSET_CDATA,byteRead) == false )
    ISOException.throwIt(SW_VERIFICATION_FAILED);
    }

    Win nt 4 and Win 2000 are the only supported windows platforms at this time.

  • Java Card headache

    Hi,I am doing my final year project on java card and I have the software installed(java_card_kit-2_2_1, OCF 1.2, j2sdk1.4.1).I'm using Schlumberger Cyberflex Access Toolkit 4.4 and I already have the e-gate USB token.The problem is that I'm not sure how i shld start developing my application.I planned to do online cash withdrawal which can download cash from the bank account directly to the smart card. Anybody can help me with this?Where should i start from?I really need help and plz feel free 2 mail me.my email is [email protected] u very much.

    You are trying to run before you learn to walk. Learn the Java Card architecture. If you don't learn these things first, you'll be asking, how to write the applet, how to generate key pairs, how to create memory in EEPROM, how to send commands to the applet to store large data sizes over 256, etc. Take one step at a time !
    To answer you question: Depends on your solution. If you are using certs for digital signing, you should generate the signing keys on the card and send a CSR with the public key. Store the signing cert on the card. PKCS#15 is the standard, but time consuming to implement on a Java Card, so I recommend just a buffer and handle it off card. For encryption certs, you can generate the key pair off card and store the certificates on the card.
    CA questions should be directed to the Security Forum.
    HTH !

  • Generate DES key with java card with JCRE 2.1.2

    Hi everyone,
    I want to generate DES key in my applet . my card supports GP 2.0.1 and JCRE 2.1.2 .
    I have tested my applet with JCRE 2.2.1 and used this JCSystem class functions to generate DES key and it compiles and works correctlly .
    but when I want to compile my applet with JCRE 2.1.2 I recieve an error which says that API 2.1.2 doesn't support JCSystem class .
    so I'll really appreciate it if anyone could tell me how can I generate DES key with JCRE 2.1.2
    and also I use JCSystem class functions to get my card's persistent and transistent memory , so with this class not working on JCRE 2.1.2 I have problem to read my free memories too .
    So I'll appreciate your help on this matter too.
    Best Regards,
    Vivian

    Hi Vivian,
    I don't seem to have any problem with the code you posted. What is the error you are getting? Is it with the compiler or with the CAP file converter? If it is a compiler error, you will need to ensure that the Java Card API jar is in your build path.
    Here is a simple class that works with JC 2.1.1 (which will work with JC 2.1.2 as well). I have confirmed that this applet compiles and will return encrypted data to the caller.
    package test;
    import javacard.framework.APDU;
    import javacard.framework.Applet;
    import javacard.framework.ISO7816;
    import javacard.framework.ISOException;
    import javacard.framework.JCSystem;
    import javacard.security.DESKey;
    import javacard.security.KeyBuilder;
    import javacard.security.RandomData;
    import javacardx.crypto.Cipher;
    * Test JC2.1.1 applet for random DES key.
    * @author safarmer - 1.0
    * @created 24/11/2009
    * @version 1.0 %PRT%
    public class TestApplet extends Applet {
        private DESKey key;
        private Cipher cipher;
         * Default constructor that sets up key and cipher.
        public TestApplet() {
            RandomData rand = RandomData.getInstance(RandomData.ALG_SECURE_RANDOM);
            short lenBytes = (short) (KeyBuilder.LENGTH_DES / 8);
            byte[] buffer = JCSystem.makeTransientByteArray(lenBytes, JCSystem.CLEAR_ON_DESELECT);
            key = (DESKey) KeyBuilder.buildKey(KeyBuilder.TYPE_DES, KeyBuilder.LENGTH_DES, false);
            rand.generateData(buffer, (short) 0, lenBytes);
            key.setKey(buffer, (short) 0);
            cipher = Cipher.getInstance(Cipher.ALG_DES_CBC_ISO9797_M1, false);
        public static void install(byte[] bArray, short bOffset, byte bLength) {
            // GP-compliant JavaCard applet registration
            new TestApplet().register(bArray, (short) (bOffset + 1), bArray[bOffset]);
        public void process(APDU apdu) {
            // Good practice: Return 9000 on SELECT
            if (selectingApplet()) {
                return;
            byte[] buf = apdu.getBuffer();
            switch (buf[ISO7816.OFFSET_INS]) {
                case (byte) 0x00:
                    cipher.init(key, Cipher.MODE_ENCRYPT);
                    short len = cipher.doFinal(buf, ISO7816.OFFSET_CDATA, buf[ISO7816.OFFSET_LC], buf, (short) 0);
                    apdu.setOutgoingAndSend((short) 0, len);
                    break;
                default:
                    // good practice: If you don't know the INStruction, say so:
                    ISOException.throwIt(ISO7816.SW_INS_NOT_SUPPORTED);
    }Cheers,
    Shane

Maybe you are looking for