gpt4 book ai didi

java - 进行椭圆曲线加密时出现无效 key 异常

转载 作者:行者123 更新时间:2023-11-30 04:10:08 25 4
gpt4 key购买 nike

当我使用 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。

最佳答案

在查看同一个示例时,我遇到了同样的问题。我用这个answer解决了它.

根本原因:

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

解决方案:

您需要使用 Java 加密扩展 (JCE) 无限强度管辖策略文件修补您的 JDK。

Instructions on how to download JCE Policy files

源代码:java.security.InvalidKeyException: Illegal key size or default parameters

关于java - 进行椭圆曲线加密时出现无效 key 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19832306/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com