java.lang.IllegalArgumentException:string curve25519 not a OID bouncycastle 1.52

时间:2015-04-28 21:29:46

标签: java cryptography bouncycastle elliptic-curve key-pair

我正在尝试使用/ java bouncy castle 1.52实现为曲线25519生成一个密钥对,它给了我什么

  

java.lang.IllegalArgumentException:string curve25519 not a OID

这是我的代码:

public KeyPair generateKeys() throws NoSuchAlgorithmException,
        NoSuchProviderException, InvalidAlgorithmParameterException {
    ECParameterSpec ecSpec = ECNamedCurveTable.getParameterSpec("curve25519");
    KeyPairGenerator g = KeyPairGenerator.getInstance("ECDSA", "BC");
    g.initialize(ecSpec, new SecureRandom());

    return g.generateKeyPair();
}

此代码的结果是下面的堆栈跟踪:

  

java.lang.IllegalArgumentException:string curve25519不是OID       在org.bouncycastle.asn1.ASN1ObjectIdentifier。(未知来源)       at org.bouncycastle.jcajce.provider.asymmetric.ec.BCECPublicKey.getEncoded(Unknown Source)       at org.bouncycastle.jcajce.provider.asymmetric.ec.BCECPrivateKey.getPublicKeyDetails(Unknown Source)       在org.bouncycastle.jcajce.provider.asymmetric.ec.BCECPrivateKey。(未知来源)       at org.bouncycastle.jcajce.provider.asymmetric.ec.KeyPairGeneratorSpi $ EC.generateKeyPair(Unknown Source)       在com.poc.databank.encryption.BouncyCastleEncryption.generateKeys(BouncyCastleEncryption.java:22)       在com.poc.databank.encryption.BouncyCastleTest.testApp(BouncyCastleTest.java:16)       at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)       at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)       at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)       at java.lang.reflect.Method.invoke(Method.java:606)       在org.junit.runners.model.FrameworkMethod $ 1.runReflectiveCall(FrameworkMethod.java:47)       在org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)       在org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)       在org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)       在org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)       在org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)       在org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)       在org.junit.runners.ParentRunner $ 3.run(ParentRunner.java:238)       在org.junit.runners.ParentRunner $ 1.schedule(ParentRunner.java:63)       在org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)       在org.junit.runners.ParentRunner.access $ 000(ParentRunner.java:53)       在org.junit.runners.ParentRunner $ 2.evaluate(ParentRunner.java:229)       在org.junit.runners.ParentRunner.run(ParentRunner.java:309)       在org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)       在org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)       在org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)       在org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)       在org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)       在org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

我检查了充气城堡代码,并发现curve25519已在CustomNamedCurves类中注册为

defineCurve("curve25519", curve25519);

但不是

defineCurveWithOID("secp192k1", SECObjectIdentifiers.secp192k1,
            secp192k1);

我知道应该有这个原因。请帮我找出一种如何生成关键对uising curve25519的方法。

1 个答案:

答案 0 :(得分:0)

@divanov运行代码时出现以下编译错误

  

错误:ECParameterSpec类中的构造函数ECParameterSpec不能应用于给定类型;
      ECParameterSpec ecSpec=new ECParameterSpec(ecP.getCurve(), ecP.getG(),
       ^
      required:EllipticCurve,java.security.spec.ECPoint,BigInteger,int
      发现:ECCurve,org.bouncycastle.math.ec.ECPoint,BigInteger,BigInteger,byte []
      原因:实际和正式的参数列表长度不同

方法签名是否已更改?我们可以使用org.bouncycastle.jcajce.provider.asymmetric.util.EC5Util

X9ECParameters ecP = CustomNamedCurves.getByName("curve25519");
ECParameterSpec ecSpec = EC5Util.convertToSpec(ecP);