我正试图解析一个EC点
04410478ed041c12ddaf693958f91f1174e0c790b2ff580ddca39bc2a4f78ad041dc379aaefe27cede2fa7601f90e3f397938ee53268564e346ac7a58aac8c28ca5415
到具有以下代码的ecdsa.PublickKey结构:
x, y := readECPoint(elliptic.P256(), point)
if x == nil || y == nil {
panic("unable to parse the public key")
}
pubKey := &ecdsa.PublicKey{Curve: elliptic.P256(), X: x, Y: y}
pubKeyBytes := elliptic.Marshal(curve, pubKey.X, pubKey.Y)在github上浏览时,我注意到了这段代码,这对我来说很有意义:
func readECPoint(curve elliptic.Curve, ecpoint []byte) (*big.Int, *big.Int) {
x, y := elliptic.Unmarshal(curve, ecpoint)
if x == nil {
// http://docs.oasis-open.org/pkcs11/pkcs11-curr/v2.40/os/pkcs11-curr-v2.40-os.html#_ftn1
// PKCS#11 v2.20 specified that the CKA_EC_POINT was to be store in a DER-encoded
// OCTET STRING.
var point asn1.RawValue
asn1.Unmarshal(ecpoint, &point)
if len(point.Bytes) > 0 {
x, y = elliptic.Unmarshal(curve, point.Bytes)
}
}
return x, y
}我试过的其他事情有:
curve := new(secp256k1.BitCurve)
x, y := curve.Unmarshal(point)但是,所有以前的代码都会返回x=nil和y=nil。
我知道KeyPair是在带有比特币曲线的高速加工中生成的。
Asn1.ObjectIdfier 1.3.132.0.10
我是否遗漏了其他东西来正确解析比特币/虚空曲线中的EC点?
发布于 2021-03-14 10:07:35
第一个代码片段似乎是错误的,因为elliptic.P256() 返回是一条实现NISTP-256的曲线,也称为secp256r1或prime256v1。但是比特币/以太曲线使用的是secp256k1格式,这是不同的。
我建议使用去乙醚包创建secp256k1曲线。
我还发现你的钥匙出了问题。它的长度为67,但公钥的大小必须为65 (参见源代码)。正如Topaco所指出的,前两个字节可能是长度为65字节(0x41)的八进制字符串(0x04)的ASN.1编码,因此实际键从第三个字节开始。
我编写了一个使用go-etherium解析密钥的最小示例:
package main
import (
"encoding/hex"
"fmt"
"github.com/ethereum/go-ethereum/crypto/secp256k1"
)
func main() {
c := *secp256k1.S256()
data, err := hex.DecodeString("04410478ed041c12ddaf693958f91f1174e0c790b2ff580ddca39bc2a4f78ad041dc379aaefe27cede2fa7601f90e3f397938ee53268564e346ac7a58aac8c28ca5415")
if err != nil {
panic(err)
}
x, y := c.Unmarshal(data[2:])
fmt.Printf("%v\n%v\n", x, y)
}输出:
54696312948195154784868816119600719747374302831648955727311433079671352319031
69965364187890201668291488802478374883818025489090614849107393112609590498325发布于 2021-03-14 09:59:39
我终于找到了一种方法,虽然比我想象的更手动,也许有人有更好的方法?
curve := new(secp256k1.BitCurve)
pubKey := ecdsa.PublicKey{
Curve: curve,
X: &big.Int{},
Y: &big.Int{},
}
xBytes := point[3:35] //the point array has 67 bytes, ignore the first 2
yBytes := point[35:]
pubKey.X = new(big.Int).SetBytes(xBytes)
pubKey.Y = new(big.Int).SetBytes(yBytes)https://stackoverflow.com/questions/66616947
复制相似问题