I have encountered a problem when testing my applet, which implemented AES alg.
When running to the following line, it always breaks.
Code: Select all
aeskey = (AESKey) KeyBuilder.buildKey(KeyBuilder.TYPE_AES, KeyBuilder.LENGTH_AES_128, false);
My code:
Code: Select all
package com.cpit.javacard;
import javacard.framework.APDU;
import javacard.framework.Applet;
import javacard.framework.ISO7816;
import javacard.framework.ISOException;
import javacard.security.AESKey;
import javacard.security.DESKey;
import javacard.security.Key;
import javacard.security.KeyBuilder;
import javacardx.crypto.Cipher;
public class DES3 extends Applet
{
final static byte AES = (byte) 0x50;
public static void install(byte[] bArray, short bOffset, byte bLength)
{
new DES3().register(bArray, (short) (bOffset + 1), bArray[bOffset]);
}
public boolean select()
{
return true;
}
public void deselect()
{
// reset the pin value
}
public void process(APDU apdu)
{
if (selectingApplet())
{
return;
}
byte[] buf = apdu.getBuffer();
switch (buf[ISO7816.OFFSET_INS])
{
case AES:
Crypt_AES(apdu);
break;
default:
ISOException.throwIt(ISO7816.SW_INS_NOT_SUPPORTED);
}
}
private void Crypt_AES(APDU apdu)
{
byte[] buffer = apdu.getBuffer();
Cipher CipherObj;
AESKey aeskey;
byte[] dkey = { (byte) 0x01, (byte) 0x02, (byte) 0x03, (byte) 0x04, (byte) 0x05, (byte) 0x06, (byte) 0x07, (byte) 0x08,
(byte) 0x01, (byte) 0x02, (byte) 0x03, (byte) 0x04, (byte) 0x05, (byte) 0x06, (byte) 0x07, (byte) 0x08 };
byte[] cleartext = { (byte) 0x77, (byte) 0xA7, (byte) 0xd6, (byte) 0xbc, (byte) 0xf5, (byte) 0x79, (byte) 0x62, (byte) 0xb9,
(byte) 0x77, (byte) 0xA7, (byte) 0xd6, (byte) 0xbc, (byte) 0xf5, (byte) 0x79, (byte) 0x62, (byte) 0xb9 };
CipherObj = Cipher.getInstance(Cipher.ALG_AES_BLOCK_128_CBC_NOPAD, false);
aeskey = (AESKey) KeyBuilder.buildKey(KeyBuilder.TYPE_AES, KeyBuilder.LENGTH_AES_128, false);
aeskey.setKey(dkey, (short) 0);
CipherObj.init((Key) aeskey, Cipher.MODE_ENCRYPT);
apdu.setOutgoingLength((byte) 0x10);
CipherObj.doFinal(cleartext, (short) 0, (byte) 0x10, buffer, (short) 0);
apdu.sendBytes((short) 0, (byte) 0x10);
}
}