Search code examples
javaencryptionkeyaes

Cipher.getBlockSize() returns 128 bits even with 256 bit key


I am trying to get some AES encryption going. I want to use AES-256.

aesKey.length = 32 or 256 bits. However, Cipher.getBlockSize() is returning 16 or 128 bits. Shouldn't Cipher.getBlockSize() return 32 instead of 16 if I am using AES-256? Or am I misunderstanding this method?

I'm just trying to determine if I am using AES-128 or -256.

byte[] aesKey = new byte[32];

SecretKey originalKey = new SecretKeySpec(aesKey, 0, aesKey.length, "AES");
Cipher enc = Cipher.getInstance("AES/ECB/PKCS5Padding");
enc.init(Cipher.ENCRYPT_MODE, originalKey);

System.out.println(enc.getBlockSize() * 8);

Solution

  • AES has a block size of 128 bit. This does not depend on the key size you are using.

    You cannot change the block size, however you can change the key size. AES is specified to support 128, 192 and 256 bit keys.