I'm currently working on a function that encrypt/decrypts a specific file with a secret key. I have written three classes, one which generates a key, one which encrypts a file with the key and one that decrypts.
Generating the key and encrypting the file works fine, but when I try to decrypt the file, an exception is thrown at line: c.init(Cipher.DECRYPT_MODE, keySpec);:
java.security.InvalidKeyException: Parameters missing
I take it I've done something wrong when streaming the secret key to my byte[] or something is wrong when decrypting the file.
Quick explanation of the three classes: KeyHandler creates a AES key and stores it on the harddrive. The name of the key/plaintext/encrypted/decrypted files is currently hardcoded for testing purposes.
EncryptionHandler transfers a .txt file on the disk into bytes, encrypts the file with the secret key and then writes the encrypted bytes to the disk using CipherOutputStream.
DecryptionHandler of course does the opposite of EncryptionHandler.
Here's the code:
public class KeyHandler {
Scanner scan = new Scanner(System.in);
public KeyHandler(){
try {
startMenu();
} catch (Exception e) {
System.out.println("fel någonstanns :)");
}
}
public void startMenu() throws Exception{
System.out.println("Hej. Med detta program kan du generera en hemlig nyckel"+"\n"+"Vill du:"+"\n"+ "1. Generera en nyckel"+"\n"+"2. Avsluta");
int val=Integer.parseInt(scan.nextLine());
do{
switch (val){
case 1: generateKey(); break;
case 2: System.exit(1);
default: System.out.println("Du måste välja val 1 eller 2");
}
}
while (val!=3);
}
pu