C.Farron
C.Farron

Reputation: 15

Java AES Encryption and Decryption: javax.crypto.IllegalBlockSizeException

newbie in Java AES! I am exploring and following the tutorial of baeldung and I got this error while seeing it for myself for 256 key length: Exception in thread "main" javax.crypto.IllegalBlockSizeException: Message must be a multiple of the block size without padding

I have the below: main Method `

public static void main(String[] args) throws NoSuchAlgorithmException, InvalidKeyException, NoSuchPaddingException, InvalidAlgorithmParameterException, BadPaddingException, IllegalBlockSizeException, InvalidKeySpecException {
        System.out.println("Encrypt/Decrypt a string");
        //3 params for AES algo: (1) input data, (2) secret key, (3) and IV
        Scanner scanner = new Scanner(System.in);
        String inputKey;
        int inputSecretKey = 256;
        IvParameterSpec IV;
        
        
        //step 1: input
        System.out.print("Input: ");
        inputKey = scanner.nextLine();
        
        //step 2: generate secret key

        System.out.println("Generating secret key with size "+inputSecretKey);
        SecretKey secretKey1 = generateKey(inputSecretKey);
        
        //step 3: generate IV
        IV = generateIv();
        
        //step 4: print
        String cipherText = encrypt("AES/CBC/PKCS5Padding", inputKey, secretKey1, IV);
        String plainText = decrypt("AES/CBC/PKCS5Padding", inputKey, secretKey1, IV);
        Assertions.assertEquals(inputKey, plainText);
        System.out.println("Encrypted: "+cipherText+" [size : "+cipherText.length()+"]");
        System.out.println("Decrypted: "+plainText+" [size : "+plainText.length()+"]");
        
        
        scanner.close();
        
    }

`

generateKey Method `

public static SecretKey generateKey(int n) throws NoSuchAlgorithmException {
        KeyGenerator keyGen = KeyGenerator.getInstance("AES");
        keyGen.init(n);
        SecretKey key = keyGen.generateKey();
        return key;
    }

`

generateIv Method `

public static IvParameterSpec generateIv() {
        byte[] iv = new byte[16];
        new SecureRandom().nextBytes(iv);
        return new IvParameterSpec(iv);
    }

`

encrypt Method `

public static String encrypt(String algorithm, String input, SecretKey key,
            IvParameterSpec iv) throws NoSuchPaddingException, NoSuchAlgorithmException,
            InvalidAlgorithmParameterException, InvalidKeyException,
            BadPaddingException, IllegalBlockSizeException {
            
            Cipher cipher = Cipher.getInstance(algorithm);
            cipher.init(Cipher.ENCRYPT_MODE, key, iv);
            byte[] cipherText = cipher.doFinal(input.getBytes());
            return Base64.getEncoder()
                .encodeToString(cipherText);
    }

`

decrypt Method (I included which specific line is eclipse pointing me at) `

public static String decrypt(String algorithm, String cipherText, SecretKey key,
            IvParameterSpec iv) throws NoSuchPaddingException, NoSuchAlgorithmException,
            InvalidAlgorithmParameterException, InvalidKeyException,
            BadPaddingException, IllegalBlockSizeException {
            
            Cipher cipher = Cipher.getInstance(algorithm);
            cipher.init(Cipher.DECRYPT_MODE, key, iv);
            byte[] plainText = cipher.doFinal(Base64.getDecoder()
                .decode(cipherText)); //<-------- GETTING ERROR IN THIS LINE
            return new String(plainText);
        }

`

I tried searching but I think no one has encountered this yet. The only thing I understood is AES/CBC/PKCS5Padding stands for algorithm/mode/padding. I did find this though I am not sure what to use. I am quite confused on what I should change in the code and the root cause of the error.

I am trying inputs like "hello" or "dFet4Q2fi" if that helps.

Upvotes: 0

Views: 1372

Answers (1)

Jens
Jens

Reputation: 69440

The second parameter of your decrypt method must be the cipherText not the input:

    String plainText = decrypt("AES/CBC/PKCS5Padding", cipherText
            , secretKey1, IV);

BTW: Take care of java naming conventions. Variable names should start with lower case character

Upvotes: 1

Related Questions