What you need to know in general:
- Key! = Password
SecretKeySpec expects a key, not a password. See below
- This may be due to a policy restriction that prevents the use of 32-byte keys. See another answer to this question.
In your case
Problem number 1: you pass the password instead of the key.
AES only supports 16, 24, or 32 byte key sizes. You need to either indicate exactly this amount, or you get a key from what you enter.
There are different ways to get a key from a key phrase. Java provides an implementation of PBKDF2 for this purpose.
I used erickson's answer to paint the whole picture (only encryption, as the decryption is similar, but includes splitting the ciphertext):
SecureRandom random = new SecureRandom(); byte[] salt = new byte[16]; random.nextBytes(salt); KeySpec spec = new PBEKeySpec("password".toCharArray(), salt, 65536, 256); // AES-256 SecretKeyFactory f = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1"); byte[] key = f.generateSecret(spec).getEncoded(); SecretKeySpec keySpec = new SecretKeySpec(key, "AES"); byte[] ivBytes = new byte[16]; random.nextBytes(ivBytes); IvParameterSpec iv = new IvParameterSpec(ivBytes); Cipher c = Cipher.getInstance("AES/CBC/PKCS5Padding"); c.init(Cipher.ENCRYPT_MODE, keySpec, iv); byte[] encValue = c.doFinal(valueToEnc.getBytes()); byte[] finalCiphertext = new byte[encValue.length+2*16]; System.arraycopy(ivBytes, 0, finalCiphertext, 0, 16); System.arraycopy(salt, 0, finalCiphertext, 16, 16); System.arraycopy(encValue, 0, finalCiphertext, 32, encValue.length); return finalCiphertext;
Other things to keep in mind:
- Always use the full name of the cipher.
AES not suitable in this case, because different JVM / JCE providers may use different default values ββfor operating mode and populating. Use AES/CBC/PKCS5Padding . Do not use ECB mode because it is not semantically safe. - If you are not using ECB mode, you need to send IV along with the ciphertext. This is usually done by adding the IV prefix to the byte array of the ciphertext. An IV is automatically created for you, and you can get it through
cipherInstance.getIV() . - Whenever you post something, you must be sure that it has not been changed along the way. It is difficult to properly implement encryption using the MAC. I recommend that you use an authentication mode such as CCM or GCM.
Artjom B.
source share