jcabi-ssh icon indicating copy to clipboard operation
jcabi-ssh copied to clipboard

Invalid private key generated with OpenSSH

Open lordofthejars opened this issue 10 years ago • 10 comments

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?

lordofthejars avatar Oct 19 '15 08:10 lordofthejars

@yegor256 please do something about this issue

dmarkov avatar Oct 22 '15 08:10 dmarkov

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

yegor256 avatar Oct 23 '15 04:10 yegor256

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 avatar Oct 24 '15 14:10 lordofthejars

Any idea? Maybe I need to install something?

lordofthejars avatar Nov 02 '15 12:11 lordofthejars

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

MathieuGingras avatar Apr 15 '16 16:04 MathieuGingras

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

lordofthejars avatar Apr 15 '16 16:04 lordofthejars

Disappointed to hear this but thanks for the info !

MathieuGingras avatar Apr 15 '16 16:04 MathieuGingras

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.

MathieuGingras avatar Apr 15 '16 21:04 MathieuGingras

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.

soreana avatar Nov 29 '19 09:11 soreana

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".

mgemmell avatar Jun 23 '20 03:06 mgemmell