Search code examples
javaputtyftps

How to use FTPSClient and keys in PuTTY format


I need to connect to remote server via FTPS (implicit or explicit). I successfully connected to server via FileZilla. Also I tested code to retrieve file from public ftp: ftp.mozilla.org Now I need the same code for ftps. I have problem with private key and KeyStore

String keyPath = "src/test/resources/keys/thekey.ppk";
        FTPSClient client = new FTPSClient();
        FileOutputStream fos = null;
        try {

            KeyStore ks = KeyStore.getInstance("JKS"); //
            FileInputStream fis = new FileInputStream(keyPath);
            ks.load(fis, "".toCharArray());//java.io.IOException: Invalid keystore format
            fis.close();
            KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory
                    .getDefaultAlgorithm());
            kmf.init(ks, "".toCharArray());

            System.out.println("connecting to 1.1.1.1...");
            client.setDefaultTimeout(10000);
            client.connect("1.1.1.1", 2190);
            System.out.println("loggin in...");
            System.out.println("login: " + client.login("login", "pass"));
            String remoteDir = "/pub/downloaded/";
            String remoteFileName = "testMsg.txt";
            String localFileName = "testMsg.local.txt";

            fos = new FileOutputStream(localFileName);

            System.out.println("retrieving file...");
            boolean isRetrieved = client.retrieveFile(remoteDir + remoteFileName, fos);
            System.out.print("File: " + remoteDir + remoteFileName + "; IsRetrieved: " + isRetrieved + "\n");

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (fos != null) {
                    fos.close();
                }
                client.disconnect();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

Keys were generated in PuTTY format. What else options can I put here KeyStore.getInstance("JKS"). If ommit the part with KeyStore than code reach line with client.retrieveFile and suspends for a long time. Need help to import keys, plz.


Solution

  • FTPS stands for FTP-over-SSL. SSL uses X.509 certificates for authentication (we omit other rarely used methods now). Putty is SSH/SFTP client (where SFTP stands for SSH File Transfer Protocol) and putty keys are SSH keys. Consequently you can't use SSH keys for SSL authentication.