Hello.
I am currently working on a little password based file encryption feature for an application I am doing. The following is an incomplete function that I have worked on to encrypt some content and (will eventually) write it to a file.
public static void encrypt(byte [] data, char [] password, File file) throws ...
{
byte[] salt = { (byte)0xA9, (byte)0x9B, (byte)0xC8, (byte)0x32,
(byte)0x56, (byte)0x35, (byte)0xE3, (byte)0x03 };
int iterationCount = 50;
PBEKeySpec keySpec = new PBEKeySpec(password, salt, iterationCount);
PBEParameterSpec paramSpec = new PBEParameterSpec(keySpec.getSalt(), keySpec.getIterationCount());
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBE");
SecretKey key = keyFactory.generateSecret(keySpec);
Cipher cipher = Cipher.getInstance(key.getAlgorithm());
cipher.init( Cipher.ENCRYPT_MODE, key, paramSpec );
byte [] enc_data = cipher.doFinal(data);
}
My problem comes however when the decryption process is used. I would like to be able to check for a correct password before continuing with the decryption process and simply break from the process if an incorrect password is entered.
My guess is that during the file encryption process I will have to write the hashed password into the output file along with the salt and the data and then retrieve this hashed password to compare with user input when decrypting the file.
I would like to know if I am correct in thinking I have to do as above, or perhaps there is already some way of doing this using the standard API functions that I could use instead of reinventing the wheel.
If I am incorrect in my way of thinking on this one please, by all means correct me. Criticism will be taken constructively.
Thank you in advance.