0
votes

I want to send a random byte array including for example 24 elements to my JavaCard applet and then my applet is supposed to change that array using a specific method. For example that method XOR each elements with 0x05 and returns the result array in APDU response.

To aim the above goal I wrote the following program so far:

package keyGeneratorPackage;

import javacard.framework.APDU;
import javacard.framework.ISO7816;
import javacard.framework.Applet;
import javacard.framework.ISOException;
import javacard.framework.JCSystem;
import javacard.framework.Util;

public class KeyGeneratorPackage extends Applet {
    private static final byte HW_CLA = (byte) 0x80;
    private static final byte HW_INS = (byte) 0x00;

    public static void install(byte[] bArray, short bOffset, byte bLength) {
        new keyGeneratorPackage.KeyGeneratorPackage().register(bArray, (short) (bOffset + 1),
                bArray[bOffset]);
    }

    public void process(APDU apdu) {
        if (selectingApplet()) {
            return;
        }
        apdu.setIncomingAndReceive();
        byte[] buffer = apdu.getBuffer();
        byte CLA = (byte) (buffer[ISO7816.OFFSET_CLA] & 0xFF);
        byte INS = (byte) (buffer[ISO7816.OFFSET_INS] & 0xFF);
        short  lc =  (short) (buffer[ISO7816.OFFSET_LC] & (short)0x00FF); 
        byte[] Data = JCSystem.makeTransientByteArray(lc, JCSystem.CLEAR_ON_DESELECT);

        if (CLA != HW_CLA) {
            ISOException.throwIt(ISO7816.SW_CLA_NOT_SUPPORTED);
        }
        switch (INS) {
        case HW_INS: {
            // copying the apdu data into byte array Data
            Util.arrayCopy(buffer, ISO7816.OFFSET_CDATA, Data, (short) 0, lc);
            getKey(apdu, Data);
        }
        break;
        default:
            ISOException.throwIt(ISO7816.SW_INS_NOT_SUPPORTED);
        }
    }

    private void getKey(APDU apdu, byte[] data) {
        byte[] buffer = apdu.getBuffer();
        short length = (short) data.length;
        //prepareing the key array of same length of Data
        byte[] key = JCSystem.makeTransientByteArray(length, JCSystem.CLEAR_ON_DESELECT);
        generateKey(data, key);

        Util.arrayCopyNonAtomic(key, (short) 0, buffer, (short) 0, (short) length);

        apdu.setOutgoingAndSend((short) 0, length);
    }

    private byte[] generateKey(byte[] Data, byte[] key) {
        short i;
        for (i = 0; i < Data.length; i++) {
            key[i] = (byte) (Data[i] ^ (byte)0x05); 
        } 
        return key;
    }

}

I must send the following APDU command after compiling and selecting my applet:

>>> 80 00 00 00 18 11 22 33 44 55 66 77 88 99 10 20 30 40 50 60 70 80 90 b1 b2 b3 b4 b5 b6 b7 b8

And the expected data that my applet must return is :

<<< 14 27 36 41 50 ... bd 

I have problem with installing this Applet.

I can not install it to my Java card while I am sure that there is no problem with the Java Card version compatibility between my applet and my card. Anyway my program is compiled with Java Card Dev Kit 2.2.1.

It is notable that I created a default Java Card project without any additional code and it installed successfully on my card. It was compiled by JavaCard Development kit version 2.2.1 ,also.

I used the globalPlatformPro to install my applet and it gave me 0x6700 error in status words.

Update 1:

I entered install command with -v -d parameter . OutPut is:

E:\javacard\GlobalPlatformPro>gp -install E:\newPack.cap -v -d
# Detected readers from SunPCSC
[ ] Duali DE-ABCM Contactless Reader 0
[ ] Duali DE-ABCM Sam1 Reader 0
[*] Duali DE-ABCM Sam2 Reader 0
[ ] JAVACOS Virtual Contact Reader 0
[ ] JAVACOS Virtual Contactless Reader 1
SCardConnect("Duali DE-ABCM Sam2 Reader 0", T=*) -> T=0, 3B6B000080653140DC48428
3009000
SCardBeginTransaction("Duali DE-ABCM Sam2 Reader 0")
Reader: Duali DE-ABCM Sam2 Reader 0
ATR: 3B6B000080653140DC484283009000
More information about your card:
    http://smartcard-atr.appspot.com/parse?ATR=3B6B000080653140DC484283009000

A>> T=0 (4+0000) 00A40400 00
A<< (0027+2) (73ms) 6F198408A000000003000000A50D9F6E064842928832319F6501FF 9000
Auto-detected ISD AID: A000000003000000
# ...
# Authentication Process Censored.
# ...
CAP file (v2.1) generated on Mon Jun 29 10:11:23 IRDT 2015
By Sun Microsystems Inc. converter 1.3 with JDK 1.7.0_09 (Oracle Corporation)
Package: newPack v1.2 with AID 1020304050
Applet: newApplet with AID 10203040500000
Import: A0000000620101 v1.2
A>> T=0 (4+0010) 84F28000 0A 4F0031993E2681C862DD 00
A<< (0011+2) (599ms) 08A000000003000000019E 9000
A>> T=0 (4+0010) 84F24000 0A 4F00EFCEFA0D825EAC3B 00
A<< (0011+2) (91ms) 08AABBCCDDEEFF00000700 9000
A>> T=0 (4+0010) 84F22000 0A 4F0050316231A15388BE 00
A<< (0143+2) (277ms) 08D156000027100106010008D156000027100101010006A000000151000
10007A0000000030000010008D156000027100102010007A0000000620201010007A000000062010
2010008A000000062010101010007A0000000620101010007A0000000620002010007A0000000620
003010007A0000000620001010008D156000027100108010006AABBCCDDEEFF0100 9000
A>> T=0 (4+0010) 84F21000 0A 4F00358E1689D8208F09 00
A<< (0176+2) (331ms) 08D15600002710010601000008D15600002710010101000006A00000015
10001000007A000000003000001000008D15600002710010201000007A000000062020101000007A
000000062010201000008A00000006201010101000007A000000062010101000007A000000062000
201000007A000000062000301000007A000000062000101000008D15600002710010801000109D15
60000271001080106AABBCCDDEEFF01000108AABBCCDDEEFF0000 9000
A>> T=0 (4+0026) 84E60200 1A 05102030405008A0000000030000000000005DC20F51A0D4CA2
9
A<< (0001+2) (474ms) 9000
A>> T=0 (4+0255) 84E80000 FF C482018E01000FDECAFFED010204020105102030405002001F0
00F001F000B000B003A000C00CA000A00150000008700000000000001010004000B01020107A0000
00062010103000B010710203040500000000806000C00800300FF0007010000001C0700CA0001101
88C00007A05308F00013D8C0002181D0441181D258B00037A0525188B000460037A198B00053B198
B00062D1A03251100FF535B321A04251100FF535B29041A07251100FF5329051605058D000728061
F10806A08116E008D0008160473001D0000000000091A0815060316058D00093B181915068C000A7
008116D008D00087A0533198B00062E1A9229041604AA59A4DD02FCF125
A<< (0000+2) (792ms) 6700
pro.javacard.gp.GPException: LOAD failed SW: 6700
        at pro.javacard.gp.GlobalPlatform.check(GlobalPlatform.java:1092)
        at pro.javacard.gp.GlobalPlatform.loadCapFile(GlobalPlatform.java:728)
        at pro.javacard.gp.GlobalPlatform.loadCapFile(GlobalPlatform.java:686)
        at pro.javacard.gp.GPTool.main(GPTool.java:453)

Is there something wrong with my applet? What is the issue?

1
please add output of installation process, when you use -d -v arguments in the install command. I.e output of gp.exe -install E:\keyGeneratorPackage.cap -v -dEbrahim Ghasemi
Have you tried loading another .cap file with the same tool, reader and card? Some readers and cards do not like APDUs of data part length 255. You could perhaps set a lower block size for applet installation, if possible.vojta
I installed my applet via gpj successfully. But When I send command for send data and recieve changed data via Opensc tools, it returns Invalid data that means command is not correct.What is issue? I will Update my question with output I recieved in the gpj platform.setare ab
Wait! Did you installed above applet successfully with GPJ (not GlobalPlatformPro)?Ebrahim Ghasemi
1 - Why you are using new keyGeneratorPackage.KeyGeneratorPackage() Just use new KeyGeneratorPackage() 2 - Some Javacards based on their transfer protocol differ in registering the applet, Try Registering the applet without parameters and tell us the results Use new KeyGeneratorPackage(bArray, bOffset, bLength) in install method, then create a constructor and use register() in that, don't forget not to send any parameter in register methodHana Bzh

1 Answers

0
votes

I could install my applet on the my smartcard successfully but via gpj platform. The output installing in gpj platform is:

Microsoft Windows [Version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation.  All rights reserved.

C:\Users\Ehsan>java -jar E:\javacard\gpj\gpj.jar -load E:\keyGeneratorPackage.ca
p -install
Found terminals: [PC/SC terminal Duali DE-ABCM Contactless Reader 0, PC/SC termi
nal Duali DE-ABCM Sam1 Reader 0, PC/SC terminal Duali DE-ABCM Sam2 Reader 0, PC/
SC terminal JAVACOS Virtual Contact Reader 0, PC/SC terminal JAVACOS Virtual Con
tactless Reader 1]
javax.smartcardio.CardNotPresentException: No card present
        at sun.security.smartcardio.TerminalImpl.connect(Unknown Source)
        at net.sourceforge.gpj.cardservices.GlobalPlatformService.main(Unknown S
ource)
Caused by: sun.security.smartcardio.PCSCException: SCARD_W_REMOVED_CARD
        at sun.security.smartcardio.PCSC.SCardConnect(Native Method)
        at sun.security.smartcardio.CardImpl.<init>(Unknown Source)
        ... 2 more
Found card in terminal: Duali DE-ABCM Contactless Reader 0
java.lang.NullPointerException
        at net.sourceforge.gpj.cardservices.GlobalPlatformService.main(Unknown S
ource)
javax.smartcardio.CardNotPresentException: No card present
        at sun.security.smartcardio.TerminalImpl.connect(Unknown Source)
        at net.sourceforge.gpj.cardservices.GlobalPlatformService.main(Unknown S
ource)
Caused by: sun.security.smartcardio.PCSCException: SCARD_W_REMOVED_CARD
        at sun.security.smartcardio.PCSC.SCardConnect(Native Method)
        at sun.security.smartcardio.CardImpl.<init>(Unknown Source)
        ... 2 more
Found card in terminal: Duali DE-ABCM Sam1 Reader 0
java.lang.NullPointerException
        at net.sourceforge.gpj.cardservices.GlobalPlatformService.main(Unknown S
ource)
Found card in terminal: Duali DE-ABCM Sam2 Reader 0
ATR: 3B 6B 00 00 80 65 31 40 DC 48 42 83 00 90 00
DEBUG: Command  APDU: 00 A4 04 00 07 A0 00 00 01 51 00 00
DEBUG: Response APDU: 6A 82
Failed to select Security Domain GP211 A0 00 00 01 51 00 00 , SW: 6A 82
DEBUG: Command  APDU: 00 A4 04 00 08 A0 00 00 00 18 43 4D 00
DEBUG: Response APDU: 6A 82
Failed to select Security Domain GemaltoXpressPro A0 00 00 00 18 43 4D 00 , SW:
6A 82
DEBUG: Command  APDU: 00 A4 04 00 08 A0 00 00 00 03 00 00 00
DEBUG: Response APDU: 6F 19 84 08 A0 00 00 00 03 00 00 00 A5 0D 9F 6E 06 48 42 9
2 88 32 31 9F 65 01 FF 90 00
Successfully selected Security Domain OP201a A0 00 00 00 03 00 00 00
DEBUG: Command  APDU: 80 50 00 00 08 BF A2 14 ED 0F E0 4A E0
DEBUG: Response APDU: 00 00 1E 60 31 80 00 01 77 7B 01 02 00 CD E9 F9 F5 18 BC A
3 C9 F1 59 25 9A 38 DD 3C 90 00
DEBUG: Command  APDU: 84 82 00 00 10 BD 78 CC 1D 5B 7F E5 46 D4 6C 02 D4 0D 32 4
6 E3
DEBUG: Response APDU: 90 00
DEBUG: Command  APDU: 84 82 00 00 08 BD 78 CC 1D 5B 7F E5 46
DEBUG: Response APDU: 90 00
DEBUG: packagePath: keyGeneratorPackage/javacard/
DEBUG: package: keyGeneratorPackage
DEBUG: package AID: 1C 2C 3C 4C 5C
DEBUG: applet AIDs: [1C 2C 3C 4C 5C 00 ]
DEBUG: Command  APDU: 80 E6 02 00 12 05 1C 2C 3C 4C 5C 08 A0 00 00 00 03 00 00 0
0 00 00 00
DEBUG: Response APDU: 00 90 00
DEBUG: Command  APDU: 80 E6 02 00 12 05 1C 2C 3C 4C 5C 08 A0 00 00 00 03 00 00 0
0 00 00 00
DEBUG: Response APDU: 00 90 00
DEBUG: Command  APDU: 80 E8 00 00 FF C4 82 01 86 01 00 0F DE CA FF ED 01 02 04 0
1 01 05 1C 2C 3C 4C 5C 02 00 1F 00 0F 00 1F 00 0A 00 0B 00 3A 00 0C 00 C3 00 0A
00 15 00 00 00 87 00 00 00 00 00 00 01 01 00 04 00 0B 01 02 01 07 A0 00 00 00 62
 01 01 03 00 0A 01 06 1C 2C 3C 4C 5C 00 00 0C 06 00 0C 00 80 03 00 FF 00 07 01 0
0 00 00 15 07 00 C3 00 01 10 18 8C 00 00 18 8B 00 01 7A 01 30 8F 00 02 8C 00 03
7A 05 25 18 8B 00 04 60 03 7A 19 8B 00 05 3B 19 8B 00 06 2D 1A 03 25 11 00 FF 53
 5B 32 1A 04 25 11 00 FF 53 5B 29 04 1A 07 25 11 00 FF 53 29 05 16 05 05 8D 00 0
7 28 06 1F 10 80 6A 08 11 6E 00 8D 00 08 16 04 73 00 1D 00 00 00 00 00 09 1A 08
15 06 03 16 05 8D 00 09 3B 18 19 15 06 8C 00 0A 70 08 11 6D 00 8D 00 08 7A 05 33
 19 8B 00 06 2E 1A 92 29 04 16 04 05 8D 00 07 28 05 18 1A 15 05 8C 00 0B 3B 15 0
5
DEBUG: Response APDU: 90 00
DEBUG: Command  APDU: 80 E8 00 00 FF C4 82 01 86 01 00 0F DE CA FF ED 01 02 04 0
1 01 05 1C 2C 3C 4C 5C 02 00 1F 00 0F 00 1F 00 0A 00 0B 00 3A 00 0C 00 C3 00 0A
00 15 00 00 00 87 00 00 00 00 00 00 01 01 00 04 00 0B 01 02 01 07 A0 00 00 00 62
 01 01 03 00 0A 01 06 1C 2C 3C 4C 5C 00 00 0C 06 00 0C 00 80 03 00 FF 00 07 01 0
0 00 00 15 07 00 C3 00 01 10 18 8C 00 00 18 8B 00 01 7A 01 30 8F 00 02 8C 00 03
7A 05 25 18 8B 00 04 60 03 7A 19 8B 00 05 3B 19 8B 00 06 2D 1A 03 25 11 00 FF 53
 5B 32 1A 04 25 11 00 FF 53 5B 29 04 1A 07 25 11 00 FF 53 29 05 16 05 05 8D 00 0
7 28 06 1F 10 80 6A 08 11 6E 00 8D 00 08 16 04 73 00 1D 00 00 00 00 00 09 1A 08
15 06 03 16 05 8D 00 09 3B 18 19 15 06 8C 00 0A 70 08 11 6D 00 8D 00 08 7A 05 33
 19 8B 00 06 2E 1A 92 29 04 16 04 05 8D 00 07 28 05 18 1A 15 05 8C 00 0B 3B 15 0
5
DEBUG: Response APDU: 90 00
DEBUG: Command  APDU: 80 E8 80 01 8B 03 1B 03 16 04 8D 00 0C 3B 19 03 16 04 8B 0
0 0D 7A 04 31 03 32 70 0D 1A 1F 19 1F 25 08 57 38 59 03 01 1F 19 92 6C F2 1A 77
08 00 0A 00 00 00 00 00 00 00 00 00 00 05 00 3A 00 0E 06 80 03 00 03 80 03 01 01
 00 00 00 06 00 00 01 03 80 03 03 03 80 0A 06 03 80 0A 01 06 80 08 0D 06 80 07 0
1 06 80 10 01 06 00 00 7D 06 00 00 AB 06 80 10 02 03 80 0A 08 09 00 15 00 00 00
11 05 04 06 03 07 07 05 23 0D 15 08 08 07 0B 09 0B 08
DEBUG: Response APDU: 00 90 00
DEBUG: Command  APDU: 80 E8 80 01 8B 03 1B 03 16 04 8D 00 0C 3B 19 03 16 04 8B 0
0 0D 7A 04 31 03 32 70 0D 1A 1F 19 1F 25 08 57 38 59 03 01 1F 19 92 6C F2 1A 77
08 00 0A 00 00 00 00 00 00 00 00 00 00 05 00 3A 00 0E 06 80 03 00 03 80 03 01 01
 00 00 00 06 00 00 01 03 80 03 03 03 80 0A 06 03 80 0A 01 06 80 08 0D 06 80 07 0
1 06 80 10 01 06 00 00 7D 06 00 00 AB 06 80 10 02 03 80 0A 08 09 00 15 00 00 00
11 05 04 06 03 07 07 05 23 0D 15 08 08 07 0B 09 0B 08
DEBUG: Response APDU: 00 90 00
DEBUG: Command  APDU: 80 E6 0C 00 1A 05 1C 2C 3C 4C 5C 06 1C 2C 3C 4C 5C 00 06 1
C 2C 3C 4C 5C 00 01 00 02 C9 00 00
DEBUG: Response APDU: 00 90 00
DEBUG: Command  APDU: 80 E6 0C 00 1A 05 1C 2C 3C 4C 5C 06 1C 2C 3C 4C 5C 00 06 1
C 2C 3C 4C 5C 00 01 00 02 C9 00 00
DEBUG: Response APDU: 00 90 00
javax.smartcardio.CardNotPresentException: No card present
        at sun.security.smartcardio.TerminalImpl.connect(Unknown Source)
        at net.sourceforge.gpj.cardservices.GlobalPlatformService.main(Unknown S
ource)
Caused by: sun.security.smartcardio.PCSCException: SCARD_W_REMOVED_CARD
        at sun.security.smartcardio.PCSC.SCardConnect(Native Method)
        at sun.security.smartcardio.CardImpl.<init>(Unknown Source)
        ... 2 more
Found card in terminal: JAVACOS Virtual Contact Reader 0
java.lang.NullPointerException
        at net.sourceforge.gpj.cardservices.GlobalPlatformService.main(Unknown S
ource)
javax.smartcardio.CardNotPresentException: No card present
        at sun.security.smartcardio.TerminalImpl.connect(Unknown Source)
        at net.sourceforge.gpj.cardservices.GlobalPlatformService.main(Unknown S
ource)
Caused by: sun.security.smartcardio.PCSCException: SCARD_W_REMOVED_CARD
        at sun.security.smartcardio.PCSC.SCardConnect(Native Method)
        at sun.security.smartcardio.CardImpl.<init>(Unknown Source)
        ... 2 more
Found card in terminal: JAVACOS Virtual Contactless Reader 1
java.lang.NullPointerException
        at net.sourceforge.gpj.cardservices.GlobalPlatformService.main(Unknown S
ource)

C:\Users\Ehsan>

I could not install my applet on the smartcard via globalPlatformPro.I dont know the reason ,anyway output of installing command in the this platform is :

E:\javacard\GlobalPlatformPro>gp -install E:\keyGeneratorPackage.cap

pro.javacard.gp.GPException: LOAD failed SW: 6700
        at pro.javacard.gp.GlobalPlatform.check(GlobalPlatform.java:1092)
        at pro.javacard.gp.GlobalPlatform.loadCapFile(GlobalPlatform.java:728)
        at pro.javacard.gp.GlobalPlatform.loadCapFile(GlobalPlatform.java:686)
        at pro.javacard.gp.GPTool.main(GPTool.java:453)

E:\javacard\GlobalPlatformPro>

This is the list of applets on the my card which last four AID and first four AID are my apps package and apps applets installed via gpj.

E:\javacard\GlobalPlatformPro>
E:\javacard\GlobalPlatformPro>gp -l
AID: A000000003000000 (|........|)
     ISD OP_READY: Security Domain, Card lock, Card terminate, Default selected,
 CVM (PIN) management

AID: AABBCCDDEEFF0000 (|........|)
     App SELECTABLE: (none)

AID: 010203040506070809AABB (|...........|)
     App SELECTABLE: (none)

AID: A9A8A7A6A5A400 (|.......|)
     App SELECTABLE: (none)

AID: 1C2C3C4C5C00 (|.,<L\.|)
     App SELECTABLE: (none)

AID: D156000027100106 (|.V..'...|)
     ExM LOADED: (none)

AID: D156000027100101 (|.V..'...|)
     ExM LOADED: (none)

AID: A00000015100 (|....Q.|)
     ExM LOADED: (none)

AID: A0000000030000 (|.......|)
     ExM LOADED: (none)

AID: D156000027100102 (|.V..'...|)
     ExM LOADED: (none)

AID: A0000000620201 (|....b..|)
     ExM LOADED: (none)

AID: A0000000620102 (|....b..|)
     ExM LOADED: (none)

AID: A000000062010101 (|....b...|)
     ExM LOADED: (none)

AID: A0000000620101 (|....b..|)
     ExM LOADED: (none)

AID: A0000000620002 (|....b..|)
     ExM LOADED: (none)

AID: A0000000620003 (|....b..|)
     ExM LOADED: (none)

AID: A0000000620001 (|....b..|)
     ExM LOADED: (none)

AID: D156000027100108 (|.V..'...|)
     ExM LOADED: (none)
     D15600002710010801 (|.V..'....|)

AID: AABBCCDDEEFF (|......|)
     ExM LOADED: (none)
     AABBCCDDEEFF0000 (|........|)

AID: 010203040506070809 (|.........|)
     ExM LOADED: (none)
     010203040506070809AABB (|...........|)

AID: A9A8A7A6A5A4 (|......|)
     ExM LOADED: (none)
     A9A8A7A6A5A400 (|.......|)

AID: 1C2C3C4C5C (|.,<L\|)
     ExM LOADED: (none)
     1C2C3C4C5C00 (|.,<L\.|)


E:\javacard\GlobalPlatformPro>

This is my final applet code:

package keyGeneratorPackage;

import javacard.framework.APDU;
import javacard.framework.Applet;
import javacard.framework.ISO7816;
import javacard.framework.ISOException;
import javacard.framework.JCSystem;
import javacard.framework.Util;

public class keyGeneratorApplet extends Applet {
    /* constants declaration */
    // code of CLA byte in the command APDU header

    // codes of INS byte in the command APDU header
    private static final byte HW_CLA = (byte) 0x80;
    private static final byte HW_INS = (byte) 0x00;
    private keyGeneratorApplet() {
        register();
    }

    public static void install(byte bArray[], short bOffset, byte bLength)
            throws ISOException {
        new keyGeneratorApplet();
    }


    public void process(APDU apdu) {
        if (selectingApplet()) {
            return;
        }
        apdu.setIncomingAndReceive();
        byte[] buffer = apdu.getBuffer();
        byte CLA = (byte) (buffer[ISO7816.OFFSET_CLA] & 0xFF);
        byte INS = (byte) (buffer[ISO7816.OFFSET_INS] & 0xFF);
        short  lc =  (short) (buffer[ISO7816.OFFSET_LC] & (short)0x00FF); 
//      byte[] Data = new byte[(byte) (buffer[ISO7816.OFFSET_LC] & 0xFF)];
        byte[] Data = JCSystem.makeTransientByteArray(lc, JCSystem.CLEAR_ON_DESELECT);

        if (CLA != HW_CLA) {
            ISOException.throwIt(ISO7816.SW_CLA_NOT_SUPPORTED);
        }
        switch (INS) {
        case HW_INS: {
            // copying the apdu data into byte array Data
            Util.arrayCopy(buffer, ISO7816.OFFSET_CDATA, Data, (short) 0, lc);
            getKey(apdu, Data);
        }
        // you forget to put a break here!
        break;
        default:
            // good practice: If you don't know the INStruction, say so:
            ISOException.throwIt(ISO7816.SW_INS_NOT_SUPPORTED);
        }
    }

    private void getKey(APDU apdu, byte[] data) {
        byte[] buffer = apdu.getBuffer();
        short length = (short) data.length;
        //prepareing the key array of same length of Data
        byte[] key = JCSystem.makeTransientByteArray(length, JCSystem.CLEAR_ON_DESELECT);
//      byte[] generatedKey = generateKey(data, key);
        // no need another array generatedKey, as we are passing key as parameter 
        generateKey(data, key);
//      length = (short) generatedKey.length;

        Util.arrayCopyNonAtomic(key, (short) 0, buffer, (short) 0, (short) length);

        apdu.setOutgoingAndSend((short) 0, length);
    }

    // .....................................
    private byte[] generateKey(byte[] Data, byte[] key) {
        short i;
        for (i = 0; i < Data.length; i++) {
            // i've no idea why you use 0x05 here,
            // in the question you mentioned 0x9D
            key[i] = (byte) (Data[i] ^ (byte)0x05); 
        } 
        return key;
    }
}

And for sending my commands to the my applet which is installed on the card ,I used from OpenSc tools . Inputs and outputs are here:

E:\javacard\opensc-tools\OpenSC-master>opensc-tool.exe -r"Duali DE-ABCM Sam2 Rea
der 0" -s 00a40400061c2c3c4c5c00 -s 80000000181122334455667788991020304050607080
90B1B2B3B4B5B6B7
Sending: 00 A4 04 00 06 1C 2C 3C 4C 5C 00
Received (SW1=0x90, SW2=0x00)
Sending: 80 00 00 00 18 11 22 33 44 55 66 77 88 99 10 20 30 40 50 60 70 80 90 B1
 B2 B3 B4 B5 B6 B7
Received (SW1=0x90, SW2=0x00):
14 27 36 41 50 63 72 8D 9C 15 25 35 45 55 65 75 .'6APcr...%5EUeu
85 95 B4 B7 B6 B1 B0 B3                         ........

E:\javacard\opensc-tools\OpenSC-master>