1

使用 flexyprovider 进行椭圆曲线加密时出现此错误。我收到了 InvalidKeyException 但我不知道如何解决它:

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)

这是我的代码

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();
    }

}

谁能帮帮我吗?我正在使用 JDK 版本 1.7.0_25。

4

1 回答 1

2

在查看相同的示例时,我遇到了同样的问题。我用这个答案解决了它。

根本原因 :

JDK 附带的默认密钥大小有限制 - 将其限制为 128。如果您的安全策略使用的密钥大小大于此大小 - 则会引发异常。

解决方案 :

您需要使用 Java Cryptography Extension (JCE) Unlimited Strength Jurisdiction Policy Files 修补您的 JDK。

有关如何下载 JCE 策略文件的说明

src:java.security.InvalidKeyException:非法密钥大小或默认参数

于 2015-04-15T19:50:42.170 回答