如何转换公钥的BigInteger(How to convert public key to Big

2019-10-16 14:23发布

我需要一个公共密钥转换为一个BigInteger。 在下面的代码中,我已经提取的私有密钥并将其存储为BigInteger 。 但是,当我遵循同样的方法对公众它不工作。

public static void main(String[] args) throws  Exception {
    Security.addProvider(new BouncyCastleProvider());
    BigInteger ZERO=new BigInteger("0");
    int c;
    //  ECParameterSpec ecSpec = ECNamedCurveTable.getParameterSpec("P-224");
    ECCurve curve = new ECCurve.Fp(
          new       BigInteger("883423532389192164791648750360308885314476597252960362792450860609699839"), // q
          new BigInteger("7fffffffffffffffffffffff7fffffffffff8000000000007ffffffffffc", 16), // a
          new BigInteger("6b016c3bdcf18941d0d654921475ca71a9db2fb27d1d37796185c2942c0a", 16)); // b

    ECParameterSpec ecSpec = new ECParameterSpec(
          curve,
          curve.decodePoint( Hex.decode("020ffa963cdca8816ccc33b8642bedf905c3d358573d3f27fbbd3b3cb9aaaf")), // G
              new   BigInteger("883423532389192164791648750360308884807550341691627752275345424702807307")); // n
    KeyPairGenerator kpg = KeyPairGenerator.getInstance("ECDSA", "BC");
    kpg.initialize(ecSpec, new SecureRandom());
    KeyPair keyPair = kpg.generateKeyPair();
    PublicKey pubKey = keyPair.getPublic();
    System.out.println(pubKey);
    PrivateKey privKey = keyPair.getPrivate();
    System.out.println(privKey);
    BigInteger s = ((ECPrivateKey) privKey).getS();
    System.out.println(s);

Answer 1:

在ECDSA私钥是BigInteger ,但公钥是ECPoint (包括两个BigInteger值)。 将其转换为BigInteger将无法工作。 采用

ECPoint w = ((ECPublicKey)pubKey).getW();
BigInteger wx = w.getAffineX(), wy = w.getAffineY();
System.out.println(wx);
System.out.println(wy);


文章来源: How to convert public key to BigInteger