/** * Parses the keyFile hiding any Exceptions that might occur. * @param keyFile the file. * @return the "parsed" file. */ private static SshPrivateKeyFile parsePrivateKeyFile(File keyFile) { try { return SshPrivateKeyFile.parse(keyFile); } catch (Exception ex) { return null; } }
public static void main(String args []) throws Exception { SshKeyPair keypair = SshKeyPairFactory.newInstance("ssh-dss"); keypair.generate(1024); SshPrivateKey priv = keypair.getPrivateKey(); SshPublicKey pub = keypair.getPublicKey(); SshPrivateKeyFile privfile = SshPrivateKeyFile.create(priv, null, SshPrivateKeyFormatFactory.newInstance(SshPrivateKeyFormatFactory.getDefaultFormatType())); FileOutputStream fout = new FileOutputStream("priv.skk"); fout.write(privfile.getBytes()); fout.close(); SshPublicKeyFile pubfile = SshPublicKeyFile.create(pub, SshPublicKeyFormatFactory.newInstance(SshPublicKeyFormatFactory.getDefaultFormatType())); fout = new FileOutputStream("pub.pkk"); fout.write(pubfile.getBytes()); fout.close(); } }
/** * Checks to see if the passPhrase is valid for the private key file. * @param keyFilePath the private key file. * @param passPhrase the password for the file. * @return true if it is valid. */ public static boolean checkPassPhrase(File keyFilePath, String passPhrase) { SshPrivateKeyFile keyFile = parsePrivateKeyFile(keyFilePath); if (keyFile != null) { try { keyFile.toPrivateKey(passPhrase); } catch (InvalidSshKeyException e) { return false; } return true; } else { return false; } } }
keyStream.close(); baos.close(); SshPrivateKeyFile pkf = SshPrivateKeyFile.parse(keyBytes); SshPrivateKey key = pkf.toPrivateKey(null); serverKeys.put(key.getAlgorithmName(), key); } else { log.warn("Private key file '" + privateKey +
/** * Parses the keyFile hiding any Exceptions that might occur. * @param keyFile the file. * @return the "parsed" file. */ private static SshPrivateKeyFile parsePrivateKeyFile(File keyFile) { try { return SshPrivateKeyFile.parse(keyFile); } catch (Exception ex) { return null; } }
/** * Checks to see if the passPhrase is valid for the private key file. * @param keyFilePath the private key file. * @param passPhrase the password for the file. * @return true if it is valid. */ public static boolean checkPassPhrase(File keyFilePath, String passPhrase) { SshPrivateKeyFile keyFile = parsePrivateKeyFile(keyFilePath); if (keyFile != null) { try { keyFile.toPrivateKey(passPhrase); } catch (InvalidSshKeyException e) { return false; } return true; } else { return false; } } }