3

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);
vojta
  • 5,591
  • 2
  • 24
  • 64
Sara Sara
  • 299
  • 1
  • 6
  • 14
  • Could you please post the exception (full stack trace)? And also the actual initialization of INITVECTOR. – jjmontes Feb 24 '16 at 18:24
  • You should add more of your code, especially those parts where key and iv are initialized. – vojta Feb 24 '16 at 20:50
  • 2
    @jjmontes This is Java Card, not Java. The only information you can access is the type of the exception and its reason (output of `getReason()` method). That is what the OP should add. – vojta Feb 25 '16 at 08:50
  • Thank you for additional info. You should also find out what the type of the exception is using `instanceof` operator and throw `getReason()` output as the status word. – vojta Feb 25 '16 at 09:37
  • 1
    It's OK I found the problem, I the length of data was incorrect, thank you @vojta for you're help ! – Sara Sara Feb 25 '16 at 10:18
  • 3
    Please post your own answer and mark as a solution. – Paul Bastian Feb 26 '16 at 13:50
  • Glad this got solved. @PaulBastian I've voted to close this question as a simple error. That the input of the function has the wrong length isn't very helpful for future readers. – Maarten Bodewes Feb 28 '16 at 12:00

0 Answers0