secp256k1在eclipse中不起作用/不支持吗?

huangapple go评论73阅读模式
英文:

secp256k1 is not working/supporting in eclipse?

问题

获得此错误:

> java.security.InvalidAlgorithmParameterException: 不支持的曲线:secp256k1(1.3.132.0.10)

在Eclipse中是否有任何可以使用secp256k1的方法?我找不到任何帮助。我如何使用secp256k1生成椭圆曲线的公钥和私钥对?

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

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);
        }
    }
}
英文:

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);
		}
	}
}

答案1

得分: 2

以下是翻译好的内容:

通常情况下,这个问题取决于Java版本,而不是Eclipse。请运行以下程序(稍微编辑了您的版本),然后告诉我们您的机器上正在运行的版本。

这是一个在线编译器的输出(直接链接:https://repl.it/@javacrypto/JavaEllipticCurveAvailable)

Java版本:main: 11 major: 0 minor: 8+10-post-Ubuntu-0ubuntu118 update: 04 build: 1
sun.security.ec.ECPrivateKeyImpl@5403
Sun EC公钥,256位
  公共x坐标:40409384899581786570368833959823834602401842042090899775653557207593518019107
  公共y坐标:55894232296453671204408524113424724230763480655586911101265852946730642819727
  参数:secp256k1(1.3.132.0.10)

代码:

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版本: " + 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;
    }
}
英文:

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;
    }
}

答案2

得分: 2

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

英文:

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

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

答案3

得分: 0

我正在使用Eclipse和OpenJDK。

最低要求 - OpenJDK版本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编码的私钥: " + Base64.getEncoder().encodeToString(privateKey.getEncoded()));

    System.out.println("Base64编码的公钥: " + Base64.getEncoder().encodeToString(publicKey.getEncoded()));
}
英文:

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()));
}

huangapple
  • 本文由 发表于 2020年10月11日 05:23:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/64298440.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定