gpt4 book ai didi

java - ECGenParameterSpec 未知曲线名称 'secp256k1'

转载 作者:搜寻专家 更新时间:2023-11-01 09:26:41 35 4
gpt4 key购买 nike

我有一个创建私钥和公钥的项目。我想用 ecdsa 加密它们,因此我使用带有“secp256k1”参数的 ECGenParameterSpec 但这会出错。如果我将其更改为“secp256r1”,则没有问题。这是我生成 key 的代码:

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

ECGenParameterSpec ecSpec = newECGenParameterSpec("secp256k1");
keyGen.initialize(ecSpec);

KeyPair kp = keyGen.generateKeyPair();
PublicKey pub = kp.getPublic();
PrivateKey pvt = kp.getPrivate();

ECPrivateKey epvt = (ECPrivateKey)pvt;
String sepvt = adjustTo64(epvt.getS().toString(16));
System.out.println("s[" + sepvt.length() + "]: " + sepvt);

ECPublicKey epub = (ECPublicKey)pub;
ECPoint pt = epub.getW();
String sx = adjustTo64(pt.getAffineX().toString(16));
String sy = adjustTo64(pt.getAffineY().toString(16));
String bcPub = sx + sy;
System.out.println("bcPub: " + bcPub);

return bcPub;

调整64位

        static private String adjustTo64(String s) {
switch(s.length()) {
case 62: return "00" + s;
case 63: return "0" + s;
case 64: return s;
default:
throw new IllegalArgumentException("not a valid key: " +s);
}
}

当我使用 secp256k1 时出现此错误:

05-01 12:20:46.066 17297-17297/com.sikke.app W/System.err: java.security.InvalidAlgorithmParameterException: unknown curve name: secp256k1
at com.android.org.conscrypt.OpenSSLECKeyPairGenerator.initialize(OpenSSLECKeyPairGenerator.java:113)
at java.security.KeyPairGenerator.initialize(KeyPairGenerator.java:439)
at com.sikke.app.AppHelper.getEcdsa(AppHelper.java:125)
at com.sikke.app.activities.AccountActivity.onCreate(AccountActivity.java:59)
at android.app.Activity.performCreate(Activity.java:6975)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1213)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2770)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)05-01 12:20:46.067 17297-17297/com.sikke.app W/System.err: at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)

最佳答案

经过长时间的搜索,我为我的项目找到了解决方案。您应该在您的项目中添加此代码以更改安全提供程序。

 Security.insertProviderAt(new org.spongycastle.jce.provider.BouncyCastleProvider(), 1);

要添加此代码,您应该使用此库:prov-1.58.0.0

关于java - ECGenParameterSpec 未知曲线名称 'secp256k1',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50114155/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com