I'm trying to encrypt a key imported in my card and store it again in the same byte array. The import_key function below receives an apdu and extracts the message KEY, then tries to encrypt it (simple DES encryption) and store it at the same place. It seems that I have done everything necessary but unfortunately I receive 6f00 every time I uncomment the encryption part!!! this is my code:
public class Encryptor extends Applet {
private byte[] KEY = new byte[128];
private byte key_len = (byte)0;
private byte[] ENC_KEY = new byte[32];
private byte enc_key_len = (byte)0;
Cipher MyCipher;
private byte[] TheDES_Key = new byte[24];
DESKey MyDES1Key = (DESKey) KeyBuilder.buildKey(KeyBuilder.TYPE_DES,KeyBuilder.LENGTH_DES, false);
byte ConfiguredKeyLength = 0;
{...}
private void import_key(APDU apdu) {
byte[] buffer = new byte[maximum_buffer_len];
buffer = apdu.getBuffer();
key_len = buffer[ISO7816.OFFSET_LC];
apdu.setIncomingAndReceive();
Util.arrayCopy(buffer, (short) (ISO7816.OFFSET_CDATA), KEY, (short) 0, (short) key_len);
Util.arrayCopyNonAtomic(ENC_KEY, (short) 0, TheDES_Key, (short) 0, (short) enc_key_len);
MyDES1Key.setKey(TheDES_Key, (short) 0);
MyCipher.init(MyDES1Key, Cipher.MODE_ENCRYPT);
byte[] CipheredData = JCSystem.makeTransientByteArray((short) 32,JCSystem.CLEAR_ON_DESELECT);
MyCipher.doFinal(KEY, (short)0,(short)key_len,CipheredData,(short)0);
Util.arrayCopyNonAtomic(CipheredData,(short)0,KEY,(short)0,(short)key_len);
return;
}
}