Search code examples
javaencryptioncryptographyaes

AES 256 CBC encryption error java.security.InvalidKeyException: Invalid AES key length: 44 bytes error


package ASE;
import java.util.Base64;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

public class Test {

    private static final String key = "BKGsN85lqJB0Slf5hcAKBkIkAWCYG5KrKCtWkgUrLGD=";// 44
    private static final String initVector = "fNRJDLaHCK30bqbE";// 16

    public static String encrypt(String value) {
        try {
            IvParameterSpec iv = new IvParameterSpec(initVector.getBytes("UTF-8"));
            SecretKeySpec skeySpec = new SecretKeySpec(key.getBytes("UTF-8"), "AES");

            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
            cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);

            byte[] encrypted = cipher.doFinal(value.getBytes());

            return Base64.getEncoder().encodeToString(encrypted);

        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return null;
    }

    public static String decrypt(String encrypted) {
        try {
            IvParameterSpec iv = new IvParameterSpec(initVector.getBytes("UTF-8"));
            SecretKeySpec skeySpec = new SecretKeySpec(key.getBytes("UTF-8"), "AES");

            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
            cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);

            byte[] original = cipher.doFinal(Base64.getDecoder().decode(encrypted));

            return new String(original);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return null;
    }

    public static void main(String[] args) {
        String originalString = "123456789";
        System.out.println("Original String to encrypt - " + originalString);
        String encryptedString = encrypt(originalString);
        System.out.println("Encrypted String - " + encryptedString);
        String decryptedString = decrypt(encryptedString);
        System.out.println("After decryption - " + decryptedString);

    }
}

When I run this code I am getting error that

java.security.InvalidKeyException: Invalid AES key length: 44 bytes at java.base/com.sun.crypto.provider.AESCrypt.init(AESCrypt.java:90)


Solution

  • You are not decoding the encryption key properly before encryption/decryption. The decoding of that key from String to bytes using UTF-8 will yield 44 bytes. Try decoding that key from String to bytes using base 64 instead:

    import java.util.Base64;
    import javax.crypto.Cipher;
    import javax.crypto.spec.IvParameterSpec;
    import javax.crypto.spec.SecretKeySpec;
    
    public class Test {
    
        private static final String key = "BKGsN85lqJB0Slf5hcAKBkIkAWCYG5KrKCtWkgUrLGD=";// 44
        private static final String initVector = "fNRJDLaHCK30bqbE";// 16
    
        public static String encrypt(String value) {
            try {
                IvParameterSpec iv = new IvParameterSpec(initVector.getBytes("UTF-8"));
                SecretKeySpec skeySpec = new SecretKeySpec(Base64.getDecoder().decode(key), "AES");
    
                Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
                cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);
    
                byte[] encrypted = cipher.doFinal(value.getBytes());
    
                return Base64.getEncoder().encodeToString(encrypted);
    
            } catch (Exception ex) {
                ex.printStackTrace();
            }
            return null;
        }
    
        public static String decrypt(String encrypted) {
            try {
                IvParameterSpec iv = new IvParameterSpec(initVector.getBytes("UTF-8"));
                SecretKeySpec skeySpec = new SecretKeySpec(Base64.getDecoder().decode(key), "AES");
    
                Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
                cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);
    
                byte[] original = cipher.doFinal(Base64.getDecoder().decode(encrypted));
    
                return new String(original);
            } catch (Exception ex) {
                ex.printStackTrace();
            }
            return null;
        }
    
        public static void main(String[] args) {
            String originalString = "123456789";
            System.out.println("Original String to encrypt - " + originalString);
            String encryptedString = encrypt(originalString);
            System.out.println("Encrypted String - " + encryptedString);
            String decryptedString = decrypt(encryptedString);
            System.out.println("After decryption - " + decryptedString);
    
        }
    }
    

    Be very careful with encoding and decoding String objects to bytes. Always make sure you are using the same representation (such as UTF-8) wherever needed.