Java Retrieve the actual value of the public key from the KeyPair object
Asked Answered
V

4

5

I wanted to ask how to retrieve the actual values of the private and public keys from the KeyPair object because i need to export them and save in a database.

KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");
kpg.initialize(1024);
KeyPair keyPair = kpg.genKeyPair();

System.out.println("Public key " + keyPair.getPublic());
System.out.println("Private key " + keyPair.getPrivate());

The output is:

Public key Sun RSA public key, 1024 bits
  modulus: 105712092415375085805423498639048173422142354311030811647243014925610093650322108853068042919471115278002432342007597147610508132502035047888382465733153739247741208519707861808073276783311634229563965825609200080862631487160732889423591650215084096832366499080850540875321197564283324922935557797293830551071
  public exponent: 65537
Private key sun.security.rsa.RSAPrivateCrtKeyImpl@35e71
Vendace answered 6/11, 2013 at 17:20 Comment(0)
S
3

It's probably easiest to use keypair.getPublic.getEncoded() or keypair.getPrivate.getEncoded():

RSA private keys are encoded in PKCS#8 format, and public keys are encoded in X.509 format.

KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");
kpg.initialize(2048);
KeyPair keyPair = kpg.generateKeyPair();
PublicKey pub = keyPair.getPublic();
PrivateKey prv = keyPair.getPrivate();

byte[] pubBytes = pub.getEncoded();
byte[] prvBytes = prv.getEncoded();

// now save pubBytes or prvBytes

// to recover the key
KeyFactory kf = KeyFactory.getInstance("RSA");
PrivateKey prv_recovered = kf.generatePrivate(new PKCS8EncodedKeySpec(prvBytes));
PublicKey pub_recovered = kf.generatePublic(new X509EncodedKeySpec(pubBytes));

System.out.println("Private Key: \n" + prv_recovered.toString());
System.out.println("Public Key: \n" + pub_recovered.toString());
Sabbat answered 10/11, 2013 at 5:15 Comment(0)
E
4

You can simply downcast to RSAPrivateKey and RSAPublicKey. Once you've done that you can access the methods of these classes.

RSAPublicKey pubKey = (RSAPublicKey) keyPair.getPublic());
BigInteger modulus = pubKey.getModulus();

etc.

Epictetus answered 6/11, 2013 at 18:25 Comment(1)
This shows how to get the individual elements that the question showed in te printout of the key. For storage, look to the other answer. Private keys should however generally be stored in a KeyStore, rather than as plaintext value on disk or a DB.Epictetus
S
3

It's probably easiest to use keypair.getPublic.getEncoded() or keypair.getPrivate.getEncoded():

RSA private keys are encoded in PKCS#8 format, and public keys are encoded in X.509 format.

KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");
kpg.initialize(2048);
KeyPair keyPair = kpg.generateKeyPair();
PublicKey pub = keyPair.getPublic();
PrivateKey prv = keyPair.getPrivate();

byte[] pubBytes = pub.getEncoded();
byte[] prvBytes = prv.getEncoded();

// now save pubBytes or prvBytes

// to recover the key
KeyFactory kf = KeyFactory.getInstance("RSA");
PrivateKey prv_recovered = kf.generatePrivate(new PKCS8EncodedKeySpec(prvBytes));
PublicKey pub_recovered = kf.generatePublic(new X509EncodedKeySpec(pubBytes));

System.out.println("Private Key: \n" + prv_recovered.toString());
System.out.println("Public Key: \n" + pub_recovered.toString());
Sabbat answered 10/11, 2013 at 5:15 Comment(0)
K
1

Have you looked into the docs?

Try the following :

getModulus

getPublicExponent

getPrivateExponent

They all return BigInteger

than can in turn be stored.

Also see if the raw encoded format is of help.

Knoll answered 6/11, 2013 at 17:50 Comment(1)
This answer shows the methods, but it completely skips how to get to the RSA classes, and it doesn't specify the format "raw encoded format" - the documentation talks about primary encoding format, which is a different thing.Epictetus
I
1

Continuing the code from original question, here is the solution to print out the content of the RSA Key itself:

KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");
kpg.initialize(1024);
KeyPair keyPair = kpg.genKeyPair();

System.out.println("Public key " + keyPair.getPublic());
System.out.println("Private key " + keyPair.getPrivate());

//to print actual content

Base64.Encoder encoder = Base64.getEncoder();

System.out.println("Private key content: " + encoder.encodeToString(keyPair.getPrivate().getEncoded()));
System.out.println("Public key content: " + encoder.encodeToString(keyPair.getPublic().getEncoded()));

Hopefully this helps more digital army in em war! Peace :D

Infuriate answered 27/10, 2022 at 6:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.