I want to encrypt a password that I need to store in a flat file to be accessed by my java application. I used the accepted answer of this question to model my solution, and it works. Since I know close to nothing about cryptography algorithms, once I read about how weak DES is I wanted to pick another algorithm, and I chose PBEWithHmacSHA256AndAES_256
. When I was using PBEWithMD5AndDES
,
pbeCipher.init(Cipher.ENCRYPT_MODE, key, new PBEParameterSpec(SALT, 20));
....
pbeCipher.init(Cipher.DECRYPT_MODE, key, new PBEParameterSpec(SALT, 20));
was enough to decrypt the string, but with this algorithm I get this exception:
java.security.InvalidAlgorithmParameterException: Missing parameter type: IV expected
at com.sun.crypto.provider.PBES2Core.engineInit(PBES2Core.java:252)
at javax.crypto.Cipher.implInit(Cipher.java:806)
at javax.crypto.Cipher.chooseProvider(Cipher.java:864)
at javax.crypto.Cipher.init(Cipher.java:1396)
at javax.crypto.Cipher.init(Cipher.java:1327)
and the only way I could get the decryption to work was if I pass the encryption cipher's algorithm parameters to the decryption one, like this:
pbeCipher.init(Cipher.ENCRYPT_MODE, key, new PBEParameterSpec(SALT, 20));
AlgorithmParameters ap = pbeCipher.getParameters();
....
pbeCipher.init(Cipher.DECRYPT_MODE, key, ap);
Which does not work for me cause I need to be able to decrypt if the application was restarted in the meantime. So, my question: is this simply how this algorithm is supposed to work, and in that case should I pick a different stronger one (or should I bother at all), or am I doing something wrong?