Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What size of initialization vector needed for AES-256 encryption in java?

I am using AES-256 encryption with CFB mode. I have to use 32 byte key. But I am little bit confused about the initialization vector. How many bytes of initialization vector should be used with AES-256 ? Is it 16 bytes or 32 bytes ?

Any code example will be appreciated.

like image 997
Mahendra Take Avatar asked Jun 30 '15 07:06

Mahendra Take


1 Answers

TL;DR: AES in CFB mode requires a unique (but not necessarily randomized) IV of 16 bytes.


The size of the IV depends on the mode, but typically it is the same size as the block size, which for AES is always 16 bytes. There are modes that differ from this, notably GCM mode which has a default size of 12 bytes but can take any sized IV - although keeping to the default is highly recommended.

The old school modes such as CBC and CFB however simply require an IV of the same size as the block size. Even CTR commonly requires 16 bytes, although in principle the IV can be any size less than 16, in which case it is (again, commonly) right padded with zero valued bytes. Note that CTR is often initialized with an initial counter value which means you must make sure that the counter is not repeated yourself.

The block size of AES is 16 bytes, whatever the key size. Saying that you have AES-256 and a key of 32 bytes is superfluous information. For AES-256 the key size must be 256 bits or 32 bytes.

The IV for CFB mode - as stated earlier - must always be 16 bytes as AES is a 128 bit block cipher. AES is restricted with regards to the block size compared with the Rijndael cipher. Rijndael may be configured with different block sizes.

like image 156
Maarten Bodewes Avatar answered Oct 24 '22 22:10

Maarten Bodewes