Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Invalid private key generated with OpenSSH #33

Open
lordofthejars opened this issue Oct 19, 2015 · 10 comments
Open

Invalid private key generated with OpenSSH #33

lordofthejars opened this issue Oct 19, 2015 · 10 comments

Comments

@lordofthejars
Copy link

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
Copy link

dmarkov commented Oct 22, 2015

@yegor256 please do something about this issue

@yegor256
Copy link
Member

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

@lordofthejars
Copy link
Author

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
Copy link
Author

Any idea? Maybe I need to install something?

@MathieuGingras
Copy link

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

@lordofthejars
Copy link
Author

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

@MathieuGingras
Copy link

Disappointed to hear this but thanks for the info !

@MathieuGingras
Copy link

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 (#50) I was able to make it work.

@soreana
Copy link

soreana commented Nov 29, 2019

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
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants