jcabi / jcabi-ssh

Java SSH client (an object-oriented wrapper around JSch)
https://ssh.jcabi.com
Other
253 stars 88 forks source link

Invalid private key generated with OpenSSH #33

Open lordofthejars opened 8 years ago

lordofthejars commented 8 years ago

I am trying to use a private key generated with OpenSSH, but I always get an invalid private key exception. I am using Java8 and jcabi-ssh 1.5. The private key is (it is for testing purposes):

-----BEGIN RSA PRIVATE KEY-----
8glbkhKvJ9OAOwX7iy7gcWzBLsENZouu/KIUg3yRIUQruM04Dvrd1Dv9mWifVHWT
1mAqeln1MRi3vR841NEhGD4vF3FmxY6tX+SYijEQ6RYcIthNTmc2GHf0hW2/yiny
0hjeoq1RYalD8SHknCM9ah2vHxVt6skBFpMoRJk9H56rEw+3TmZ7Fy5D0woT6fk2
Gl/Ntdr+h2fbWopPm6ZLoX32hFBE2mwRgjfC5Kwx3zDcSW2VGCZ2sBc/cFcv7B5z
lCcoR1xGj62sg2v9TCvdZ6hSBugbJ0w8pkrDCBBvK6ZrMqAZg/vp9uwB1bYh+VL3
rFlisD5qMI3/RTIw8Lh0loWAmS4Hn/Kq/SqXfyJT7zsXeF0aY3j+vRlx9qzb2rHZ
wKAq8vevVDOLT2y/rrKMBbKm7rPNFDyldTjnB9yX2dsod8oMhsxgqmPqrR1ukbdo
vd3SIHa+DjoA+gwZlpbas586oZZn3r83k5NbgUsn+ewOCax7B6RfMEtWKg+LVMYV
H+H4L/wLtMZcjYK/SnFhufweRjPzTnnJpd/Bq9P7DilodZygbBhR4k8/s8TbpQHr
yEeE9PKf6q+6NAd70KdLzTxf4T+P9svkW0pB6il/V3XWVrxpcdvlxRF7Xyi935Ay
GL2IvUK9Ndfns/bvjPqq6PpX/W8tr4qIeUakqsI1DsYkoKDALiqpXKWDhGHX6MxG
Ki01wA3D4Me3Yzgx71HPy9p6ujU/2Az0udp9he2WoKvEh1X7Bk9VRVnatP7tADUS
lC17Y2PevdUlYETS31x1gJ0lW+NROs7jwC9sgssykVriZoXftiWpuQVcGO1yyT6G
-----END RSA PRIVATE KEY-----

And the code I am using:

String privateKey = IOUtils.toString(
                JSchTest.class.getResourceAsStream("/org/arquillian/cube/containerobject/SshdContainer/test_rsa"), "UTF-8");
        System.out.println(privateKey);
        String hello = new Shell.Plain(
                new SSH(
                        sshdContainer.getIp(), 2222,
                        "test", privateKey
                        )
        ).exec("echo 'Hello, world!'");

Anything that I am going wrong, do I need another dependency?

dmarkov commented 8 years ago

@yegor256 please do something about this issue

yegor256 commented 8 years ago

@lordofthejars looks correct to me. what exactly do you see? can you post stacktrace here?

lordofthejars commented 8 years ago

Sorry for being a bit late, JavaOne thing hehehe:

java.io.IOException: com.jcraft.jsch.JSchException: invalid privatekey: [B@24a1c17f
    at com.jcabi.ssh.SSH.session(SSH.java:279)
    at com.jcabi.ssh.SSH.exec(SSH.java:219)
    at com.jcabi.ssh.Shell$Plain.exec(Shell.java:172)
Caused by: com.jcraft.jsch.JSchException: invalid privatekey: [B@24a1c17f
    at com.jcraft.jsch.KeyPair.load(KeyPair.java:902)
    at com.jcraft.jsch.KeyPair.load(KeyPair.java:543)
    at com.jcraft.jsch.IdentityFile.newInstance(IdentityFile.java:40)
    at com.jcraft.jsch.JSch.addIdentity(JSch.java:393)
    at com.jcraft.jsch.JSch.addIdentity(JSch.java:353)
    at com.jcabi.ssh.SSH.session(SSH.java:261)
lordofthejars commented 8 years ago

Any idea? Maybe I need to install something?

MathieuGingras commented 8 years ago

Any news on this ? I got the same error but I know the key is valid, since I use it almost everyday

lordofthejars commented 8 years ago

No answer, I tried everything but didn't work and I decided to give up.

MathieuGingras commented 8 years ago

Disappointed to hear this but thanks for the info !

MathieuGingras commented 8 years ago

If it may interest others, I was able to fix my problem. It turns out that my private key need a passphrase. By switching to 2.0-snapshot, and using the new constructor (https://github.com/jcabi/jcabi-ssh/issues/50) I was able to make it work.

soreana commented 4 years ago

I have the same problem. It was my mistake. I forget to put a "\n" at the end of each line, in my key string.

mgemmell commented 4 years ago

I have the same problem. It was my mistake. I forget to put a "\n" at the end of each line, in my key string.

Same with me, I had to edit my private key file (.ppk) to replace the \r with an \n, at the end of each line. I did this easily in Notepad++ with Find/Replace, Extended search mode, find "\r" and replace with "\n".