# Why is there an extra block when encrypting?

I am passing input data 20 bytes long and java AES-CBC returns 48 bytes instead of 32 which is what I think it should output because of padding. My key is 16 bytes long.

  byte[] ciphertext;

Cipher enc = Cipher.getInstance("AES/CBC/PKCS5Padding");

enc.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(key, "AES"), new IvParameterSpec(vector));
ciphertext = encrypt.doFinal(data.getbytes("UTF-8"));

-
Could you show the content of the data variable? –  Nova Nov 9 at 11:17
In particular if data is String, it cannot be 20 bytes. Java String contains char and Java char is NOT a byte, although it SOMETIMES encodes as one. If you mean it has 20 chars, probably some of them are >=U+0080 and therefore encode to multiple bytes, enough to make the total at least 32 bytes. –  dave_thompson_085 Nov 9 at 12:55
data is c6be25d903159d680d81f3d99bb702451e9f7158 and is stored as byte[] data= c6be25d903159d680d81f3d99bb702451e9f7158.get bytes() –  ds123456 Nov 9 at 14:38
Identical question on SO: stackoverflow.com/questions/26824828/… –  Artjom B. Nov 9 at 14:47

## 1 Answer

You should get the IV of CBC as the first ciphertext block.

Wikipedia: CBC

-
Nope. CBC treats IV as if it were block zero, and you must transmit or store IV along with ciphertext, and this is sometimes done simply by concatenating IV with ciphertext -- but not in Java crypto, which treats them as separate fields. –  dave_thompson_085 Nov 9 at 12:51
That's exactly what I was thinking, but apperantly it's not true, despite making sense. –  Nova Nov 9 at 13:51