Changing the parameter of a java method

Asked

Viewed 75 times

1

I am developing a code that uses ECC encryption. I am using the following code:

import java.security.*;
import java.security.spec.*;

public class Teste {
 public static void main(String[] args) {
            KeyPairGenerator kpg;
            kpg = KeyPairGenerator.getInstance("EC","SunEC");
            ECGenParameterSpec ecsp;
            ecsp = new ECGenParameterSpec("secp256r1");
            kpg.initialize(ecsp);
            KeyPair kp = kpg.genKeyPair();
            PrivateKey privKey = kp.getPrivate();
            PublicKey pubKey = kp.getPublic();
            System.out.println(pubKey.toString());
  }
}

The output shown on the console is as follows:

Sun EC public key, 256 bits
  public x coord: 4615855875618351712635877256976103420366861757278971514413945892411643339267
  public y coord: 86295372175394984599678936024544037153778609141128039300731169643337680519305

I tried to change some things in the code but saw that this print model must come from some specific method (KeyPairGenerator.getInstance("EC","SunEC"); or new ECGenParameterSpec("secp256r1");).

How do I access this method and change the print to show only the numbers and disregard the text shown? He wanted the data obtained with the method getPublic(); was in the format below:

4615855875618351712635877256976103420366861757278971514413945892411643339267
86295372175394984599678936024544037153778609141128039300731169643337680519305
  • 1

    PublicKey is an interface, and the type returned by KeyPairGenerator is an implementation detail that may vary according to the JVM used, the encryption providers you are using, etc. Vc might even see the type with pubKey.getClass(), make a cast for this type and see if it has specific methods to return this data, but it is not guaranteed (and the specific type can change according to the JVM, so it is better not to get stuck to these internal implementations). Maybe it’s easier to use substring as a result of pubKey.toString() and extract what you need...

  • I got it, thank you!

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.