技术标签: 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) {
文章浏览阅读1.4w次,点赞13次,收藏19次。将个人照片的pdf放到根目录下,实现代码:\begin{IEEEbiography}[{\includegraphics[width=1in,height=1.25in,clip,keepaspectratio]{Robot_Starscream.pdf}}]{Robot_Starscream}A good boy\end{IEEEbiography}效果:_ieeebiography
文章浏览阅读631次。要求:代码:class TV{ int channel; String name; TV(String name) { this.name = name; channel = 5; } public void setChannel(int channel) { this.channel = channel; } public _java算法:小明看电视
文章浏览阅读2.3k次。在使用sm30维护表数据时,经常会要求一些字段是不可输入的,它们的值要自动带出来,也就是给一个默认值,比如一些描述字段,表数据的维护人,维护日期以及时间。这些是通过SE54中的“事件(EVENT)”来实现的。下面以一个小例子详细说一下具体的实现过程。1,首先要有一个表,咔嚓~~~下面这个表就创建好了,4个字段,包括2个描述字段-物料组描述和采购组描述,这两个字段就打算在表维护视图中做成不可输入..._abap sm30 默认值
文章浏览阅读3.3k次,点赞2次,收藏3次。详细论述了Qt(QtWebEngine)加载本地网页跨域的问题。_qt 调用地图提示跨域加载失败
文章浏览阅读661次,点赞26次,收藏21次。本文介绍了三种典型的卷积神经网络:LeNet-5、AlexNet和ResNet。这些网络在图像分类、目标检测和语义分割等计算机视觉任务中取得了巨大成功。它们的设计思想和结构各不相同,但都为深度学习在图像处理领域的发展做出了重要贡献。
文章浏览阅读3.1k次,点赞17次,收藏48次。这里写自定义目录标题欢迎使用Markdown编辑器新的改变功能快捷键合理的创建标题,有助于目录的生成如何改变文本的样式插入链接与图片如何插入一段漂亮的代码片生成一个适合你的列表创建一个表格设定内容居中、居左、居右SmartyPants创建一个自定义列表如何创建一个注脚注释也是必不可少的KaTeX数学公式新的甘特图功能,丰富你的文章UML 图表FLowchart流程图导出与导入导出导入欢迎使用Markdown编辑器你好! 这是你第一次使用 Markdown编辑器 所展示的欢迎页。如果你想学习如何使用Mar_在ultra96开发板上部署训练好的模型
文章浏览阅读1.6k次,点赞33次,收藏21次。ArrayList底层是一段连续的空间,并且可以动态扩容,是一个动态类型的顺序表,与普通数组的区别就是它是没有固定大小的限制。
文章浏览阅读7.8k次,点赞11次,收藏72次。实现稀疏矩阵(采用三元组表示)的基本运算假设nxn的稀疏矩阵A采用三元组表示,设计一个程序exp4-2.cpp,完成如下功能。(1)生成如下两个稀疏矩阵的三元组a和ba=1 0 3 00 1 0 00 0 1 00 0 1 1b=3 0 0 00 4 0 00 0 1 00 0 2 0(2)输出a矩阵的三元组(3)输出b的转置矩阵的三元组代码如下//稀疏矩阵的三元组表示-算法#include <stdio.h>#define M 4#define N 4#d_实现稀疏矩阵(采用三元组表示)的基本运算
文章浏览阅读2.8k次,点赞8次,收藏21次。在利用Xilinx Zynq开发板进行PCIe数据传输时,经常用到XDMA这个IP,并配套Xilinx官方测试程序xdma_rw.exe进行PC到开发板的数据传输测试。可是在传输超过一定大小的数据时,就会失败。是一个叫XDMA_MAX_TRANSFER_SIZE的参数限制了传输数据量,将其修改为合适的大小即可。_xdma读取非8字节卡死
文章浏览阅读344次。前提:非对称加密算法会产生一把公钥和一把私钥,私钥我们自己留着,公钥可以发给别人。公钥和私钥都可以加密信息,公钥加密私钥解密。私钥加密公钥解密。CA认证中心,可以生成数字证书,公钥是公开的且不可伪造。信用hash算法可以得到digest,digest用私钥加密就变成数字签名。验证不可否认性:验证数据安全性:我的公钥已经发给你了。我要给你发信,你怎么知道我的信没有被别人改动过?我需要把信的内容通过hash算法得到digest,再用私钥加密成数字签名附在信里发出。.._伪造公钥
文章浏览阅读347次。查了好多资料,发现还是不全,干脆自己整理吧,至少保证在我的做法正确的,以免误导读者,也是给自己做个记录吧! Parent-Child cursor (父子游标) 父游标:只要SQL语句文本相同,它们就对应同一个parent cursor。 子游标:在某些情况下,虽然SQL语句的文本相同,但是因为其它因素不同(这些因素可以在视图V$S...
文章浏览阅读242次。知识点:Android中的VideoView控件默认情况下是不能全屏播放的,它是固有的比例播放,假如你放在默认播放会有黑框解决的办法是重写VideoView控件实现代码如下:[code="java"]package com.shenghe.bank.landi.helper;import android.content.Context;import android...._videoview有没有重新绘制方法