title: Get a PrivateKey from a RSA .pem file
body: <p>Given this <code>.pem</code> file (generated with openssl and encrypted with a password):</p>
  
  <pre><code>-----BEGIN RSA PRIVATE KEY-----
  Proc-Type: 4,ENCRYPTED
  DEK-Info: DES-EDE3-CBC,AC009672952033EB
  
  2wegzxf3MtncXS1CY3c.....
  ....
  ....
  -----END RSA PRIVATE KEY-----
  </code></pre>
  
  <p>How do I get a <code>PrivateKey</code> object in Java? I wrote the following code but I cannot find the right way to get a <code>KeySpec</code>:</p>
  
  <pre><code>PrivateKey readFromPem(File keyFile, String password){
      PemReader r = new PemReader(new InputStreamReader(new FileInputStream(keyFile)));
      PemObject pemObject = r.readPemObject();
      byte[] encodedKey = pemObject.getContent();
  
      KeySpec keySpec = ???? // how to get this?
  
      KeyFactory keyFactory = KeyFactory.getInstance("RSA");
      PrivateKey key = keyFactory.generatePrivate(keySpec);
      return key;
  }
  </code></pre>
  
  <p>I guess I should build a <code>RSAPrivateKeySpec</code>, but I don't know how. I tried the method from <a href="https://stackoverflow.com/a/20748064/3202887">this answer</a> and <a href="https://stackoverflow.com/a/30929175/3202887">this other answer</a>, but they both result in errors when parsing the byte array.</p>