secp256k1 is not working/supporting in eclipse?

3.9k views Asked by At

Getting this error:

java.security.InvalidAlgorithmParameterException: Unsupported curve: secp256k1 (1.3.132.0.10)

is there any kind of method that i can use secp256k1 in eclipse? i cannot found any help. how i can use secp256k1 to generate a public and private key pair of the elliptic curve?

import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.spec.ECGenParameterSpec;
import java.security.spec.*;

public class ECCKeyGeneration {

    public static void main(String[] args) {
        try {
            KeyPairGenerator kpg;
            kpg = KeyPairGenerator.getInstance("EC");
            ECGenParameterSpec ecsp;
            ecsp = new ECGenParameterSpec("secp256k1");
            kpg.initialize(ecsp);

            KeyPair kp = kpg.genKeyPair();
            PrivateKey privKey = kp.getPrivate();
            PublicKey pubKey = kp.getPublic();

            System.out.println(privKey.toString());
            System.out.println(pubKey.toString());
        } catch (Exception ex) {
            System.out.println(ex);
        }
    }
}
3

There are 3 answers

6
Michael Fehr On

Usually, this question depends on the Java version and not Eclipse. Kindly run the following program (slightly edited version of yours) and give us the version that is running on your machine.

This is an output of an online compiler (direct link: https://repl.it/@javacrypto/JavaEllipticCurveAvailable)

Java Version: main: 11 major: 0 minor: 8+10-post-Ubuntu-0ubuntu118 update: 04 build: 1
sun.security.ec.ECPrivateKeyImpl@5403
Sun EC public key, 256 bits
  public x coord: 40409384899581786570368833959823834602401842042090899775653557207593518019107
  public y coord: 55894232296453671204408524113424724230763480655586911101265852946730642819727
  parameters: secp256k1 (1.3.132.0.10)

code:

import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.spec.ECGenParameterSpec;
import java.security.spec.*;

public class Main {

    public static void main(String[] args) {
      System.out.println("Java Version: " + getJavaVersion());
        try {
            KeyPairGenerator kpg;
            kpg = KeyPairGenerator.getInstance("EC");
            ECGenParameterSpec ecsp;
            ecsp = new ECGenParameterSpec("secp256k1");
            kpg.initialize(ecsp);

            KeyPair kp = kpg.genKeyPair();
            PrivateKey privKey = kp.getPrivate();
            PublicKey pubKey = kp.getPublic();

            System.out.println(privKey.toString());
            System.out.println(pubKey.toString());
        } catch (Exception ex) {
            System.out.println(ex);
        }
    }
    public static String getJavaVersion() {
        String[] javaVersionElements = System.getProperty("java.runtime.version").split("\\.|_|-b");
        String main = "", major = "", minor = "", update = "", build = "";
        int elementsSize = javaVersionElements.length;
        if (elementsSize > 0) {main = javaVersionElements[0];}
        if (elementsSize > 1) {major   = javaVersionElements[1];}
        if (elementsSize > 2) {minor   = javaVersionElements[2];}
        if (elementsSize > 3) {update  = javaVersionElements[3];}
        if (elementsSize > 4) {build   = javaVersionElements[4];}
        return "main: " + main + " major: " + major + " minor: " + minor + " update: " + update + " build: " + build;
    }
}
0
jplayin On

I am using Eclipse and OpenJDK.

The minimum requirement - OpenJDK version 11.0.5

public static void main(String[] args) 
    throws NoSuchAlgorithmException, InvalidAlgorithmParameterException {

    KeyPairGenerator kpg = KeyPairGenerator.getInstance("EC");

    ECGenParameterSpec m = new ECGenParameterSpec("secp256k1");

    kpg.initialize(m);

    ECPrivateKey privateKey = (ECPrivateKey) kpg.generateKeyPair().getPrivate();

    ECPublicKey publicKey = (ECPublicKey) kpg.generateKeyPair().getPublic();

    System.out.println("Base64 encoded private key : " + Base64.getEncoder().encodeToString(privateKey.getEncoded()));

    System.out.println("Base64 encoded public key : " + Base64.getEncoder().encodeToString(publicKey.getEncoded()));
}
1
Hustwhw On

// specific reliable provider, that works even java 17+

KeyPairGenerator.getInstance("EC", "BC")