jsch密钥连接远程Linux报错,信息如下:
代码语言:javascript复制Exception in thread "main" com.jcraft.jsch.JSchException: invalid privatekey: [B@277050dc
at com.jcraft.jsch.KeyPair.load(KeyPair.java:664)
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:407)
at com.jcraft.jsch.JSch.addIdentity(JSch.java:388)
at com.scc.nanny.ssh.SSH.<init>(SSH.java:59)
at com.scc.nanny.ssh.SSH.main(SSH.java:124)
主要原因是生成密钥的时候使用的openssh版本过高导致,其生成的密钥类似如下:
代码语言:javascript复制-----BEGIN OPENSSH PRIVATE KEY-----
b3BlbnNzaC1rZXktdjEAAAAACmFlczI1Ni1jdHIAAAAGYmNyeXB0AAAAGAAAABDRomEs9d
Av8l4J6WvCkc3NAAAAEAAAAAEAAAEXAAAAB3NzaC1yc2EAAAADAQABAAABAQDD1C48/OJ3
wlBM60pV5bkeoaYPxxmul56E4k/0efMhWzsvq89texmpQ3AgZYaOXsUMztHmOxlr5JPLkP
wAkZcU0PvCztU4uZVOY6 cztlFoTq5wXZ9XBRE6dlsUeEZzs9c kGvkaCm/IhExnQTUAy
5B2cMWHwTL88dFdxyY4vd6HG3n JLJoCPQnG5VGSNBr2 DIc6Fll2ZyJhEA5zW4y Ps26g
GUIYIg7fueiz4MFgKE6t6GFcEr4BBqvNBOAEuHgAggH0gs2jQ7go5/huf98guQAb u22cQ
oGjHgoJc iMbo ahs6x6jVPMWB4p5kApPrp5cX/f XE/AmScUwnTAAAD8O2iFuYxKJ2kNU
jd52P4FeH7WC/JLjmBo1d3MfR2pJGEODsIlvAihz0I2IEofHmxLg5HSx9p33aFdQZQ7o5c
ZDA7HKQ/6BPuCL0bvQA Gu5gYp5KuNBGJlSTQ3m3khsvIp7eRkKvvt4UxGWPakL7ahlnyb
eFcJsB dnyDJ5QCWVer2uEnPYqd 7p6vav/mrQyJdpjX9tMkMjo77U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BLFB9BjQYkOPXLShKpTnynFPMoLAdDG8h4Tf9XTBUStp1yXDUryveabJEgnRIW
/g==
-----END OPENSSH PRIVATE KEY-----
而以前的版本生成的密钥类似如下:
代码语言:javascript复制-----BEGIN RSA PRIVATE KEY-----
Proc-Type: 4,ENCRYPTED
DEK-Info: AES-128-CBC,749E5AFD6F06C22EF6FD3AECCB6E540B
FwOlFOtCM JH3EG7gzDOffkwLysiDCucdUeDZaK08rFSWzKpMwfPD/AZKNi0dqZR
1M63kFFuZvHWvtlTUvHcm2UqqupndnkSm ShLDo8osczfp iQJkgHRdZ6KSJ72FL
12R34pkN9qAIUQUTIzQvQm8f3D9dYUmUSZk5v5WvELYgar RsdV 6ejZS0s aHeN
2Rsi5vgeEjbRcb6o6qF/tUAH6mY 9pkBUIgJR8bIuHW/YudpUy9YAspiDL2pJwYO
0eG Co9KBJkJIiKlvm9zmH9yAjX o3vcDqlOPsOuTEzON4AFZKb6OwKO7m3y6 wC
P/oEdfsGtWQL33vSfNe04fFRFkQ06wDQ/eaObG0vgPZCrZT2dfrI3eUs76nzCb84
GeM7 hrEFeijfni3BfqhFZ9QG0fuudYmliK3A9vJzW2DNMAM9ZR4j4VGVfmwCdG3
GiRdxxG6TA8rQuAXEtEgDkI9AtIU3fo/kaC3fD2weVVVN8Mpmr1yCdMxgdESoRfM
Sgg0v6/XG4sCtjxKNhDrLF3LjKAV5kV zS34f1ycYqfHijEVXcV8rRTrys6ieIqq
GtCVUaMUh7LWWRlQ1uRUMIW39OpW8P/Q/5Syst8plvzBjSX/pUPj 0O4nJLsVD4E
1Jr5RDymNB/7IlSrdEycMKhz/WShteQ3JV rv3wheRfuTQiuZUTGQCSSkwv1G7s
IP68Cdm4NZhKZUN3AcoMOWf9DsfDy2uXFUYSqWtu2GtRcmfGT1v B148uk6WpYV6
0X5dRtj36/uM4tNoaED9SG9FkvKdbb15PyYCWmtmeZjo4MLjrxELePsPJSl85Ifc
Ul3WfhHd3Iz1Jyd5ZApNWSXKOGVnc1Zu/0zrhbY7lrUBCKK4rASLL/ZtUQNaPXYi
9sBPZ1B4dga3VuTyoUqN999rl nbM/66ckYEqTsfoNiKvyStF5Nm6Faf9Ez0fZT9
1VQlUvCvSLhA7NLnRDsAyG1ZOYfY7itZKLvCtglVSoNWgOv6I2wn f2bs4hXZ2wR
fYkUApMLH9t1IM9JCZct5Jfpok5KmF9kN7Fa510EEnuuJK03xcoi0GmSUu2yGN4k
1SExm2zmsUHP 4HfTmPv848KhFZbs5VLSJ cJSZKjyK3EoNvM4YUQuWm0IRm3bFB
/sYETXnMY90HDhR7fjG0H074x0wgk 9Wd6rdqkfbizYEz3yvypAEPcB0lCBZQsMF
uyvDPshdokhwlmGJXN6uh7SUlEKUCMpJE0sSCVRD3uVYg0ErAOa 1BuIJKOSCp0D
CCElu0gnnKctvTK aW/l011uzw6Ck0KgUu KU9SfWmuTWNnsIFe8HfxIyiIDblcz
lqrgB2wks6hdi3Ealtb4B mseVpeRJhiNWJ2DIiXo4CxD6NZffiUW5BXNpNcDyuS
zCQwXQ4eduMR qy41aHac6FiChvauXe09QNpANlxxlCIC6OVa/priFnLx5X849tn
xiCnjGVO0p/altdM2G5Z5LGSMu/rjUK6YxBdi8oRYGhbAtgVOkdicoAFIOs spVx
dAzGWpRtuUIlIB97oXloovzwyMZGXWRy6yhLWJlpSYKSADXoEIQ8NuN e33UEzeq
-----END RSA PRIVATE KEY-----
我的jsch版本是0.1.54,这个版本是解析不了最新版的密钥的。