'Encryption and Decreptyon with PBEWithHmacSHA256AndAES_128

I'm doing an academic project, in that project I need to encrypt information to txt files that store the information, and also decrypt it to the server, so that I'm able to work with the information. I tried to do the code in the project but it wasn't working, so I tried to simplify and make a separate project only encrypting and decrypting a single string from and to a file. However, it isn't working.

What follows is the small test project I made.

Main

package cipherTest;

import java.io.File; 
import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;
import java.util.List;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.PBEKeySpec;

public class F {

    private static Cipher c;
    private static Cipher d;
    private static List<String> list;

    public static void main(String[] args) throws InvalidKeyException, NoSuchAlgorithmException, InvalidKeySpecException, NoSuchPaddingException, IOException, IllegalBlockSizeException, BadPaddingException {
        generateEncryptCipher(args[0]);
        generateDecryptCipher(args[0]);
        checkFiles("test.txt");
        Writer w = new Writer();
        w.encrypt(c);
        Read r = new Read();
        r.decrypt(d);
        System.out.println(list);
        

    }



    public static void generateEncryptCipher(String password) throws NoSuchAlgorithmException, InvalidKeySpecException, NoSuchPaddingException, InvalidKeyException {
        PBEKeySpec keySpec = new PBEKeySpec(password.toCharArray());

        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBEWithHmacSHA256AndAES_128");

        SecretKey skc = keyFactory.generateSecret(keySpec);

        c = Cipher.getInstance("PBEWithHmacSHA256AndAES_128");
        c.init(Cipher.ENCRYPT_MODE, skc);
    }

    public static void generateDecryptCipher(String password) throws NoSuchAlgorithmException, InvalidKeySpecException, NoSuchPaddingException, InvalidKeyException{

        PBEKeySpec keySpec = new PBEKeySpec(password.toCharArray());

        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBEWithHmacSHA256AndAES_128");

        SecretKey skd = keyFactory.generateSecret(keySpec);

        d = Cipher.getInstance("PBEWithHmacSHA256AndAES_128");
        d.init(Cipher.DECRYPT_MODE, skd);

    }

    private static void checkFiles(String filename) throws IOException {
        File file = new File(filename);
        if(!file.exists()){
            file.createNewFile();
        }
    }
}

Write encryption in file.

package cipherTest;

import java.io.FileWriter;
import java.io.IOException;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;

import javax.xml.bind.DatatypeConverter;

public class Writer {

    public Writer() {
        
    }
    
    public void encrypt(Cipher c) throws IllegalBlockSizeException, BadPaddingException, IOException {
        String s = "Frase de teste, por favor funciona.";
        byte[] enc = c.doFinal(s.getBytes());
        String encryptedtext = DatatypeConverter.printBase64Binary(enc);
        FileWriter writer = new FileWriter("test.txt", true);
        writer.write(encryptedtext + System.lineSeparator());
        writer.close();
    }

}

Read and decrypt file

package cipherTest;

import java.io.File;  
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.xml.bind.DatatypeConverter;


public class Read {
    public Read() {

    }
    
    public List<String> decrypt(Cipher c) throws FileNotFoundException, IllegalBlockSizeException, BadPaddingException {
        List<String> list = new ArrayList<>();
        Scanner scanner = new Scanner(new File("test.txt"));
        while(scanner.hasNextLine()){
            byte[] encrypted = DatatypeConverter.parseBase64Binary(scanner.nextLine());
            String decrypted = new String(c.doFinal(encrypted)); 
            list.add(decrypted);
        }
        scanner.close();
        return list;
    }
}

This is the error that I'm getting.

Exception in thread "main" java.security.InvalidKeyException: requires PBE parameters
    at com.sun.crypto.provider.PBES2Core.engineInit(PBES2Core.java:165)
    at javax.crypto.Cipher.implInit(Cipher.java:801)
    at javax.crypto.Cipher.chooseProvider(Cipher.java:863)
    at javax.crypto.Cipher.init(Cipher.java:1248)
    at javax.crypto.Cipher.init(Cipher.java:1185)
    at cipherTest.F.generateDecryptCipher(F.java:59)
    at cipherTest.F.main(F.java:26)
Caused by: java.security.InvalidAlgorithmParameterException: Parameters missing
    at com.sun.crypto.provider.CipherCore.init(CipherCore.java:549)
    at com.sun.crypto.provider.PBES2Core.engineInit(PBES2Core.java:292)
    at com.sun.crypto.provider.PBES2Core.engineInit(PBES2Core.java:163)
    ... 6 more

I'm not really understanding this.

I'm supposed to decrypt files when I start the server. From what I'm understanding I need some parameters to decrypt it, but from what I'm finding online I get the parameters after I encrypt it.

I'd like to know why this doesn't work. What I'm doing wrong.

update

after making this change :

d.init(Cipher.DECRYPT_MODE, skd, d.getParameters());

I'm getting this error:

Exception in thread "main" javax.crypto.IllegalBlockSizeException: Input length must be multiple of 16 when decrypting with padded cipher
    at com.sun.crypto.provider.CipherCore.prepareInputBuffer(CipherCore.java:1005)
    at com.sun.crypto.provider.CipherCore.doFinal(CipherCore.java:848)
    at com.sun.crypto.provider.PBES2Core.engineDoFinal(PBES2Core.java:323)
    at javax.crypto.Cipher.doFinal(Cipher.java:2164)
    at cipherTest.Read.decrypt(Read.java:25)
    at cipherTest.F.main(F.java:32)


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source