Java Default Crypto/Aes Behavior

Default AES length for java crypto cipher

According to the javadocs for javax.crypto.Cipher, that string ("AES/CBC/PKCS5Padding") gives you an AES cipher algorithm.

There isn't a default key length. (But there is a guarantee that every standard Java SE platform supports AES keys of 128 bits. Some Java SE platforms support other AES key lengths (192 & 256) as well.)

The actual key length is not part of the algorithm name / Cipher object. It will depend on whatever key that givemeKey is returning. However in your example the base64 encoded key string is 24 characters long so that means it could not possibly be a 265 bit key.

(Do the math. 6 bits per character x 24 characters == 144 bits max. So assuming that some of those bits are "envelope" or "padding", that looks like it would be an 128 bit key.)

AES is a symmetric-key algorithm; see Wikipedia.

AES encryption in Java - what mode?

Don't rely on default behavior. If you known the options, go ahead and specify them. If you don't know the options, then go and find them out, and then specify them.

Both of the encrypted messages are 'hello' encrypted with your key .. the only difference is different padding modes:

57C758B2B3A8580658A11DBD95109EC4 decrypts to   68656c6c6f0b0b0b0b0b0b0b0b0b0b0b
h e l l o <-- pkcs5 padding -->

3874350661ABB0B452A4960FE3953C18 decrypts to 68656c6c6f0000000000000000000000
h e l l o <-- zerro padding -->

What is the default initialization vector behavior for the Java implementation of AES?

The Sun provider defaults to AES/ECB/PKCS5Padding when given the "AES" transformation. ECB mode does not use an initialization vector. Note that ECB is not considered secure.

What's the default size of AES generated keys?

I am not sure there is specification for the default size but the Sun JCE generates 16 bytes (128-bit) keys.

You can find out by checking the encoded size,

  int keyBits = (key.getEncoded()).length * 8;

Java AES without padding


See my comment. Sorry I probably should have taken a closer look the first time.

  1. Change "AES" to "AES/CBC/NoPadding"
  2. Change decryptor.init(Cipher.DECRYPT_MODE, skeySpec); to decryptor.init(Cipher.DECRYPT_MODE, skeySpec, encryptor.gerParameters());

To encrypt only 16 bytes of data, fixed length, using a method that requires no initialization vector to be saved, Change "AES" to "AES/ECB/NoPadding"

I pick ECB because that is the default.

If you need to encrypt more than 16 bytes, consider using something other than ECB, which suffers a certain repetition detection flaw

In this bitmap example, this image has repeated white blocks, so you can deduce the outline of the image simply by looking for where the blocks become different.

before encryption
encrypted

If you are only encrypting one block, it doesn't really matter though, only if you are encrypting multiple blocks that are combined does ECB become revealing.

Related: https://security.stackexchange.com/questions/15740/what-are-the-variables-of-aes



Related Topics



Leave a reply



Submit