Update 1: I installed my applet on the javacard(I used from the source code which is as accepted answer In my already question) .When I send generatedKey command via OpenSc ,It just returns 9000 as response instead of sending XORed Data! I created my project with Javacard version 2.2.1 and i am certain that my card is compattible with that version. Why expected data not recieved by OpenSc?
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.*;
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().register(bArray, (short) (bOffset + 1),
bArray[bOffset]);
}
public void process(APDU apdu) {
if (selectingApplet()) {
return;
}
byte[] buffer = apdu.getBuffer();
byte CLA = (byte) (buffer[ISO7816.OFFSET_CLA] & 0xFF);
byte INS = (byte) (buffer[ISO7816.OFFSET_INS] & 0xFF);
byte[] Data = new byte[] { (byte) (buffer[ISO7816.OFFSET_CDATA] & 0xFF) };
if (CLA != HW_CLA) {
ISOException.throwIt(ISO7816.SW_CLA_NOT_SUPPORTED);
}
switch (INS) {
case HW_INS:
getKey(apdu, Data);
break;
default:
ISOException.throwIt(ISO7816.SW_INS_NOT_SUPPORTED);
}
}
private void getKey(APDU apdu, byte[] data) {
byte[] buffer = apdu.getBuffer();
byte[] generatedKey = generateKey(data);
short length = (short) generatedKey.length;
Util.arrayCopyNonAtomic(generatedKey, (short) 0, buffer, (short) 0,
(short) length);
apdu.setOutgoingAndSend((short) 0, length);
}
private byte[] generateKey(byte[] Data) {
byte[] key = new byte[] { (byte) 0x00 };
for (int i = 0; i < Data.length; i++) {
key[i] = (byte) (Data[i] ^ 5);
}
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 26
Is there something wrong with my applet?
In method,
private void getKey( APDU apdu , byte[] data)
you need to call,Remember:
So update your method like this:
}
Note:
setIncomingAndReceive
method may only be called once in a Applet.process() method. For more detail, read setIncomingAndReceive.EDIT: There are several problems in your code. I'm mentioning all of them one by one.
Problem 1:
byte[] Data =new byte[] {(byte) (buffer[ISO7816.OFFSET_CDATA] & 0xFF)};
It creates a
byte[] Data
of length 1 with value0x11
.Solution:
new
creates space into persistent EEP memory forData
. If you don't needData
again you can make ittransient
byte array.Rewrite it like this (Persistent):
Or this (Transient):
Problem 2:
i) Your
generateKey()
method will crash, because you are creatingbyte[] key
same as you do forbyte[] Data
.ii) You may not declare
int i
because only few cards supports it, usebyte
orshort
.Solution: As far as I understand what are you trying to do in the
generateKey()
method, I rewrite it for you like this:The full working code is:
JavaCard: v.2.2.2
globalPlatform: v.2.1.1
Suggestion: Read this document carefully first.
APDU's I sent :
Cheers!