从ANSI X9.62编码的字节

时间:2017-06-22 13:01:24

标签: java cryptography ecdsa

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

1 个答案:

答案 0 :(得分:1)

假设您已经将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