Java 执行椭圆曲线加密时出现无效密钥异常
我在使用flexyprovider进行椭圆曲线加密时遇到了这个错误。我收到一个InvalidKeyException,但我不知道如何解决它:Java 执行椭圆曲线加密时出现无效密钥异常,java,security,cryptography,jce,elliptic-curve,Java,Security,Cryptography,Jce,Elliptic Curve,我在使用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:134
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。我在看同一个示例时遇到了同样的问题。我用这个解决了它 根本原因: 默认JDK附带了一些密钥大小限制,将其限制为128。如果安全策略使用的密钥大小大于此值,则会引发异常 解决方案: 您需要使用Java加密扩展(JCE)策略文件修补JDK
src:奇怪;您的代码似乎与示例相同-您是否做了任何更改?请尝试安装
Java Cryptography Extension(JCE)Unlimited Strength辖区策略文件
文件以查看其是否有效。我不确定代码是否需要它,但请无论如何尝试。更新问题,如果你已经安装了它,它仍然无法工作。耶…这是相同的代码。。。我没有做任何改变。但是这个错误显示了。您是否将US_export_policy.jar和local_policy.jar放置在$JAVA_HOME/jre/lib/security/目录中?