gpt4 book ai didi

java - 从 ANSI X9.62 编码字节构造 ECPublicKey

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:50:07 24 4
gpt4 key购买 nike

我需要通过 ECDSA secp256k1 曲线验证远程生成的签名。我得到 64 个字节,代表 r 和 s 的原始字节。以及 ANSI X9.62 编码的公钥 ([0x04, X (32 bytes), Y (32 bytes)])。如何使用此数据验证签名?

最佳答案

假设您已经将 x 和 y 作为 BigIntegers:

// Create the W point
ECPoint w = new ECPoint(x, y);

// Create the ECField from hey integer (p)
ECField field = new ECFieldFp(new BigInteger("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2F", 16));

// Create the EllipticCurve with a and b
EllipticCurve curve = new EllipticCurve(field, new BigInteger("0"), new BigInteger("7"));

// Create the G point:
BigInteger gx = new BigInteger("79BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798", 16);
BigInteger gy = new BigInteger("483ADA7726A3C4655DA4FBFC0E1108A8FD17B448A68554199C47D08FFB10D4B8", 16);
ECPoint g = new ECPoint(gx, gy);

// Create ECParameterSpec
ECParameterSpec spec = new ECParameterSpec(curve, new ECPoint(gx, gy), new BigInteger("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141", 16), 1);

// Create the KeySpec:
KeySpec keySpec = new ECPublicKeySpec(w, spec);

// Generate the key
KeyFactory keyFactory = KeyFactory.getInstance("EC");
PublicKey key = keyFactory.generatePublic(keySpec);

此处使用的常量可用here

关于java - 从 ANSI X9.62 编码字节构造 ECPublicKey,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44700175/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com