How to construct PrivateKey if you know the curve name & raw private key/point?

前端 未结 1 909
再見小時候
再見小時候 2021-01-06 14:11

Requirement: For a given named-curve, send as little data as you can, so that receiver can construct EC PrivateKey.

I am currently using BouncyCastle/SpongyCastle on

相关标签:
1条回答
  • 2021-01-06 14:30

    I was able to figure out how to re-construct EC PrivateKey from BigInteger D & curve name.

     public static PrivateKey getPrivateKeyFromECBigIntAndCurve(BigInteger s, String curveName) {
        X9ECParameters ecCurve = ECNamedCurveTable.getByName(curveName);
        ECParameterSpec ecParameterSpec = new ECNamedCurveSpec(curveName, ecCurve.getCurve(), ecCurve.getG(), ecCurve.getN(), ecCurve.getH(), ecCurve.getSeed());
        ECPrivateKeySpec privateKeySpec = new ECPrivateKeySpec(s, ecParameterSpec);
        try {
            KeyFactory keyFactory = KeyFactory.getInstance("EC");
            return keyFactory.generatePrivate(privateKeySpec);
        } catch (NoSuchAlgorithmException | InvalidKeySpecException e) {
            e.printStackTrace();
            return null;
        }
    }
    
    0 讨论(0)
提交回复
热议问题