Java recover the public key by only having the privatekey possible? [closed]

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


closed as off-topic by Geoffroy Couteau, e-sushi♦Jan 30 at 17:15

This question appears to be off-topic. The users who voted to close gave this specific reason:

• "Programming questions are off-topic even if you are writing or debugging cryptographic code. Unless your question is specifically about how the cryptographic algorithm, protocol or side-channel (mitigation) works, you should look into asking on Stack Overflow instead." – e-sushi
If this question can be reworded to fit the rules in the help center, please edit the question.

• Your question makes very little sense. a) in your stackoverflow post you link to a question talking about RSA b) here you post a piece of code that generates an elliptic curve key-pair (including private and public key!) and c) the comments inside the code also make little sense as there is no 256-byte = 2048 bit elliptic curve. And of course, we won't help you write code for this, but we can help you with questions about the underlying cryptography. – SEJPM Jan 30 at 14:03

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.

• put on hold as off-topic by Geoffroy Couteau, e-sushi♦ 1 hour ago This question appears to be off-topic for this site. While what’s on- and off-topic is not always intuitive, you can learn more about it by reading the help center. The users who voted to close gave this specific reason: "Programming questions are off-topic even if you are writing or debugging cryptographic code. Unless your question is specifically about how the cryptographic algorithm, protocol or side-channel (mitigation) works, you should look into asking on Stack Overflow instead." – e-sushi – Ahmet Caballero Jan 30 at 18:59
• I posted this problem on stackoverflow as you can see on the source link i posted. – Ahmet Caballero Jan 30 at 19:01
• There is 1 user added comment – Ahmet Caballero Jan 30 at 19:01
• You should ask on the Crypto stackexchange whether such an operation is even feasible, before trying to do it in code. – Henrik 5 hours ago – Ahmet Caballero Jan 30 at 19:01
• @AhmetCaballero the problem with your question (additionally to the points below your question here by me) is that it essentially goes like "Here's a link to an stackoverflow question, how can I program this and then some (unrelated?) piece of code". As it stands your question is "how can I program this" which is a programming question and not a crypto question which would be "with X cryptosystem, can I derive the public key from the private key" this would be a perfectly fine question (and if you edit, the question probably will be opened again). – SEJPM Jan 30 at 19:11