I am looking into https://github.com/str4d/ed25519-java library as well as some other libraries like BouncyCastle, but I am so far unsuccessful in generating a public key out from a private key. I am not interested in generating a key pair because I already have a private key and am interested in generating the corresponding public key only.
How derive ed25519 (eddsa) public key from private key using Java
4.8k Views Asked by troy_achilies At
2
There are 2 best solutions below
0
On
If you want to avoid or can't use Bouncy Castle for some reason, then use Sun Crypto Service Provider instead (for Java 15 and later). Something like the following should work, where params is either NamedParameterSpec.Ed25519 or NamedParameterSpec.Ed448
public PublicKey getEdDSAPublicKeyFromPrivateKey(NamedParameterSpec params, byte[] privateKey) {
EdDSAOperations eddsaOperations = new EdDSAOperations(EdDSAParameters.get(InvalidAlgorithmParameterException::new, params));
EdECPoint edecPublicKeyPoint = eddsaOperations.computePublic(privateKey);
// Use KeyFactory to convert public point to public key
KeyFactory kf = KeyFactory.getInstance("EdDSA");
EdECPublicKeySpec pubSpec = new EdECPublicKeySpec(params, edecPublicKeyPoint);
return kf.generatePublic(pubSpec);
}
Using Bouncy Castle (BC) as crypto provider library is always a good choice, and they have a "build in" support for deriving the public key from an existing private key. Please keep in mind that BC does not work with a Private or Public key but instead with Ed25519PrivateKeyParameters and Ed25519PublicKeyParameters but as you can see it's very easy to get them from the encoded keys.
The full program is a little bit longer to prove that the rebuild public key is been able to verify a signature generated by its corresponding private key. Therefore, the main part of the program is to generate and verify a ED25519 signature.
Those two lines are doing what you are asking:
The following lines are verifying the signature with the rebuild public key successfully.
output:
Security warning: the code does not have any exception handling and is for educational purpose only.
code: