java privatekey输出字符串,从本地Java中的私钥字符串派生EC公钥以获取曲线secp256k1...-程序员宅基地

技术标签: java privatekey输出字符串  

I need to derive an EC Public Key from an EC private key string without the "help" of any third party library.

The Private key is externally produced and provided and I need to get the Public Key to generate a Bitcoin address. As my project is working "offline" I do not need a library like Bouncy Castle for any other purpose, so I would like to eliminate it.

The following program is fully working and shows the (very short) example when working with Bouncy Castle to get a solution. The second part is the native Java solution with the kindly help from the routines by the so user SkateScout, for details see his answer https://stackoverflow.com/a/42797410/8166854.

Please keep in mind that this solution is working only for the Elliptic curve "secp256k1". You can check my keypair on https://gobittest.appspot.com/Address.

My question: is there any other solution available to avoid the mass of code for scalar operations?

import org.bouncycastle.jce.ECNamedCurveTable;

import org.bouncycastle.jce.spec.ECNamedCurveParameterSpec;

import java.math.BigInteger;

import java.security.*;

import java.security.interfaces.ECPrivateKey;

import java.security.interfaces.ECPublicKey;

import java.security.spec.*;

public class DerivePublicKeyFromPrivateKeyCurveSecp256k1 {

// get bouncycastle here: https://mvnrepository.com/artifact/org.bouncycastle/bcprov-jdk15on/1.65

// tested with version 15 1.65

final static BigInteger FieldP_2 = BigInteger.TWO; // constant for scalar operations

final static BigInteger FieldP_3 = BigInteger.valueOf(3); // constant for scalar operations

public static void main(String[] args) throws GeneralSecurityException {

System.out.println("Generate ECPublicKey from PrivateKey (String) for curve secp256k1");

System.out.println("Check keys with https://gobittest.appspot.com/Address");

// https://gobittest.appspot.com/Address

String privateKey = "D12D2FACA9AD92828D89683778CB8DFCCDBD6C9E92F6AB7D6065E8AACC1FF6D6";

String publicKeyExpected = "04661BA57FED0D115222E30FE7E9509325EE30E7E284D3641E6FB5E67368C2DB185ADA8EFC5DC43AF6BF474A41ED6237573DC4ED693D49102C42FFC88510500799";

System.out.println("\nprivatekey given : " + privateKey);

System.out.println("publicKeyExpected: " + publicKeyExpected);

// routine with bouncy castle

System.out.println("\nGenerate PublicKey from PrivateKey with BouncyCastle");

ECNamedCurveParameterSpec spec = ECNamedCurveTable.getParameterSpec("secp256k1"); // this ec curve is used for bitcoin operations

org.bouncycastle.math.ec.ECPoint pointQ = spec.getG().multiply(new BigInteger(1, hexStringToByteArray(privateKey)));

byte[] publickKeyByte = pointQ.getEncoded(false);

String publicKeyBc = byteArrayToHexString(publickKeyByte);

System.out.println("publicKeyExpected: " + publicKeyExpected);

System.out.println("publicKey BC : " + publicKeyBc);

System.out.println("publicKeys match : " + publicKeyBc.contentEquals(publicKeyExpected));

// regeneration of ECPublicKey with java native starts here

System.out.println("\nGenerate PublicKey from PrivateKey with Java native routines");

// the preset "303E.." only works for elliptic curve secp256k1

// see answer by user dave_thompson_085

// https://stackoverflow.com/questions/48832170/generate-ec-public-key-from-byte-array-private-key-in-native-java-7

String privateKeyFull = "303E020100301006072A8648CE3D020106052B8104000A042730250201010420" +

privateKey;

byte[] privateKeyFullByte = hexStringToByteArray(privateKeyFull);

System.out.println("privateKey full : " + privateKeyFull);

KeyFactory kecFactory = KeyFactory.getInstance("EC");

PrivateKey privateKeyNative = kecFactory.generatePrivate(new PKCS8EncodedKeySpec(privateKeyFullByte));

ECPrivateKey ecPrivateKeyNative = (ECPrivateKey) privateKeyNative;

ECPublicKey ecPublicKeyNative = getPublicKey(ecPrivateKeyNative);

byte[] ecPublicKeyNativeByte = ecPublicKeyNative.getEncoded();

String publicKeyNativeFull = byteArrayToHexString(ecPublicKeyNativeByte);

String publicKeyNativeHeader = publicKeyNativeFull.substring(0, 46);

String publicKeyNativeKey = publicKeyNativeFull.substring(46, 176);

System.out.println("ecPublicKeyFull : " + publicKeyNativeFull);

System.out.println("ecPublicKeyHeader: " + publicKeyNativeHeader);

System.out.println("ecPublicKeyKey : " + publicKeyNativeKey);

System.out.println("publicKeyExpected: " + publicKeyExpected);

System.out.println("publicKeys match : " + publicKeyNativeKey.contentEquals(publicKeyExpected));

}

private static String byteArrayToHexString(byte[] a) {

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/weixin_42484477/article/details/118825744

智能推荐

AVFrame&AVPacket_天天av-程序员宅基地

文章浏览阅读1.5w次。AVFrame:( This structure describes decoded (raw) audio or video data. AVFrame must be allocated using av_frame_alloc(). Note that this only allocates the AVFrame itself, the buffers for the data mus_天天av

Java经典例题07:用100元人民币兑换10元、5元、1元的纸币_编程把100元换成1元5元10元-程序员宅基地

文章浏览阅读3.5k次,点赞2次,收藏12次。解题思路分析:1.100元兑换10元纸币,可以兑换10张,但每种纸币都要有,所以最多只能兑换9张,最少兑换1张。则初始值为1;循环条件小于10或者小于等于9。2.100元兑换5元纸币,可以兑换20,但每种纸币都要有,所以最多只能兑换19张,最少兑换1张。初始值为1;循环条件小于20或者小于等于19。3.100元兑换1元纸币,可以兑换100张,但每种纸币都要有,所以最多只能兑换99张,最少兑换1张。则初始值为1;循环条件小于100或者小于等于99。_编程把100元换成1元5元10元

猜三次年龄_找人猜三次年龄-程序员宅基地

文章浏览阅读450次。1、允许用户最多尝试三次2、每尝试三次后,如果还没猜对,就问用户是否继续玩,如果回答Y,y,就继续猜三次,以此往复,如果回答N,n,就直接退出times=0count=3while times<=3:age=int(input(‘请输入年龄:’))if age == 18:print(‘猜对了’)breakelif age > 18:print(‘猜大了’)else:print(‘猜小了’)times+=1if times3:choose = input(‘继续猜Y_找人猜三次年龄

SDOI2017 Round2 详细题解-程序员宅基地

文章浏览阅读152次。这套题实在是太神仙了。。做了我好久。。。好多题都是去搜题解才会的 TAT。剩的那道题先咕着,如果省选没有退役就来填吧。「SDOI2017」龙与地下城题意丢 \(Y\) 次骰子,骰子有 \(X\) 面,每一面的概率均等,取值为 \([0, X)\) ,问最后取值在 \([a, b]\) 之间的概率。一个浮点数,绝对误差不超过 \(0.013579\) 为正确。数据范围每组数据有 \...

嵌入式数据库-Sqlite3-程序员宅基地

文章浏览阅读1.1k次,点赞36次,收藏25次。阅读引言: 本文将会从环境sqlite3的安装、数据库的基础知识、sqlite3命令、以及sqlite的sql语句最后还有一个完整的代码实例, 相信仔细学习完这篇内容之后大家一定能有所收获。

C++ Builder编写WinForm从Web服务器下载文件-程序员宅基地

文章浏览阅读51次。UnicodeString templateSavePath = ChangeFileExt(ExtractFilePath(Application->ExeName),"tmp.doc");IdAntiFreeze1->OnlyWhenIdle = false;//设置使程序有反应.TMemoryStream *templateStream ;templateStre..._c++webserver下载文件

随便推点

JAVA小项目潜艇大战_java潜艇大战-程序员宅基地

文章浏览阅读8.3k次,点赞10次,收藏41次。一、第一天1、创建战舰、侦察潜艇、鱼雷潜艇、水雷潜艇、水雷、深水炸弹类完整代码:package day01;//战舰public class Battleship { int width; int height; int x; int y; int speed; int life; void move(){ System.out.println("战舰移动"); }}package day01;//侦察潜艇_java潜艇大战

02表单校验的基本步骤-程序员宅基地

文章浏览阅读940次。表单校验的基本步骤_表单校验

libOpenBlas.dll缺失依赖解决办法-程序员宅基地

文章浏览阅读4.5k次。libOpenBlas.dll缺失依赖解决办法 intellij idea 1.dll文件缺失依赖,报错:“找不到指定模块”2.下载depends查看dll缺失文件3.下载缺失依赖libopenblas.dll出错起因由于java web项目需要调用openBlas库来进行运算,就下载了预编译的libopenblas文件进行调用,首先遇到路径出错问题、之后又是dll文件缺失依赖问题,以下是解决..._libopenblas.dll

Swoole 实践篇之结合 WebSocket 实现心跳检测机制-程序员宅基地

文章浏览阅读251次,点赞3次,收藏10次。这里实现的心跳检测机制是一个基础版的,心跳包的主要作用是用于检测用户端是否存活,有助于我们及时判断用户端是否存在断线的问题。在我之前开发过的项目中,有一个基于物联网在线直播抓娃娃的项目,其中就有需要实时监控设备在线状态的需求,该需求就是使用心跳包来实现的。实际上心跳检测技术,应用更广泛的是实时通信、或设备管理的场景偏多。

Maven dependency scope_maven dependent scope-程序员宅基地

文章浏览阅读714次。Dependency scope is used to limit the transitivity of a dependency, and also to affect the classpath used for various build tasks.There are 6 scopes available:compileThis is the default scop_maven dependent scope

TCP头部结构信息_tcp头部包含哪些信息-程序员宅基地

文章浏览阅读3.6k次。TCP 头部结构信息_tcp头部包含哪些信息