久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

從私鑰派生 ECDSA 公鑰

Deriving ECDSA Public Key from Private Key(從私鑰派生 ECDSA 公鑰)
本文介紹了從私鑰派生 ECDSA 公鑰的處理方法,對(duì)大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

我試圖從私鑰生成公共 ECDSA 密鑰,但我沒有設(shè)法在互聯(lián)網(wǎng)上找到有關(guān)如何執(zhí)行此操作的太多幫助.幾乎所有東西都是為了從公鑰規(guī)范生成公鑰,我不知道如何得到它.到目前為止,這是我整理的:

I was attempting to generate a public ECDSA key from a private key, and I haven't managed to find much help on the internet as to how to do this. Pretty much everything is for generating a public key from a public key spec, and I don't know how to get that. So far, this is what I've put together:

public void setPublic() throws GeneralSecurityException {
    ECNamedCurveParameterSpec params = ECNamedCurveTable.getParameterSpec("secp256k1");
    KeyFactory fact = KeyFactory.getInstance("ECDSA", "BC");
    ECCurve curve = params.getCurve();
    java.security.spec.EllipticCurve ellipticCurve = EC5Util.convertCurve(curve, params.getSeed());
    java.security.spec.ECPoint point = ECPointUtil.decodePoint(ellipticCurve, this.privateKey.getEncoded());
    java.security.spec.ECParameterSpec params2=EC5Util.convertSpec(ellipticCurve, params);
    java.security.spec.ECPublicKeySpec keySpec = new java.security.spec.ECPublicKeySpec(point,params2);
    this.publicKey = fact.generatePublic(keySpec);
}

但是,在運(yùn)行時(shí),我收到以下錯(cuò)誤:

However, when running, I get the following error:

Exception in thread "main" java.lang.IllegalArgumentException: Invalid point encoding 0x30
at org.bouncycastle.math.ec.ECCurve.decodePoint(Unknown Source)
at org.bouncycastle.jce.ECPointUtil.decodePoint(Unknown Source)
at Wallet.Wallet.setPublic(Wallet.java:125)

我做錯(cuò)了什么?有沒有更好/更簡(jiǎn)單的方法來做到這一點(diǎn)?

What am I doing wrong? Is there a better/easier way to do this?

我已經(jīng)設(shè)法編譯了一些代碼,但它不能正常工作:

I've managed to get some code to compile, but it does not work correctly:

public void setPublic() throws GeneralSecurityException {
    BigInteger privKey = new BigInteger(getHex(privateKey.getEncoded()),16);
    X9ECParameters ecp = SECNamedCurves.getByName("secp256k1");
    ECPoint curvePt = ecp.getG().multiply(privKey);
    BigInteger x = curvePt.getX().toBigInteger();
    BigInteger y = curvePt.getY().toBigInteger();
    byte[] xBytes = removeSignByte(x.toByteArray());
    byte[] yBytes = removeSignByte(y.toByteArray());
    byte[] pubKeyBytes = new byte[65];
    pubKeyBytes[0] = new Byte("04");
    System.arraycopy(xBytes, 0, pubKeyBytes, 1, xBytes.length);
    System.arraycopy(yBytes, 0, pubKeyBytes, 33, xBytes.length);




    ECNamedCurveParameterSpec params = ECNamedCurveTable.getParameterSpec("secp256k1");
    KeyFactory fact = KeyFactory.getInstance("ECDSA", "BC");
    ECCurve curve = params.getCurve();
    java.security.spec.EllipticCurve ellipticCurve = EC5Util.convertCurve(curve, params.getSeed());
    java.security.spec.ECPoint point = ECPointUtil.decodePoint(ellipticCurve, pubKeyBytes);
    java.security.spec.ECParameterSpec params2 = EC5Util.convertSpec(ellipticCurve, params);
    java.security.spec.ECPublicKeySpec keySpec = new java.security.spec.ECPublicKeySpec(point,params2);
    this.publicKey = fact.generatePublic(keySpec);
}

private byte[] removeSignByte(byte[] arr)
{
    if(arr.length==33)
    {
        byte[] newArr = new byte[32];
        System.arraycopy(arr, 1, newArr, 0, newArr.length);
        return newArr;
    }
    return arr;
}

當(dāng)我運(yùn)行它時(shí),它會(huì)生成一個(gè)公鑰,但它與私鑰對(duì)應(yīng)的不是同一個(gè).

When I run it, it generates a publicKey, but it's not the same one that the private key corresponds to.

推薦答案

所以過了一會(huì)兒,我想出了一個(gè)解決方案,并決定發(fā)布它以防其他人和我有同樣的問題:

So after a while, I figured out a solution and decided to post it in case anyone else has the same issue as me:

KeyFactory keyFactory = KeyFactory.getInstance("ECDSA", "BC");
    ECParameterSpec ecSpec = ECNamedCurveTable.getParameterSpec("secp256k1");

    ECPoint Q = ecSpec.getG().multiply(((org.bouncycastle.jce.interfaces.ECPrivateKey) this.privateKey).getD());

    ECPublicKeySpec pubSpec = new ECPublicKeySpec(Q, ecSpec);
    PublicKey publicKeyGenerated = keyFactory.generatePublic(pubSpec);
    this.publicKey = publicKeyGenerated;

根據(jù)@MaartenBodewes 評(píng)論刪除了解碼 ECPoint 的代碼.

Removed the code decoding the ECPoint as per @MaartenBodewes comment.

這篇關(guān)于從私鑰派生 ECDSA 公鑰的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

Java Remove Duplicates from an Array?(Java從數(shù)組中刪除重復(fù)項(xiàng)?)
How to fix Invocation failed Unexpected Response from Server: Unauthorized in Android studio(如何修復(fù)調(diào)用失敗來自服務(wù)器的意外響應(yīng):在 Android 工作室中未經(jīng)授權(quán))
AES encryption, got extra trash characters in decrypted file(AES 加密,解密文件中有多余的垃圾字符)
AES Error: Given final block not properly padded(AES 錯(cuò)誤:給定的最終塊未正確填充)
Detecting incorrect key using AES/GCM in JAVA(在 JAVA 中使用 AES/GCM 檢測(cè)不正確的密鑰)
AES-256-CBC in Java(Java 中的 AES-256-CBC)
主站蜘蛛池模板: 国产精品久久久久久中文字 | 国产1区2区3区 | 国产精品久久久久久一区二区三区 | 啪一啪在线视频 | 91高清在线观看 | 久久久久久久一区 | 久久国产精彩视频 | 日韩不卡视频在线观看 | 男女羞羞视频在线观看 | 狠狠干天天干 | 久久99网 | 免费成人高清在线视频 | 亚洲国产高清高潮精品美女 | 男女视频在线看 | 久草新在线 | 国产区在线视频 | 久久精品小视频 | 91综合在线观看 | 亚洲精品免费视频 | 国产一区二区三区四区三区四 | 午夜精品久久久 | 一区二区三区视频在线观看 | 欧美日韩亚洲视频 | 亚洲精品一| 久久久久国产精品午夜一区 | 国产激情视频网 | 亚洲在线免费 | 中文字幕一区二区三区四区不卡 | 精品国产久 | 三极网站 | 成人午夜影院 | 久久午夜电影 | 91视频精选 | 亚洲欧美日韩系列 | 欧美日韩电影一区二区 | 一级毛片中国 | 不卡一二三区 | 日韩精品一区二区三区在线观看 | 欧美高清免费 | 日韩精品一区二区三区第95 | 成年网站在线观看 |