0

Today following a guide I tried to encrypt and after decrypt a string. Unluckily, I've gotten an InvalidKeyException, I googled up that and found out that for encrypt a string that have more than 128 bits (16 bytes -> 16 characters) I need the JCE, so I got into the oracle website, downloaded it and putted it on my java build path, under the folder /lib/security. Still no luck, i still keep getting this error, with this code:

import java.security.Security;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;

public class EncryptTests {
  public static void main(String[] args) throws Exception {
        byte[] input = "www.java2s.com".getBytes();
        byte[] keyBytes = new byte[] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09,
            0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17 };

        SecretKeySpec key = new SecretKeySpec(keyBytes, "AES");
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
        System.out.println(new String(input));

        // encryption pass
        cipher.init(Cipher.ENCRYPT_MODE, key);

        byte[] cipherText = new byte[cipher.getOutputSize(input.length)];
        int ctLength = cipher.update(input, 0, input.length, cipherText, 0);
        ctLength += cipher.doFinal(cipherText, ctLength);
        System.out.println(new String(cipherText));
        System.out.println(ctLength);

        // decryption pass
        cipher.init(Cipher.DECRYPT_MODE, key);
        byte[] plainText = new byte[cipher.getOutputSize(ctLength)];
        int ptLength = cipher.update(cipherText, 0, ctLength, plainText, 0);
        ptLength += cipher.doFinal(plainText, ptLength);
        System.out.println(new String(plainText));
        System.out.println(ptLength);
      }
    }   

This is the console output:

www.java2s.com Exception in thread "main" java.security.InvalidKeyException: Illegal key size or default parameters at javax.crypto.Cipher.checkCryptoPerm(Cipher.java:1026) at javax.crypto.Cipher.implInit(Cipher.java:801) at javax.crypto.Cipher.chooseProvider(Cipher.java:864) at javax.crypto.Cipher.init(Cipher.java:1249) at javax.crypto.Cipher.init(Cipher.java:1186) at EncryptTests.main(EncryptTests.java:23)

Any fix? :)

Dipu Raj
  • 1,784
  • 4
  • 29
  • 37
Sapus Boh
  • 105
  • 1
  • 1
  • 8
  • 2
    I would guess you installed the policy files incorrectly, either the wrong files or the wrong place. – President James K. Polk Nov 26 '16 at 18:29
  • Everything is correct – Sapus Boh Nov 26 '16 at 20:25
  • 1
    You haven't installed the policy files correctly. Also, why can't you just use a 128 bit key? You can encrypt data of arbitrary length with any AES key size. – Luke Joshua Park Nov 27 '16 at 08:47
  • Print out the [max key length allowed](http://docs.oracle.com/javase/7/docs/api/javax/crypto/Cipher.html#getMaxAllowedKeyLength(java.lang.String)) for that algorithm. If it is less than integer max, the files were *not* installed properly. – Leigh Feb 11 '17 at 01:47
  • Possible duplicate of [Java Security: Illegal key size or default parameters?](http://stackoverflow.com/questions/6481627/java-security-illegal-key-size-or-default-parameters) – Leigh Feb 11 '17 at 01:51

0 Answers0