• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Importing RSA public key into a keystone?

 
David Tong
Greenhorn
Posts: 6
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Never mind, JSch worked and was simpler.
 
Richard Tookey
Bartender
Posts: 1166
17
Java Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic