'Connection with server using private key Jsch
I am able to connect with a remote server using the myKey.pem . Now I want to store the content of this pem key into a String privateKey and then connect using Jsch.
JSch jsch = new JSch();
Session session;
session = jsch.getSession(username, host, 22);
session.setConfig("StrictHostKeyChecking", "no");
if (StringUtils.hasText(password)) {
session.setPassword(password);
} else {
jsch.addIdentity(privateKey);
}
session.connect();
return session;
whereas the myKey.pem have the data like following
-----BEGIN RSA PRIVATE KEY-----
MIIEpQIBAAKCAQEA3Tz2mr7SZiAMfQyuvBjM9Oi..Z1BjP5CE/Wm/Rr500P
RK+Lh9x5eJPo5CAZ3/ANBE0sTK0ZsDGMak2m1g7..3VHqIxFTz0Ta1d+NAj
wnLe4nOb7/eEJbDPkk05ShhBrJGBKKxb8n104o/..PdzbFMIyNjJzBM2o5y
5A13wiLitEO7nco2WfyYkQzaxCw0AwzlkVHiIyC..71pSzkv6sv+4IDMbT/
XpCo8L6wTarzrywnQsh+etLD6FtTjYbbrvZ8RQM..Hg2qxraAV++HNBYmNW
s0duEdjUbJK+ZarypXI9TtnS4o1Ckj7POfljiQI..IBAFyidxtqRQyv5KrD
kbJ+q+rsJxQlaipn2M4lGuQJEfIxELFDyd3XpxP..Un/82NZNXlPmRIopXs
2T91jiLZEUKQw+n73j26adTbteuEaPGSrTZxBLR..yssO0wWomUyILqVeti
6AkL0NJAuKcucHGqWVgUIa4g1haE0ilcm6dWUDo..fd+PpzdCJf1s4NdUWK
YV2GJcutGQb+jqT5DTUqAgST7N8M28rwjK6nVMI..BUpP0xpPnuYDyPOw6x
4hBt8DZQYyduzIXBXRBKNiNdv8fum68/5klHxp6..4HRkMUL958UVeljUsT
BFQlO9UCgYEA/VqzXVzlz8K36VSTMPEhB5zBATV..PRiXtYK1YpYV4/jSUj
vvT4hP8uoYNC+BlEMi98LtnxZIh0V4rqHDsScAq..VyeSLH0loKMZgpwFEm
bEIDnEOD0nKrfT/9K9sPYgvB43wsLEtUujaYw3W..Liy0WKmB8CgYEA34xn
1QlOOhHBn9Z8qYjoDYhvcj+a89tD9eMPhesfQFw..rsfGcXIonFmWdVygbe
6Doihc+GIYIq/QP4jgMksE1ADvczJSke92ZfE2i..fitBpQERNJO0BlabfP
ALs5NssKNmLkWS2U2BHCbv4DzDXwiQB37KPOL1c..kBHfF2/htIs20d1UVL
+PK+aXKwguI6bxLGZ3of0UH+mGsSl0mkp7kYZCm..OTQtfeRqP8rDSC7DgA
kHc5ajYqh04AzNFaxjRo+M3IGICUaOdKnXd0Fda..QwfoaX4QlRTgLqb7AN
ZTzM9WbmnYoXrx17kZlT3lsCgYEAm757XI3WJVj..WoLj1+v48WyoxZpcai
uv9bT4Cj+lXRS+gdKHK+SH7J3x2CRHVS+WH/SVC..DxuybvebDoT0TkKiCj
BWQaGzCaJqZa+POHK0klvS+9ln0/6k539p95tfX..X4TCzbVG6+gJiX0ysz
Yfehn5MCgYEAkMiKuWHCsVyCab3RUf6XA9gd3qY..fCTIGtS1tR5PgFIV+G
engiVoWc/hkj8SBHZz1n1xLN7KDf8ySU06MDggB..hJ+gXJKy+gf3mF5Kmj
DtkpjGHQzPF6vOe907y5NQLvVFGXUq/FIJZxB8k..fJdHEm2M4=
-----END RSA PRIVATE KEY-----
But I am facing the excpetion java.io.FileNotFoundException: -----BEGIN RSA PRIVATE KEY--
Anyone please guide how I can do that?
Solution 1:[1]
Now I want to store the content of this pem key into a String privateKey...
You do not have to, as addIdentity(privateKey) takes as string parameter "the file name of the private key file". That is the reason you get the following error: java.io.FileNotFoundException: -----BEGIN RSA PRIVATE KEY--. You are passing the contents of the file instead of the file name/path, and JSch attempts to read a file using the string that you gave (i.e., "-----BEGIN RSA PRIVATE KEY--..."), but such a file cannot be found. So, instead, you need to pass the path/name of your private key file. For instance, "/path-to-the-file/myKey.pem", or if the key is in the project's root dir, just use "myKey.pem". If the file is encrypted with a passphrase then use addIdentity("myKey.pem", "passphrase").
I see that you already have a private key in the format that JSch supports. If, however, you happen to generate the key in new OpenSSH that starts with "-----BEGIN OPENSSH PRIVATE KEY-----", or if, for any reason, you get the following error: invalid privatekey: [B@59c40796, then use ssh-keygen to convert the key to PEM format, as decribed here.
ssh-keygen -p -f <filename> -m pem
If you are creating new keys with ssh-keygen, just add "-m pem" to generate the keys.
ssh-keygen -m pem
The following has been tested (using Rebex SFTP Server) and works as expected:
import com.jcraft.jsch.*;
public class JScheExample {
public void go() throws JSchException, SftpException {
ChannelSftp channelSftp = setupJsch("tester", "password", "id_rsa", "127.0.0.1", 2222);
//ChannelSftp channelSftp = setupJsch("username", "password", "myKey.pem", "127.0.0.1", 22);
channelSftp.connect();
// transfer file from local to remote server
channelSftp.put("clientTestFile.txt", "clientTestFile.txt");
// download file from remote server to local
channelSftp.get("testfile.txt", "testfile.txt");
}
private ChannelSftp setupJsch(String username, String password, String pvKeyPath, String hostname, int port) throws JSchException {
JSch jsch = new JSch();
if (!pvKeyPath.equals("") && pvKeyPath != null)
jsch.addIdentity(pvKeyPath);
Session jschSession = jsch.getSession(username, hostname, port);
if (!password.equals("") && password != null)
jschSession.setPassword(password);
jschSession.setConfig("StrictHostKeyChecking", "no");
jschSession.connect();
return (ChannelSftp) jschSession.openChannel("sftp");
}
public static void main(String[] args) throws JSchException, SftpException{
new JScheExample().go();
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Chris |
