@Test public void testEncryptedKey() { KeyPair keyPair = KeyTestUtil.getKeyPairFromProperties("withpass", "id_rsa"); SSHKey sshKey = new SSHKey(KeyFormat.SSH_RSA, keyPair); try { Key bcKey = PEMUtil.readKey(KeyTestUtil.getPrivateKeyAsFile("withpass", "id_rsa"), "dummydummy".toCharArray()); assertEquals("fingerprints should match", sshKey.getFingerprint(), ((FingerprintableKey) bcKey).getFingerprint()); } catch (IOException e) { FailUtil.sprintFail(e); } }
public static Key readKey(InputStream is, final char[] passphrase) throws IOException { JcaPEMKeyConverter converter = new JcaPEMKeyConverter(); PEMParser parser = null; KeyPair keyPair = null; try { parser = new PEMParser(new InputStreamReader(is)); Object o = parser.readObject(); if (o instanceof PEMEncryptedKeyPair) { PEMEncryptedKeyPair _encPair = (PEMEncryptedKeyPair) o; PEMDecryptorProvider decryptionProv = new JcePEMDecryptorProviderBuilder().build(passphrase); keyPair = converter.getKeyPair(_encPair.decryptKeyPair(decryptionProv)); } else if (o instanceof PEMKeyPair) { keyPair = converter.getKeyPair((PEMKeyPair) o); } if (keyPair != null) { if (keyPair.getPrivate() instanceof RSAPrivateKey || keyPair.getPublic() instanceof RSAPublicKey) { return new SSHKey(KeyFormat.SSH_RSA, keyPair); } else if (keyPair.getPrivate() instanceof DSAPrivateKey || keyPair.getPublic() instanceof DSAPublicKey) { return new SSHKey(KeyFormat.SSH_DSS, keyPair); } } return null; } finally { if (parser != null) { try { parser.close(); } catch (IOException ignored) {} } } }
PEMUtil { public static Key readKey(InputStream is, final char[] passphrase) throws IOException { JcaPEMKeyConverter converter = new JcaPEMKeyConverter(); PEMParser parser = null; KeyPair keyPair = null; try { parser = new PEMParser(new InputStreamReader(is)); Object o = parser.readObject(); if (o instanceof PEMEncryptedKeyPair) { PEMEncryptedKeyPair _encPair = (PEMEncryptedKeyPair) o; PEMDecryptorProvider decryptionProv = new JcePEMDecryptorProviderBuilder().build(passphrase); keyPair = converter.getKeyPair(_encPair.decryptKeyPair(decryptionProv)); } else if (o instanceof PEMKeyPair) { keyPair = converter.getKeyPair((PEMKeyPair) o); } if (keyPair != null) { if (keyPair.getPrivate() instanceof RSAPrivateKey || keyPair.getPublic() instanceof RSAPublicKey) { return new SSHKey(KeyFormat.SSH_RSA, keyPair); } else if (keyPair.getPrivate() instanceof DSAPrivateKey || keyPair.getPublic() instanceof DSAPublicKey) { return new SSHKey(KeyFormat.SSH_DSS, keyPair); } } return null; } finally { if (parser != null) { try { parser.close(); } catch (IOException ignored) {} } } } }
PEMUtil { public static Key readKey(InputStream is, final char[] passphrase) throws IOException { JcaPEMKeyConverter converter = new JcaPEMKeyConverter(); PEMParser parser = null; KeyPair keyPair = null; try { parser = new PEMParser(new InputStreamReader(is)); Object o = parser.readObject(); if (o instanceof PEMEncryptedKeyPair) { PEMEncryptedKeyPair _encPair = (PEMEncryptedKeyPair) o; PEMDecryptorProvider decryptionProv = new JcePEMDecryptorProviderBuilder().build(passphrase); keyPair = converter.getKeyPair(_encPair.decryptKeyPair(decryptionProv)); } else if (o instanceof PEMKeyPair) { keyPair = converter.getKeyPair((PEMKeyPair) o); } if (keyPair != null) { if (keyPair.getPrivate() instanceof RSAPrivateKey || keyPair.getPublic() instanceof RSAPublicKey) { return new SSHKey(KeyFormat.SSH_RSA, keyPair); } else if (keyPair.getPrivate() instanceof DSAPrivateKey || keyPair.getPublic() instanceof DSAPublicKey) { return new SSHKey(KeyFormat.SSH_DSS, keyPair); } } return null; } finally { if (parser != null) { try { parser.close(); } catch (IOException ignored) {} } } }  }
PEMUtil { public static Key readKey(InputStream is, final char[] passphrase) throws IOException { JcaPEMKeyConverter converter = new JcaPEMKeyConverter(); PEMParser parser = null; KeyPair keyPair = null; try { parser = new PEMParser(new InputStreamReader(is)); Object o = parser.readObject(); if (o instanceof PEMEncryptedKeyPair) { PEMEncryptedKeyPair _encPair = (PEMEncryptedKeyPair) o; PEMDecryptorProvider decryptionProv = new JcePEMDecryptorProviderBuilder().build(passphrase); keyPair = converter.getKeyPair(_encPair.decryptKeyPair(decryptionProv)); } else if (o instanceof PEMKeyPair) { keyPair = converter.getKeyPair((PEMKeyPair) o); } if (keyPair != null) { if (keyPair.getPrivate() instanceof RSAPrivateKey || keyPair.getPublic() instanceof RSAPublicKey) { return new SSHKey(KeyFormat.SSH_RSA, keyPair); } else if (keyPair.getPrivate() instanceof DSAPrivateKey || keyPair.getPublic() instanceof DSAPublicKey) { return new SSHKey(KeyFormat.SSH_DSS, keyPair); } } return null; } finally { if (parser != null) { try { parser.close(); } catch (IOException ignored) {} } } }  static Key readKey(InputStream is, final char[] passphrase); static Key readKey(File privateKeyFile, final char[] passphrase); static Key readKey(byte[] keyBytes, final char[] passphrase); }
PEMUtil { public static Key readKey(InputStream is, final char[] passphrase) throws IOException { JcaPEMKeyConverter converter = new JcaPEMKeyConverter(); PEMParser parser = null; KeyPair keyPair = null; try { parser = new PEMParser(new InputStreamReader(is)); Object o = parser.readObject(); if (o instanceof PEMEncryptedKeyPair) { PEMEncryptedKeyPair _encPair = (PEMEncryptedKeyPair) o; PEMDecryptorProvider decryptionProv = new JcePEMDecryptorProviderBuilder().build(passphrase); keyPair = converter.getKeyPair(_encPair.decryptKeyPair(decryptionProv)); } else if (o instanceof PEMKeyPair) { keyPair = converter.getKeyPair((PEMKeyPair) o); } if (keyPair != null) { if (keyPair.getPrivate() instanceof RSAPrivateKey || keyPair.getPublic() instanceof RSAPublicKey) { return new SSHKey(KeyFormat.SSH_RSA, keyPair); } else if (keyPair.getPrivate() instanceof DSAPrivateKey || keyPair.getPublic() instanceof DSAPublicKey) { return new SSHKey(KeyFormat.SSH_DSS, keyPair); } } return null; } finally { if (parser != null) { try { parser.close(); } catch (IOException ignored) {} } } }  static Key readKey(InputStream is, final char[] passphrase); static Key readKey(File privateKeyFile, final char[] passphrase); static Key readKey(byte[] keyBytes, final char[] passphrase);  }