0

I´m trying to encript a string but appears the error Invalid Key Exception:Ilegal key size, I'm trying to make a solution one month, I need help :(. "sorry my bad english"

    private static String secretKey = "PSKDF2";
    private static String salt = "ssshhhhhhhhhhh!!!!";

    public static String encrypt(String strToEncrypt, String secret) {
        try {
            byte[] iv = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
            IvParameterSpec ivspec = new IvParameterSpec(iv);

            SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA256");
            KeySpec spec = new PBEKeySpec(secretKey.toCharArray(), salt.getBytes(), 65536, 256);
            SecretKey tmp = factory.generateSecret(spec);
            SecretKeySpec secretKey = new SecretKeySpec(tmp.getEncoded(), "AES");

            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            cipher.init(Cipher.ENCRYPT_MODE, secretKey, ivspec);
            return Base64.getEncoder().encodeToString(cipher.doFinal(strToEncrypt.getBytes("UTF-8")));
        } catch (Exception e) {
            System.out.println("Error while encrypting: " + e.toString());
        }
        return null;
    }

    public static String decrypt(String strToDecrypt, String secret) {
        try {
            byte[] iv = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
            IvParameterSpec ivspec = new IvParameterSpec(iv);

            SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA256");
            KeySpec spec = new PBEKeySpec(secretKey.toCharArray(), salt.getBytes(), 65536, 256);
            SecretKey tmp = factory.generateSecret(spec);
            SecretKeySpec secretKey = new SecretKeySpec(tmp.getEncoded(), "AES");

            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
            cipher.init(Cipher.DECRYPT_MODE, secretKey, ivspec);
            return new String(cipher.doFinal(Base64.getDecoder().decode(strToDecrypt)));
        } catch (Exception e) {
            System.out.println("Error while decrypting: " + e.toString());
        }
        return null;
    }

I think the error is here.

Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");

and the error is this

Error while encrypting: java.security.InvalidKeyException: Illegal key size

Please Help :(

  • Months ago I faced to a similar situation. I found out that different jdk versions (Open jdk / Oracle jdk) have different ways to implement it. https://stackoverflow.com/questions/40381968/dh-key-size-must-be-multiple-of-64-and-can-only-range-from-512-to-2048-inclusi and https://stackoverflow.com/questions/36298394/java-invalidalgorithmparameterexception-prime-size-must-be-multiple-of-64 was the solutions of my problem. – Reporter Jan 29 '20 at 14:48
  • I dont understand my friend :( – CristianFnf Jan 29 '20 at 15:00
  • Please edit your question and post the full stack trace of the exception. It will help to isolate the line causing the exception and also provides other frequently helpful information. The exception is probably being thrown from the `cipher.init(Ci...);` line. Thank you and welcome to stackoverflow. – President James K. Polk Jan 30 '20 at 12:19
  • Also, provide the version of java you are using. – President James K. Polk Jan 30 '20 at 12:26
  • I just ran your code and it works for me. – President James K. Polk Jan 30 '20 at 12:31

1 Answers1

0

You may try the following ways to solve that. It seems some packages have not yet been installed.

President James K. Polk
  • 40,516
  • 21
  • 95
  • 125
tcf01
  • 1,699
  • 1
  • 9
  • 24
  • I don't understand how to install Java Cryptography Extension (JCE) Unlimited Strength Jurisdiction Policy Files 7 Download :( – CristianFnf Jan 29 '20 at 14:32
  • @CristianFnf Yu don't need the Unlimited Strength Jurisdiction Policy Files. You just need to upgrade to a recent Java version. Current Java 8 or 11 versions will just work out of the box. No need to install anything additionally. – Robert Jan 30 '20 at 18:02