title: Programatically convert a JKS to PEM
body: <p>I need to convert a JKS (password protected) in to a .PEM containing the KeyPairs (or selected alias) for other services to use.</p>
  
  <p>So far, I have written this:</p>
  
  <pre><code> @Transactional
  public ResponseEntity&lt;String&gt; getPublicKeyFromJKS(long jksid) {
      try {
          //1. Lift the entity
          JKSFile jksFile = jksFileRepo.getOne(jksid);
  
          //2. Get the Object from S3
          S3Object object = getS3ObjectService.getS3Object(jksFile.getS3ObjectKey());
          System.out.println(object.getObjectContent().getHttpRequest());
          InputStream in = object.getObjectContent();
  
          //3. Make KeyStore...
          KeyStore keyStore = KeyStore.getInstance("JKS");
          keyStore.load(in, rsaConfig.getKeystorePassword().toCharArray());
  
          //4. Get keys to PEM formatted file.
          PrivateKey privateKey = (PrivateKey) keyStore.getKey("access", rsaConfig.getKeystorePassword().toCharArray());
          Certificate certificate = keyStore.getCertificate("access");
          PublicKey publicKey = certificate.getPublicKey();
  
          KeyPair keyPair = new KeyPair(publicKey, privateKey);
  
  
          System.out.println(keyPair.getPrivate().toString());
          System.out.println(keyPair.getPublic().toString());
  </code></pre>
  
  <p>I am pulling the requested JKS from my amazon S3 bucket and reading the Private and Public key without issue...but I would now like to be able to put them in to a PEM formatted keystore (for use by another service).</p>
  
  <p>Thanks for any suggestions! I want this programatically...not using the java command line keytool.</p>