Page 1 of 1

the length of cipher

Posted: Sat Jul 16, 2016 2:26 am
by tykeal
Hey!

I encountered a problem. I am trying to encrypt a byte array with length 8. But the length of cipher I obtained is 16. How can I make this to be 8?

In the following code, temp length is 8, and out's length is 16.

Code: Select all

Cipher cipherinstance = Cipher.getInstance(Cipher.ALG_DES_CBC_ISO9797_M2, false);
DESKey deskey = (DESKey)KeyBuilder.buildKey(KeyBuilder.TYPE_DES, KeyBuilder.LENGTH_DES, false);
deskey.setKey(KEY_8_BYTES, (short)0);
out = cipherinstance.doFinal(temp, (short)0, (short)temp.length, res, (short)0);

Re: the length of cipher

Posted: Sat Jul 16, 2016 4:34 am
by chico0609
Use Cipher.ALG_DES_CBC_NOPAD to replace Cipher.ALG_DES_CBC_ISO9797_M2.

Re: the length of cipher

Posted: Sat Jul 16, 2016 5:08 am
by wousim
That is just due to the cipher padding method. You should be able to safely ignore the last 8 bytes though as CBC mode does not cause the first block to be different even though there is additional padding.