Invalid private key when opening SSH tunnel with JSch

筅森魡賤 提交于 2020-07-15 08:20:50

问题


With JSch I'm calling addIdentity() to add a private key and getSession() to open an SSH tunnel.

When running this code locally on my Windows machine the opening of the tunnel is working.

However when running that same code with the same private key on our CI the following error occurs:

2016-12-07 01:01:32 ERROR SSHConnector:25 - invalid privatekey: [B@4bb4de6a
com.jcraft.jsch.JSchException: invalid privatekey: [B@4bb4de6a
    at com.jcraft.jsch.KeyPair.load(KeyPair.java:747)
    at com.jcraft.jsch.KeyPair.load(KeyPair.java:561)
    at com.jcraft.jsch.IdentityFile.newInstance(IdentityFile.java:40)
    at com.jcraft.jsch.JSch.addIdentity(JSch.java:408)
    at com.jcraft.jsch.JSch.addIdentity(JSch.java:389)

The private key looks something like this:

PuTTY-User-Key-File-2: ssh-rsa
Encryption: none
Comment: imported-openssh-key
Public-Lines: 6
XXXXXXXXXXXXXXXXXXX
Private-Lines: 14
XXXXXXXXXXXXXXXXXX
Private-MAC: XXXXXXXXXXXXXXXX

What could be the error here?


回答1:


My problem was solved by using pem file instead of ppk.

pem file is in OpenSSH format that com.jcraft.jsch.JSch jar requires




回答2:


My educated guess is that jSCh does not support PuTTY key format, but rather standard PEM or OpenSSH format. You can convert the PPK to PEM in the PuTTYgen and it should solve your problems.



来源:https://stackoverflow.com/questions/41014428/invalid-private-key-when-opening-ssh-tunnel-with-jsch

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!