问题
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