File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Other JSE/JEE APIs and the fly likes Importing RSA public key into a keystone? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Other JSE/JEE APIs
Bookmark "Importing RSA public key into a keystone?" Watch "Importing RSA public key into a keystone?" New topic
Author

Importing RSA public key into a keystone?

David Tong
Greenhorn

Joined: Dec 09, 2013
Posts: 6
    
    2
I'm trying to establish a FTPS connection using Apache FTPSClient. It works fine from the command line.
The target site has my public key - I don't need a password or passphrase.

java.security.KeyStoreException: key is not encoded as EncryptedPrivateKeyInfo
at sun.security.provider.JavaKeyStore.engineSetKeyEntry(JavaKeyStore.java:311)
at sun.security.provider.JavaKeyStore$JKS.engineSetKeyEntry(JavaKeyStore.java:55)
at java.security.KeyStore.setKeyEntry(KeyStore.java:943)

I think I may be going about this the wrong way, but I can't seem to find the right path. Any help would be appreciated.


package junk;

import java.io.PrintWriter;
import java.security.KeyStore;
import java.security.NoSuchAlgorithmException;

import javax.net.ssl.KeyManager;
import javax.net.ssl.KeyManagerFactory;

import org.apache.commons.net.PrintCommandListener;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPReply;
import org.apache.commons.net.ftp.FTPSClient;

public final class FTPSexample {

public static final void main(String[] args) throws NoSuchAlgorithmException {

String server, username;
String protocol = "SSL"; // SSL/TLS
FTPSClient client = null;

server = "ftp.server.com";
username = "user";

String publicKey = "ssh-rsa AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA== foo@127.0.0.1";
String privateKey = "-----BEGIN RSA PRIVATE KEY-----\n"+
"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n"+
"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n"+
"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n"+
"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n"+
"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n"+
"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n"+
"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n"+
"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n"+
"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n"+
"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n"+
"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n"+
"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n"+
"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA==\n"+
"-----END RSA PRIVATE KEY-----\n";

try {
client = new FTPSClient(protocol);
KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());
ks.load(null, null);
ks.setKeyEntry("key", privateKey.getBytes(), null);
KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance("JKS");
keyManagerFactory.init(ks, null);
KeyManager[] keyManagers = keyManagerFactory.getKeyManagers();
client.setKeyManager(keyManagers[0]);
client.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out)));

client.connect(server);
int reply = client.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
client.disconnect();
System.err.println("FTP server refused connection.");
System.exit(1);
}
if (!client.login(username, "")) {
System.out.println("Login failed");
} else {
client.setFileType(FTP.BINARY_FILE_TYPE);
client.enterLocalPassiveMode();
System.out.println("Login succeeded");
}
} catch (Exception e1) {
e1.printStackTrace();
}
}
}
David Tong
Greenhorn

Joined: Dec 09, 2013
Posts: 6
    
    2
Never mind, JSch worked and was simpler.
Richard Tookey
Ranch Hand

Joined: Aug 27, 2012
Posts: 1110
    
  10

David Tong wrote:Never mind, JSch worked and was simpler.


JSch talks the secure shell protocol (SSH) using 'scp' or 'sftp' but FTPS talks FTP over SSL. These are two very different protocols. Presumably when you say "It works fine from the command line." you mean that you were talking to an 'ssh' server such as 'openssh' using an 'scp' or ''sftp' client.

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Importing RSA public key into a keystone?