0

收到此错误:

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

1 回答 1

1

通常,这个问题取决于 Java 版本而不是 Eclipse。请运行以下程序(您的稍作编辑的版本)并将您机器上运行的版本提供给我们。

这是在线编译器的输出(直接链接: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)

代码:

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;
    }
}
于 2020-10-10T21:49:36.143 回答