title: Java Retrieve the actual value of the public key from the KeyPair object
body: <p>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.</p>
  
  <pre><code>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());
  </code></pre>
  
  <p>The output is: </p>
  
  <pre><code>Public key Sun RSA public key, 1024 bits
    modulus: 105712092415375085805423498639048173422142354311030811647243014925610093650322108853068042919471115278002432342007597147610508132502035047888382465733153739247741208519707861808073276783311634229563965825609200080862631487160732889423591650215084096832366499080850540875321197564283324922935557797293830551071
    public exponent: 65537
  Private key sun.security.rsa.RSAPrivateCrtKeyImpl@35e71
  </code></pre>