How to get the related public-key object java.security.PublicKey
out of a private-key Object java.security.PrivateKey
in the RSA cryptosystem.
Java is able to create a public key by using the modulus and exponent:
RSAPublicKeySpec keySpec = new RSAPublicKeySpec(modulus, exponent);
kf.generatePublic(keySpec);
So we need to extract these values out of the private key:
KeyFactory kf = KeyFactory.getInstance("RSA");
RSAPrivateKeySpec priv = kf.getKeySpec(privateKey, RSAPrivateKeySpec.class);
The RSAPrivateKeySpec
-Object now contains the modulus we need, but the exponent is not the one we need for the public key.
For the public key the exponent is commonly at 65537: http://en.wikipedia.org/wiki/65537_(number)
Therefore we can now create the public key:
KeyFactory kf = KeyFactory.getInstance("RSA");
RSAPrivateKeySpec priv = kf.getKeySpec(privateKey, RSAPrivateKeySpec.class);
RSAPublicKeySpec keySpec = new RSAPublicKeySpec(priv.getModulus(), BigInteger.valueOf(65537));
PublicKey publicKey = kf.generatePublic(keySpec);