简体   繁体   中英

Java: How to generate KeyPair form private key, using only pure Java.Security without BouncyCastle?

Java: How to generate KeyPair form private key, using only pure Java.Security without third party provider like BouncyCastle ?

I am using this code to generate KeyPair :

KeyPairGenerator keyGen = KeyPairGenerator.getInstance("EC");
ECGenParameterSpec ecSpec = new ECGenParameterSpec("secp256k1");
keyGen.initialize(ecSpec, new SecureRandom());
KeyPair keyPair = keyGen.generateKeyPair();

But what if I want to generate KeyPair from private key I already know? I tried to find solution on web, but without success.

The public key in Java is just s * G where s is the private key (a vector) and G is the base point of the elliptic curve. However, there is no way to perform EC point calculations in Java, so you're stuck.

The one thing you can do is to lift the EC point calculations from an external library and use that. Well, OK, or you can perform the calculations yourself. Java does give you the option to retrieve the BigInteger values of s and the x and y values of G after all.

There is a roundabout way of doing the calculation by using ECDH (which basically is point multiplication, after all), but that's such a long workaround with so many hacks that performing the calculations yourself is much easier and cleaner.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM