Source:
public static KeyPair readPem(InputStream input, final String passphrase) { byte[] cache = Streams.readFully(input); input = new ByteArrayInputStream(cache); try { PEMParser pemParser = new PEMParser(new InputStreamReader(input)); Object object = pemParser.readObject(); pemParser.close(); JcaPEMKeyConverter converter = new JcaPEMKeyConverter().setProvider("BC"); KeyPair kp = null; if (object==null) { throw new IllegalStateException("PEM parsing failed: missing or invalid data"); } else if (object instanceof PEMEncryptedKeyPair) { if (passphrase==null) throw new PassphraseProblem("passphrase required"); try { PEMDecryptorProvider decProv = new JcePEMDecryptorProviderBuilder().build(passphrase.toCharArray()); kp = converter.getKeyPair(((PEMEncryptedKeyPair) object).decryptKeyPair(decProv)); } catch (Exception e) { Exceptions.propagateIfFatal(e); throw new PassphraseProblem("wrong passphrase"); } } else if (object instanceof PEMKeyPair) { kp = converter.getKeyPair((PEMKeyPair) object); } else if (object instanceof PrivateKeyInfo) { PrivateKey privKey = converter.getPrivateKey((PrivateKeyInfo) object); kp = new KeyPair(null, privKey); } else { throw new IllegalStateException("PEM parser support missing for: "+object); } return kp; } catch (Exception e) { Exceptions.propagateIfFatal(e); input = new ByteArrayInputStream(cache); try { Security.addProvider(new BouncyCastleProvider()); PEMReader pr = new PEMReader(new InputStreamReader(input), new PasswordFinder() { public char[] getPassword() { return passphrase!=null ? passphrase.toCharArray() : new char[0]; } }); KeyPair result = (KeyPair) pr.readObject(); pr.close(); if (result==null) throw Exceptions.propagate(e); log.warn("PEMParser failed when PEMReader succeeded, with "+result+"; had: "+e); return result; } catch (Exception e2) { Exceptions.propagateIfFatal(e2); throw Exceptions.propagate(e); } } }

Target:
public static KeyPair readPem(InputStream input, final String passphrase) { byte[] cache = Streams.readFully(input); input = new ByteArrayInputStream(cache); try { PEMParser pemParser = new PEMParser(new InputStreamReader(input)); Object object = pemParser.readObject(); pemParser.close(); JcaPEMKeyConverter converter = new JcaPEMKeyConverter().setProvider("BC"); KeyPair kp = null; if (object==null) { throw new IllegalStateException("PEM parsing failed: missing or invalid data"); } else if (object instanceof PEMEncryptedKeyPair) { if (passphrase==null) throw new PassphraseProblem("passphrase required"); try { PEMDecryptorProvider decProv = new JcePEMDecryptorProviderBuilder().build(passphrase.toCharArray()); kp = converter.getKeyPair(((PEMEncryptedKeyPair) object).decryptKeyPair(decProv)); } catch (Exception e) { Exceptions.propagateIfFatal(e); throw new PassphraseProblem("wrong passphrase", e); } } else if (object instanceof PEMKeyPair) { kp = converter.getKeyPair((PEMKeyPair) object); } else if (object instanceof PrivateKeyInfo) { PrivateKey privKey = converter.getPrivateKey((PrivateKeyInfo) object); kp = new KeyPair(null, privKey); } else { throw new IllegalStateException("PEM parser support missing for: "+object); } return kp; } catch (Exception e) { Exceptions.propagateIfFatal(e); input = new ByteArrayInputStream(cache); try { Security.addProvider(new BouncyCastleProvider()); @SuppressWarnings("deprecation") org.bouncycastle.openssl.PEMReader pr = new org.bouncycastle.openssl.PEMReader(new InputStreamReader(input), new PasswordFinder() { public char[] getPassword() { return passphrase!=null ? passphrase.toCharArray() : new char[0]; } }); @SuppressWarnings("deprecation") KeyPair result = (KeyPair) pr.readObject(); pr.close(); if (result==null) throw Exceptions.propagate(e); log.warn("PEMParser failed when deprecated PEMReader succeeded, with "+result+"; had: "+e); return result; } catch (Exception e2) { Exceptions.propagateIfFatal(e2); throw Exceptions.propagate(e); } } }