title: How to read .pem file to get private and public key
body: <p>I am writing a small piece of code which reads public and private key stored in .pem file. I am using the following commands to generate the keys.  </p>
  
  <p>Below command to generate pair of key.  </p>
  
  <pre><code>   $openssl genrsa -out mykey.pem 2048
  </code></pre>
  
  <p>This command to generate the private key</p>
  
  <pre><code>$openssl pkcs8 -topk8 -inform PEM -outform PEM -in mykey.pem \
      -out private_key.pem -nocrypt
  </code></pre>
  
  <p>and this command to get the public key.  </p>
  
  <pre><code>$ openssl rsa -in mykey.pem -pubout -outform DER -out public_key.der
  </code></pre>
  
  <p>I have written two methods which reads the private key and public key respectively.</p>
  
  <pre><code>   public  PrivateKey getPemPrivateKey(String filename, String algorithm) throws Exception {
        File f = new File(filename);
        FileInputStream fis = new FileInputStream(f);
        DataInputStream dis = new DataInputStream(fis);
        byte[] keyBytes = new byte[(int) f.length()];
        dis.readFully(keyBytes);
        dis.close();
  
        String temp = new String(keyBytes);
        String privKeyPEM = temp.replace("-----BEGIN PRIVATE KEY-----\n", "");
        privKeyPEM = privKeyPEM.replace("-----END PRIVATE KEY-----", "");
        //System.out.println("Private key\n"+privKeyPEM);
  
        Base64 b64 = new Base64();
        byte [] decoded = b64.decode(privKeyPEM);
  
        PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(decoded);
        KeyFactory kf = KeyFactory.getInstance(algorithm);
        return kf.generatePrivate(spec);
        }
  
     public  PublicKey getPemPublicKey(String filename, String algorithm) throws Exception {
        File f = new File(filename);
        FileInputStream fis = new FileInputStream(f);
        DataInputStream dis = new DataInputStream(fis);
        byte[] keyBytes = new byte[(int) f.length()];
        dis.readFully(keyBytes);
        dis.close();
  
        String temp = new String(keyBytes);
        String publicKeyPEM = temp.replace("-----BEGIN PUBLIC KEY-----\n", "");
        publicKeyPEM = publicKeyPEM.replace("-----END PUBLIC KEY-----", "");
  
  
        Base64 b64 = new Base64();
        byte [] decoded = b64.decode(publicKeyPEM);
  
        X509EncodedKeySpec spec =
              new X509EncodedKeySpec(decoded);
        KeyFactory kf = KeyFactory.getInstance(algorithm);
        return kf.generatePublic(spec);
        }
  </code></pre>
  
  <p>I feel like this is a naive way of doing it. I couldn't get any better way of doing it over internet. Can anyone suggest me what is the best way of writing the same code to handle the generic cases. I don' want to use any kind of third party library.<br>
  I have very basic knowledge of singing/encrypting and hardly use any java security APIs. So if I am not making sense somewhere then please point out.</p>