1

I got this error when I did elliptical curve cryptography using flexyprovider. I received an InvalidKeyException but I can't figure out how to solve it:

Exception in thread "main" java.security.InvalidKeyException: Illegal key size
at javax.crypto.Cipher.checkCryptoPerm(Cipher.java:1024)
at javax.crypto.Cipher.init(Cipher.java:1345)
at javax.crypto.Cipher.init(Cipher.java:1282)
at ExampleECIES.main(ExampleECIES.java:43)

This is my code

public class ExampleECIES {

    public static void main(String[] args) throws Exception {

    Security.addProvider(new FlexiCoreProvider());
    Security.addProvider(new FlexiECProvider());

    KeyPairGenerator kpg = KeyPairGenerator.getInstance("ECIES", "FlexiEC");

    CurveParams ecParams = new BrainpoolP160r1();

    kpg.initialize(ecParams, new SecureRandom());
    KeyPair keyPair = kpg.generateKeyPair();
    PublicKey pubKey = keyPair.getPublic();
    PrivateKey privKey = keyPair.getPrivate();

    // Encrypt

    Cipher cipher = Cipher.getInstance("ECIES", "FlexiEC");

    IESParameterSpec iesParams = new IESParameterSpec("AES128_CBC",
        "HmacSHA1", null, null);
    System.out.println(iesParams);
    cipher.init(Cipher.ENCRYPT_MODE, pubKey, iesParams);

    String cleartextFile = "cleartext.txt";
    String ciphertextFile = "ciphertextECIES.txt";

    byte[] block = new byte[64];
    FileInputStream fis = new FileInputStream(cleartextFile);
    FileOutputStream fos = new FileOutputStream(ciphertextFile);
    CipherOutputStream cos = new CipherOutputStream(fos, cipher);

    int i;
    while ((i = fis.read(block)) != -1) {
        cos.write(block, 0, i);
    }
    cos.close();

    // Decrypt

    String cleartextAgainFile = "cleartextAgainECIES.txt";

    cipher.init(Cipher.DECRYPT_MODE, privKey, iesParams);

    fis = new FileInputStream(ciphertextFile);
    CipherInputStream cis = new CipherInputStream(fis, cipher);
    fos = new FileOutputStream(cleartextAgainFile);

    while ((i = cis.read(block)) != -1) {
        fos.write(block, 0, i);
    }
    fos.close();
    }

}

Can anyone please help me? I'm using JDK version 1.7.0_25.

Charles
  • 50,943
  • 13
  • 104
  • 142
Shakeeb Manjeri
  • 120
  • 1
  • 1
  • 9
  • strange; your code seems identical to the example http://www.flexiprovider.de/examples/ExampleECIES.html - did you make any changes? – andrew cooke Nov 07 '13 at 12:18
  • Try installing the `Java Cryptography Extension (JCE) Unlimited Strength Jurisdiction Policy File` file to see if it works. I am unsure if the code requires it but try anyway. Update the question if you already have it installed and it persists to not work. – initramfs Nov 08 '13 at 10:40
  • ya...this is the same code... i didnt make any change. but this error shows. – Shakeeb Manjeri Nov 08 '13 at 11:28
  • Did you place US_export_policy.jar and local_policy.jar to in the $JAVA_HOME/jre/lib/security/ directory? – divanov Nov 13 '13 at 20:37

1 Answers1

2

I had the same problem when looking at the same example. I solved it with this answer.

Root Cause :

There are key size restrictions with the default JDK comes with - which limits it to 128. If your security policy uses a key size larger than this - then the exception is thrown.

Solution :

You need to patch your JDK with Java Cryptography Extension (JCE) Unlimited Strength Jurisdiction Policy Files.

Instructions on how to download JCE Policy files

src: java.security.InvalidKeyException: Illegal key size or default parameters

Community
  • 1
  • 1
Daniel
  • 2,950
  • 2
  • 25
  • 45