如何在java.security.KeyPairGenerator中使用自定义EC参数?

时间:2018-04-09 09:20:23

标签: java elliptic-curve key-pair

我想使用Edwards曲线" Curve41417" (safecurves)用于生成密钥对。

现在我已将其转换为Weierstrass格式并尝试以下源代码:

public class Test {

    public static void main(String[] args) {

        try {
            KeyPairGenerator keyGen = null;
            keyGen = KeyPairGenerator.getInstance("ECDSA", "LunaProvider");

            // Create a secure random number generator using the SHA1PRNG algorithm
            SecureRandom secureRandomGenerator = SecureRandom.getInstance("SHA1PRNG");

            //generator with the given parameter set and source of randomness
            keyGen.initialize(curve41417InHex(),secureRandomGenerator);

            //keyGen.initialize(ecSpec);
            KeyPair keyPair = keyGen.generateKeyPair();

        } catch (NoSuchAlgorithmException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (NoSuchProviderException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }catch (InvalidAlgorithmParameterException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

    public static ECParameterSpec curve41417InHex(){
        ECPoint point = new ECPoint(
                // gx
                new BigInteger("36AF93BFF765C2150A948827D97CF68F5F83E0D0C7411AE313A89ABF50224BBAE8C2F76271040290884CF5629DAB279D49AB0F98",16),
                // gy
                new BigInteger("1952C13B138703B04EA0D313944A8B1E9AE7882380AD83907F12F2A937C2503ADA9E6BF01CA1F76FDF9032C79F130EB2BEA4C102",16));

        final ECParameterSpec curve41417 = new ECParameterSpec(

                new EllipticCurve(
                        // field the finite field that this elliptic curve is over.
                        new ECFieldFp(new BigInteger("3FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEF",16)),
                        // a : the first coefficient of this elliptic curve.
                        new BigInteger("3FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFBD324",16),
                        // b : the second coefficient of this elliptic curve.
                        new BigInteger("3FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFCC3EC75",16)
                        ),
                point,
                // Order n
                new BigInteger("7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEB3CC92414CF706022B36F1C0338AD63CF181B0E71A5E106AF79",16),
                // Cofactor
                8);
        return curve41417;
    } 

}

上一个源代码的输出为:密钥生成期间的异常 - 不支持的曲线:java.security.spec.ECParameterSpec@43814d18

我相信我在从蒙哥马利到Weierstrass等式的转换中做错了,或者我没有使用适当的类来提供自定义EC参数。

有人已成功使用自定义EC参数进行密钥对生成吗?也许对于另一个蒙哥马利或爱德华兹曲线?

1 个答案:

答案 0 :(得分:0)

我相信不可能使用Luna的JSP API在LunaProvider中基于自定义EC曲线生成EC密钥对。但是,可以使用JCProv API(Luna提供)生成EC密钥对。 JCProv-PKCS#11 Java包装器,它是与PKCS#11实现接近的较低级别的API。您可以使用CryptokiEx.C_GenerateKey(..)方法来生成密钥对。 here可以找到PKSC#11 C_GenerateKey()方法文档。