I am trying to encrypt data using Cipher class . I want to specify the initial vector so I use the following functions :
try {
cipherCBC = Cipher.getInstance(Cipher.ALG_DES_CBC_NOPAD, false);
cipherCBC.init(k, Cipher.MODE_ENCRYPT,INITVECTOR,(short)0,(short)8);
cipherCBC.doFinal(data, (short) 0, (short) data.length, result, (short) 0);
} catch (Exception e) {
ISOException.throwIt(ISO7816.SW_CONDITIONS_NOT_SATISFIED);
}
with the byte array INITVECTOR initialised by a 8-byte array.
The problem is that I get always an exception caught when I use the init function.
EXTRA INFO:
The key is build here :
octetsLus = (byte) (apdu.setIncomingAndReceive());
if (octetsLus != 16) {
ISOException.throwIt(ISO7816.SW_WRONG_LENGTH);
}
// build host crypto
Util.arrayCopy(buffer, (short)(ISO7816.OFFSET_CDATA+4), message, (short) 0,(short) 4);
Util.arrayCopy(buffer, (short)(ISO7816.OFFSET_CDATA+8), message, (short) 4,(short) 4);
Util.arrayCopy(buffer, (short)(ISO7816.OFFSET_CDATA), message, (short) 8,(short) 4);
Util.arrayCopy(buffer, (short)(ISO7816.OFFSET_CDATA+12), message, (short) 12,(short) 4);
// GENERATE SESSION KEYS
encrypt_DES(ENC_key, message,(byte) 0x01);
Util.arrayCopy(result,(short) 0, ENC_SESS_KEY, (short) 0,(short) 16);
encrypt_DES(MAC_key, message,(byte) 0x01);
Util.arrayCopy(result,(short) 0, MAC_SESS_KEY, (short) 0,(short) 16);
ENC_key = (DESKey) KeyBuilder.buildKey(KeyBuilder.TYPE_DES, KeyBuilder.LENGTH_DES3_2KEY, false);
ENC_key.setKey(ENC_SESS_KEY, (short) 0);
MAC_key = (DESKey) KeyBuilder.buildKey(KeyBuilder.TYPE_DES, KeyBuilder.LENGTH_DES3_2KEY, false);
MAC_key.setKey(MAC_SESS_KEY, (short) 0);
Util.arrayCopy(buffer, (short)(ISO7816.OFFSET_CDATA), message, (short) 0,(short) 8);
Util.arrayCopy(buffer, (short)(ISO7816.OFFSET_CDATA+8), message, (short) 8,(short) 8);
for(i=0;i<8;i++)
message[(short)(16+i)]=(byte)PADDING[i];
Concerning the initial vector, even if I use the following initialization, I get the same problem :
INITVECTOR =new byte[]{(byte)0x00,(byte)0x00,(byte)0x00,(byte)0x00,(byte)0x00,(byte)0x00,(byte)0x00,(byte)0x00};
On the other hand, when I use the init function which use default initialization parameters the encrypt function works well:
cipherCBC.init(k, Cipher.MODE_ENCRYPT);