简体   繁体   English

如何从其私钥生成 ECDSA 公钥?

[英]How to generate the ECDSA public key from its private key?

The following site is frequently referenced and, I assume, accurate:以下网站经常被引用,我认为是准确的:

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

I'm trying to repro these steps in Golang but failing at the first step :-(我正在尝试在 Golang 中重现这些步骤,但在第一步失败了 :-(

Is someone able to provide me with a Golang snippet that, given a ECDSA private key, returns the public key?有人能够为我提供一个 Golang 片段,在给定 ECDSA 私钥的情况下,返回公钥吗? I think I may specifically mean the private key exponent and public key exponent per the above site's examples.我想我可能专门指上述网站示例中的私钥指数和公钥指数。

ie given eg a randomly-generated (hex-encoded) private key (exponent?) E83385AF76B2B1997326B567461FB73DD9C27EAB9E1E86D26779F4650C5F2B75 returns the public key 04369D83469A66920F31E4CF3BD92CB0BC20C6E88CE010DFA43E5F08BC49D11DA87970D4703B3ADBC9A140B4AD03A0797A6DE2D377C80C369FE76A0F45A7A39D3F即赋予例如随机生成(十六进制编码)的私有密钥(指数?) E83385AF76B2B1997326B567461FB73DD9C27EAB9E1E86D26779F4650C5F2B75返回公钥04369D83469A66920F31E4CF3BD92CB0BC20C6E88CE010DFA43E5F08BC49D11DA87970D4703B3ADBC9A140B4AD03A0797A6DE2D377C80C369FE76A0F45A7A39D3F

I've found many (relevant) results:我发现了许多(相关)结果:

https://crypto.stackexchange.com/questions/5756/how-to-generate-a-public-key-from-a-private-ecdsa-key https://crypto.stackexchange.com/questions/5756/how-to-generate-a-public-key-from-a-private-ecdsa-key

But none that includes a definitive example.但没有一个包括一个明确的例子。

Go's crypto/ecdsa module allows keys to generated and includes a Public function on the type but this returns the PublicKey property. Go 的crypto/ecdsa模块允许生成密钥并在类型上包含一个Public函数,但这会返回PublicKey属性。

Alternative ways that start from a private key appear to require going through a PEM-encoded (including a DER-encoded ASN) form of the key which feels circuitous (and I would need to construct).从私钥开始的替代方法似乎需要通过 PEM 编码(包括 DER 编码的 ASN)形式的密钥,这感觉很迂回(我需要构建)。

Update:更新:

See the answers below: andrew-w-phillips@ and kelsnare@ provided the (same|correct) solution.请参阅以下答案:andrew-w-phillips@ 和 kelsnare@ 提供了(相同|正确的)解决方案。 Thanks to both of them!感谢他们俩!

For posterity, Bitcoin (and Ethereum) use an elliptic curve defined by secp256k1 .对于后代,比特币(和以太坊)使用由secp256k1定义的椭圆曲线。 The following code from andrew-w-phillips@ and kelsnare@ using Ethereum's implementation of this curve, works:来自 andrew-w-phillips@ 和 kelsnare@ 的以下代码使用以太坊对这条曲线的实现,有效:

import (
    "crypto/ecdsa"
    "crypto/elliptic"
    "fmt"
    "math/big"
    "strings"

    "github.com/ethereum/go-ethereum/crypto/secp256k1"
)
func Public(privateKey string) (publicKey string) {
    var e ecdsa.PrivateKey
    e.D, _ = new(big.Int).SetString(privateKey, 16)
    e.PublicKey.Curve = secp256k1.S256()
    e.PublicKey.X, e.PublicKey.Y = e.PublicKey.Curve.ScalarBaseMult(e.D.Bytes())
    return fmt.Sprintf("%x", elliptic.Marshal(secp256k1.S256(), e.X, e.Y))
}
func main() {
    privateKey := "E83385AF76B2B1997326B567461FB73DD9C27EAB9E1E86D26779F4650C5F2B75"
    log.Println(strings.ToUpper(Public(privateKey)))
}

yields:产量:

04369D83469A66920F31E4CF3BD92CB0BC20C6E88CE010DFA43E5F08BC49D11DA87970D4703B3ADBC9A140B4AD03A0797A6DE2D377C80C369FE76A0F45A7A39D3F

After reading the answer by Andrew W. Phillips and a little help from https://github.com/bitherhq/go-bither/tree/release/1.7/crypto在阅读了Andrew W. Phillips的回答以及https://github.com/bitherhq/go-bither/tree/release/1.7/crypto的一些帮助后

package main

import (
    "crypto/ecdsa"
    "crypto/elliptic"
    "crypto/rand"
    "fmt"
    "log"
    "math/big"
)

func PubBytes(pub *ecdsa.PublicKey) []byte {
    if pub == nil || pub.X == nil || pub.Y == nil {
        return nil
    }
    return elliptic.Marshal(elliptic.P256(), pub.X, pub.Y)
}

func toECDSAFromHex(hexString string) (*ecdsa.PrivateKey, error) {
    pk := new(ecdsa.PrivateKey)
    pk.D, _ = new(big.Int).SetString(hexString, 16)
    pk.PublicKey.Curve = elliptic.P256()
    pk.PublicKey.X, pk.PublicKey.Y = pk.PublicKey.Curve.ScalarBaseMult(pk.D.Bytes())
    return pk, nil
}

func main() {
    pHex := "E83385AF76B2B1997326B567461FB73DD9C27EAB9E1E86D26779F4650C5F2B75"
    pk, err := toECDSAFromHex(pHex)
        if err != nil {
        log.Fatal(err.Error())
    }
    fmt.Printf("Generated Public Key: %x\n", PubBytes(&pk.PublicKey))

    hash := []byte("Hello Gopher!")

    fmt.Printf("\nSigning...\n\n")
    r, s, err := ecdsa.Sign(rand.Reader, pk, hash)
    if err != nil {
        log.Fatal(err.Error())
    }
    fmt.Printf("\nVerifying..\n\n")
    if ecdsa.Verify(&pk.PublicKey, hash, r, s) {
        fmt.Println("Success!!")
    } else {
        fmt.Println("Failure!!")
    }
}
// Output
// Generated Public Key: 04265a5015c0cfd960e5a41f35e0a87874c1d8a28289d0d6ef6ac521ad49c3d80a8a7019ceef189819f066a947ad5726db1a4fe70a3208954c46b0e60f2bf7809c
// 
// Signing...
// 
// 
// Verifying..
// 
// Success!!

======== ORIGINAL ANSWER =========== ========原始答案==========

Dunno much of crypto but crypto/elliptic has a Marshal function不知道多少加密,但加密/椭圆具有元帅功能

So once you have a * PrivateKey maybe the below will work所以一旦你有一个 * PrivateKey ,下面的可能会起作用

import (
  "crypto/elliptic"
  "crypto/ecdsa"
)
var privKey *ecdsa.PrivateKey
func main() {
  // some init to privKey
  pk := privKey.PublicKey()
  keybuf := elliptic.Marshal(pk.Curve, pk.X, pk.Y)
  log.Printf("Key: %s\n", string(keybuf))
}

I'm shooting completely in the dark with this.我正在用这个完全在黑暗中拍摄。 Hope it helps希望能帮助到你

I haven't got that low-level but maybe something like this:我没有那么低级,但也许是这样的:

var pri ecdsa.PrivateKey
pri.D, _ = new(big.Int).SetString("E83385AF76B2B1997326B567461FB73DD9C27EAB9E1E86D26779F4650C5F2B75",16)
pri.PublicKey.Curve = elliptic.P256()
pri.PublicKey.X, pri.PublicKey.Y = pri.PublicKey.Curve.ScalarBaseMult(pri.D.Bytes())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM