ECDH密钥协商中secp256r1椭圆曲线坐标无效怎么办?

ecdh 密钥协商中 secp256r1 椭圆曲线坐标无效

在 secp256r1 椭圆曲线上进行 ecdh 密钥协商时,您可能会遇到“坐标无效”错误。这可能是由于以下原因导致:

问题分析:

您提到的代码中,getpublickeyfromxy() 方法将十六进制字符串转换为 bigint 时,没有指定正号。这可能会导致负 bigint,从而导致坐标解析失败。

解决方案:

解决这个问题,需要确保在转换 bigint 时指定正号。以下是修改后的 getpublickeyfromxy() 方法:

public static PublicKey getPublicKeyFromXY(String hexStr) {
  Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
  ECNamedCurveParameterSpec spec = ECNamedCurveTable.getPa

rameterSpec("secp256r1"); byte[] coordinates = HexUtil.decode(hexStr); BigInteger x = new BigInteger(1, coordinates.slice(1, 33)); BigInteger y = new BigInteger(1, coordinates.slice(33)); ECPoint point = new ECPoint(x, y); return KeyFactory.getInstance("EC", "BC").generatePublic(new ECPublicKeySpec(point, spec)); }

在修改后的方法中,x 和 y 被转换为正 bigint,从而修复了坐标解析问题。