Search code examples
javabytesmartcardjavacardapdu

How to write data to smart card using java card? with example


can some one tell me how to write data in smart card using java card? i have this code i am using java card 2.2

private void writeName(APDU apdu) throws ISOException
{ 
    apdu.setIncomingAndReceive();
    byte[] apduBuffer = apdu.getBuffer();

    byte j = (byte)apduBuffer[4];

    userName = new byte[j];
    for (byte i=0; i<j; i++)
    {
        userName[(byte)i] = (byte)apduBuffer[5+i];
    }
}

Solution

  • If you are using JCOP plugin for developing the Applet, you can see .CAP files in the bin/ directory for each package. Now you will need to install each package in order of dependency. Suppose your application consists of packages A, B and C with your applet in C package. And B imports A and C imports B.

    The order in which you will need to install the packages are A,B,C. Using JCOP, this can be accomplished by:

    cm> install <pkgAID> package.cap
    

    Once you have done this for the three packages, you will need to install the applet. This can be accomplished by:

    cm> install -q <install params> <pkgAID> <AppletAID> packageC.cap
    

    Once you do this you will see the applet in SELECTABLE state. Your JCOP shell console would look like :

    Card Manager AID   :  A000000003000000
    Card Manager state :  SECURED
    
        Application:  SELECTABLE (--------) A00000008710015553
        Application:  SELECTABLE (--------) "2PAY.SYS.DDF01"
        Application:  SELECTABLE (--------) A0004D6946617265
        Application:  SELECTABLE (--------) A0000000041010  
    

    And all those packages that are "modules" (libraries) are shown like this:

        Load File  :      LOADED (--------) A0000002994E6F53
         Module    :                        A0000002994E6F53656C656374
        Load File  :      LOADED (--------) A0000002990410  
         Module    :                        A00000029904101045
    

    All those applets ini SELECTABLE state can now be selected and interacted with. If you want to send data to Applet with A00000008710015553, you will need to first select it. The interaction would be as shown below:

    cm>  /select A00000008710015553
     => 00 A4 04 00 09 A0 00 00 00 87 10 01 55 53 00       ............US.
     (5273 usec)
     <= 90 00                                              ..
    Status: No Error
    

    Now, you can send data to the above Applet by using send command:

    cm>  /send 00200000043131313100
     => 00 20 00 00 04 31 31 31 31 00                      . ...1111.
     (34423 usec)
     <= 90 00                                              ..
    Status: No Error
    

    These are all standard GP commands. I assumed that you are using a GP compliant card; you have mentioned nothing about it though.

    I personally feel you will need to read through a bit on how JC Runtime Environment and JC VM works and it's relation with the API and a bit of GP spec before you delve into further.