How to convert public key to BigInteger

放肆的年华 提交于 2019-12-11 09:49:29

问题


I need to convert a public key into a BigInteger. In the code below I have extracted the private key and stored it as BigInteger. But when I followed the same method for the public it is not working.

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);

回答1:


In ECDSA the private key is a BigInteger, but the public key is an ECPoint (consisting of two BigInteger values). Converting it to BigInteger won't work. Use

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


来源:https://stackoverflow.com/questions/10118224/how-to-convert-public-key-to-biginteger

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!