Reputation: 130
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.
Upvotes: 1
Views: 724
Reputation: 3010
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
Upvotes: 2