I have written a below program to encrypt a file with AES 128 algorithm. This code works fine. It does encrypt and decrypt file successfully..
Here in this code I am generating SecretKey in the main() method with the use of key generator. But can anybody please tell me how can I generate SecretKey based on user's password in the below program?
You can use the PBKDF2 algorithm to generate a key from a password; see this answer from a while ago.
Please make sure you use a different, random Initialisation Vector for each password-based key and each message, and save it with the encrypted data. Otherwise each message using the same key and starting with the same data will be encrypted the same, giving attackers a head start.
I have tried copy pasting the code from the thread you mentioned but I got the exception on line
SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
java.security.NoSuchAlgorithmException: Algorithm PBKDF2WithHmacSHA1 not available
Can you please tell me what is wrong with my code?
That's the stack trace I get from J2RE 1.4.2. Even if for some reason you can't move to a supported version of Java, it's still a good algorithm, and you should be able to find a compatible version somewhere.