Java recover the public key by only having the privatekey possible? Source:   

https://stackoverflow.com/questions/48521925/java-recover-the-public-key-by-only-having-the-privatekey-possible

How to solve this problem programmatically 

try {
    KeyPairGenerator keyGen = KeyPairGenerator.getInstance("ECDSA", "BC");
    SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
    ECGenParameterSpec ecSpec = new ECGenParameterSpec("prime192v1");
    // Initialize the key generator and generate a KeyPair
    keyGen.initialize(ecSpec, random); // 256 bytes provides an
                                        // acceptable security level
    KeyPair keyPair = keyGen.generateKeyPair();
    // Set the public and private keys from the keyPair
    privateKey = keyPair.getPrivate();
    publicKey = keyPair.getPublic();


    System.out.println("Private and public keys:");
    System.out.println("PRIVATE: " + StringUtil.getStringFromKey(this.privateKey));
    System.out.println("PUBLIC: " + StringUtil.getStringFromKey(this.publicKey));

} catch (Exception e) {
    throw new RuntimeException(e);
}
OK, I will take a stab in the dark and ask the cryptographic question at hand.


  Given a private key, can we recover the public key?


The answer is it depends, but usually you can.

If we are talking about RSA, then the private key sometimes is depicted as "only" being $d$, which isn't enough to recover $e$ and $n$ (even though $e$ is usually fixed). However practically, all decent forms of encoding and libraries should store auxiliary information as their private key, which usually includes $d,n$ and some other values and from $d$ and $n$ we can easily derive, guess or brute force $e$ (see this Q/A on the site for more information).

If we are talking about elliptic curves, then the private key is some integer $a$. Again this integer alone is not enough to recover the public key, but as soon as we also learn which curve is used (which is usually stored in the private key format as well), we can recover the public key the same way as we do during key generation, using scalar multiplication on the curve, i.e. $P=[a]G$.

Now on your StackOverflow post, people have commented "nope" or similarly unhelpful statements. These are false (as you can see above).  However, they apply for the converse statement. That is, given a public key, you can't (easily) recover the private key.