PBEWithHmacSHA512AndAES_128 and AES Modes like GCM I've asked this question on stackoverflow

And I think one gentleman has already answered the first part of question


  a) In my code i am keeping IV and salt with ciphertext. I wanted to use AES/GCM to check integrity of entire IV + salt.


I've got the following answer and I'm fine with it:


  PBEWithHmacSHA512AndAES_128 does not come with flexibility of
  specifying mode like GCM


Can you please help me with the next question:


  b) Why the byte [] of key is 9 bytes? (When I give input as Pree@2017 the key generated is 9 bytes - I have check the length of
  pbeKey.getEncoded() and its 9.


Can anyone from the community help please ...
Why the byte [] of key is 9 bytes? (When I give input as Pree@2017 the
  key generated is 9 bytes - I have check the length of
  pbeKey.getEncoded() and its 9.


The length of "Pree@2017" is 9 chars.
The reason why you are getting the original password is described here


  When you generate a SecretKey using the SecretKeyFactory
  PBEWithHmacSHA256AndAES_128 you will get an instance of
  com.sun.crypto.provider.PBEKey and this class has the "special
  feature" that it returns the original "key" (aka password) when
  calling getEncoded() and not the cryptographic key material. If I
  understand it correctly the key derivation will not be made by the
  KeyFactory but by the Cipher itself.
  
  Therefore you should not try to convert the SecretKey instance into a
  SecretKeySpec instance; instead just use the generated SecretKey
  instance in the correct cipher instance:

Cipher c = Cipher.getInstance("PBEWithHmacSHA256AndAES_128");
c.init(Cipher.ENCRYPT_MODE, secretKey);