The below one is not working for DSA
public class UseSFTPWithServerValidationKeyFile
{
/**
* Put a file, get it back as a copy and delete the local copy and the
* remote copy
*
* @param name original filename
* @param ftp reference to FTP client
*/
private static void putGetDelete(String srcFileName, String destFileName, FTPClientInterface ftp)
throws Exception
{
ftp.put(srcFileName, destFileName);
ftp.get(srcFileName + ".copy", destFileName);
//ftp.delete(destFileName);
}
public static void main(String[] args)
{
testServerPublicKeyFile(args);
}
public static void testServerPublicKeyFile(String[] args)
{
// we want remote host, user name and password
if (args.length < 6)
{
System.out.println("Usage: run remote-host username password publickeyfile sourcefile destfile key(DSA or RSA)");
System.out.println("publickeyfile = file containing the public key of the server in OpenSSH or SECSH format");
System.exit(1);
}
// extract command-line arguments
String host = args[0];
String username = args[1];
String password = args[2];
String keyfile = args[3];
String srcFileName = args[4];
String destFileName = args[5];
String key = args[6];
// set up logger so that we get some output
Logger log = Logger.getLogger(UseSFTPWithServerValidationKeyFile.class);
Logger.setLevel(Level.INFO);
try
{
// deleting local file if exists
File file = new File(srcFileName + ".copy");
log.info(" Deleted local copy : " + file.delete());
// create client
log.info("Creating SFTP client");
SSHFTPClient ftp = new SSHFTPClient();
// set remote host
ftp.setRemoteHost(host);
// now if your keyfile is a DSA public key, then you
// should disable all keypairs, and then enable DSA. This
// forces the server to send its DSA public key - if it sent
// an RSA public key and your keyfile is DSA, server validation
// will fail
if ("RSA".equalsIgnoreCase(key))
{
ftp.disableAllAlgorithms(SSHFTPAlgorithm.KEY_PAIR);
ftp.setAlgorithmEnabled(SSHFTPAlgorithm.KEY_RSA, true);
}
else if ("DSA".equalsIgnoreCase(key))
{
ftp.disableAllAlgorithms(SSHFTPAlgorithm.KEY_PAIR);
ftp.setAlgorithmEnabled(SSHFTPAlgorithm.KEY_DSA, true);
}
log.info("Setting user-name and password");
ftp.setAuthentication(username, password);
log.info("Loading server public-key from " + keyfile);
ftp.getValidator().addKnownHost(host, keyfile);
// connect to the server
log.info("Connecting to server " + host);
ftp.connect();
log.info("Setting transfer mode to ASCII");
ftp.setType(FTPTransferType.ASCII);
putGetDelete(srcFileName, destFileName, ftp);
log.info("Successfully transferred in ASCII mode");
// Shut down client
log.info("Quitting client");
ftp.quit();
log.info("Example complete");
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
=================================================
The below one is working for both DSA or RSA
public class UseSFTPServerAutoValidationPublicKey
{
public static void main(String[] args)
{
testServerPublicKeyFile(args);
}
public static void testServerPublicKeyFile(String[] args)
{
// we want remote host, user name and password
if (args.length < 6)
{
System.out.println("Usage: run remote-host username password publickeyfile sourcefile destfile key(DSA or RSA)");
System.out.println("publickeyfile = file containing the public key of the server in OpenSSH or SECSH format");
System.exit(1);
}
// extract command-line arguments
String host = args[0];
String username = args[1];
String password = args[2];
String keyfile = args[3];
String srcFileName = args[4];
String destFileName = args[5];
String key = args[6];
// set up logger so that we get some output
Logger log = Logger.getLogger(UseSFTPWithServerValidationKeyFile.class);
Logger.setLevel(Level.INFO);
try
{
// deleting local file if exists
File file = new File(srcFileName + ".copy");
log.info(" Deleted local copy : " + file.delete());
// create client
log.info("Creating SFTP client");
SSHFTPClient ftp = new SSHFTPClient();
// set remote host
ftp.setRemoteHost(host);
// now if your keyfile is a DSA public key, then you
// should disable all keypairs, and then enable DSA. This
// forces the server to send its DSA public key - if it sent
// an RSA public key and your keyfile is DSA, server validation
// will fail
if ("RSA".equalsIgnoreCase(key))
{
ftp.disableAllAlgorithms(SSHFTPAlgorithm.KEY_PAIR);
ftp.setAlgorithmEnabled(SSHFTPAlgorithm.KEY_RSA, true);
}
else if ("DSA".equalsIgnoreCase(key))
{
ftp.disableAllAlgorithms(SSHFTPAlgorithm.KEY_PAIR);
ftp.setAlgorithmEnabled(SSHFTPAlgorithm.KEY_DSA, true);
}
log.info("Setting user-name and password");
ftp.setAuthentication(username, password);
File f = new File(keyfile);
FileOutputStream fout = new FileOutputStream(f);
log.info("\n ------------- Open SSH Format " + SSHFTPPublicKey.OPENSSH_FORMAT);
SSHFTPClient.getHostPublicKey(host).write(fout, SSHFTPPublicKey.OPENSSH_FORMAT);
log.info(" Before Closing the Output Stream " + keyfile);
fout.close();
log.info("Loading server public-key from " + keyfile);
ftp.getValidator().addKnownHost(host, new FileInputStream(f));
// connect to the server
log.info("Connecti