My applet returns 16 bytes of 0 instead of AES encrypted value,why?
Posted: Mon Nov 30, 2015 2:54 am
I am writing a java card applet to encrypt 16 bytes of incoming APDU command data section and return the encrypted value.
My code:
The sending / received APDUs are as follows
>> 00 A4 04 00 06 81 82 83 84 85 01
<< 90 00
>> 80 12 00 00 10 00 11 22 33 44 55 66 77 88 99 AA BB CC DD EE FF
<< 90 00
>> 80 C0 00 00
<< 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ...
Why my applet returns 16 bytes of 0 instead of AES encrypted value?
My code:
Code: Select all
public class DoAES extends Applet {
static Cipher myCipher;
static AESKey myAESKey;
byte[] cipheredData = JCSystem.makeTransientByteArray((short) 0x10, JCSystem.CLEAR_ON_RESET);
final static byte SET_KEY = (byte) 0x12;
final static byte WRITE_TEXT = (byte) 0x04;
final static byte READ_TEXT = (byte) 0xC0;
private DoAES() {
try {
myCipher = Cipher.getInstance(Cipher.ALG_AES_BLOCK_128_ECB_NOPAD, false);
myAESKey = (AESKey) KeyBuilder.buildKey(KeyBuilder.TYPE_AES,
KeyBuilder.LENGTH_AES_128, false);
} catch (CryptoException e) {
ISOException.throwIt(((CryptoException) e).getReason());
}
}
public static void install(byte bArray[], short bOffset, byte bLength)
throws ISOException {
(new DoAES()).register();
}
public void process(APDU apdu) throws ISOException {
if (selectingApplet()) {
return;
}
byte[] buffer = apdu.getBuffer();
if ((buffer[ISO7816.OFFSET_CLA] & 0x00FF) != 0x80) {
ISOException.throwIt(ISO7816.SW_CLA_NOT_SUPPORTED);
}
try {
switch (buffer[ISO7816.OFFSET_INS]) {
case SET_KEY:
myAESKey.setKey(buffer, (short) ISO7816.OFFSET_CDATA);
myCipher.init(myAESKey, Cipher.MODE_ENCRYPT);
break;
case WRITE_TEXT:
myCipher.doFinal(buffer, (short) ISO7816.OFFSET_CDATA, (short) 0x10, cipheredData, (short) 0);
break;
case READ_TEXT:
Util.arrayCopyNonAtomic(cipheredData, (short) 0, buffer, (short) 0, (short) 0x10);
apdu.setOutgoingAndSend((short) 0, (short) 0x10);
break;
default:
ISOException.throwIt(ISO7816.SW_INS_NOT_SUPPORTED);
}
} catch (CryptoException e) {
ISOException.throwIt(((CryptoException) e).getReason());
}
}
}
The sending / received APDUs are as follows
>> 00 A4 04 00 06 81 82 83 84 85 01
<< 90 00
>> 80 12 00 00 10 00 11 22 33 44 55 66 77 88 99 AA BB CC DD EE FF
<< 90 00
>> 80 C0 00 00
<< 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ...
Why my applet returns 16 bytes of 0 instead of AES encrypted value?