jsch密钥连接远程Linux报错,信息如下:
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.(SSH.java:59) at com.scc.nanny.ssh.SSH.main(SSH.java:124)
主要原因是生成密钥的时候使用的openssh版本过高导致,其生成的密钥类似如下:
-----BEGIN OPENSSH PRIVATE KEY-----b3BlbnNzaC1rZXktdjEAAAAACmFlczI1Ni1jdHIAAAAGYmNyeXB0AAAAGAAAABDRomEs9dAv8l4J6WvCkc3NAAAAEAAAAAEAAAEXAAAAB3NzaC1yc2EAAAADAQABAAABAQDD1C48/OJ3wlBM60pV5bkeoaYPxxmul56E4k/0efMhWzsvq89texmpQ3AgZYaOXsUMztHmOxlr5JPLkPwAkZcU0PvCztU4uZVOY6+cztlFoTq5wXZ9XBRE6dlsUeEZzs9c++kGvkaCm/IhExnQTUAy5B2cMWHwTL88dFdxyY4vd6HG3n+JLJoCPQnG5VGSNBr2+DIc6Fll2ZyJhEA5zW4y+Ps26gGUIYIg7fueiz4MFgKE6t6GFcEr4BBqvNBOAEuHgAggH0gs2jQ7go5/huf98guQAb+u22cQoGjHgoJc+iMbo+ahs6x6jVPMWB4p5kApPrp5cX/f+XE/AmScUwnTAAAD8O2iFuYxKJ2kNUjd52P4FeH7WC/JLjmBo1d3MfR2pJGEODsIlvAihz0I2IEofHmxLg5HSx9p33aFdQZQ7o5cZDA7HKQ/6BPuCL0bvQA+Gu5gYp5KuNBGJlSTQ3m3khsvIp7eRkKvvt4UxGWPakL7ahlnybeFcJsB+dnyDJ5QCWVer2uEnPYqd+7p6vav/mrQyJdpjX9tMkMjo77UoPzSvXxtw4kxdsJeczgDvMSp/p2MXNQK7biC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XTBUStp1yXDUryveabJEgnRIW/g==-----END OPENSSH PRIVATE KEY-----
而以前的版本生成的密钥类似如下:
-----BEGIN RSA PRIVATE KEY-----Proc-Type: 4,ENCRYPTEDDEK-Info: AES-128-CBC,749E5AFD6F06C22EF6FD3AECCB6E540BFwOlFOtCM+JH3EG7gzDOffkwLysiDCucdUeDZaK08rFSWzKpMwfPD/AZKNi0dqZR1M63kFFuZvHWvtlTUvHcm2UqqupndnkSm+ShLDo8osczfp+iQJkgHRdZ6KSJ72FL12R34pkN9qAIUQUTIzQvQm8f3D9dYUmUSZk5v5WvELYgar+RsdV+6ejZS0s+aHeN2Rsi5vgeEjbRcb6o6qF/tUAH6mY+9pkBUIgJR8bIuHW/YudpUy9YAspiDL2pJwYO0eG+Co9KBJkJIiKlvm9zmH9yAjX+o3vcDqlOPsOuTEzON4AFZKb6OwKO7m3y6+wCP/oEdfsGtWQL33vSfNe04fFRFkQ06wDQ/eaObG0vgPZCrZT2dfrI3eUs76nzCb84GeM7+hrEFeijfni3BfqhFZ9QG0fuudYmliK3A9vJzW2DNMAM9ZR4j4VGVfmwCdG3GiRdxxG6TA8rQuAXEtEgDkI9AtIU3fo/kaC3fD2weVVVN8Mpmr1yCdMxgdESoRfMSgg0v6/XG4sCtjxKNhDrLF3LjKAV5kV+zS34f1ycYqfHijEVXcV8rRTrys6ieIqqGtCVUaMUh7LWWRlQ1uRUMIW39OpW8P/Q/5Syst8plvzBjSX/pUPj+0O4nJLsVD4E1Jr5RDymNB/7IlSrdEycMKhz/WShteQ3JV++rv3wheRfuTQiuZUTGQCSSkwv1G7sIP68Cdm4NZhKZUN3AcoMOWf9DsfDy2uXFUYSqWtu2GtRcmfGT1v+B148uk6WpYV60X5dRtj36/uM4tNoaED9SG9FkvKdbb15PyYCWmtmeZjo4MLjrxELePsPJSl85IfcUl3WfhHd3Iz1Jyd5ZApNWSXKOGVnc1Zu/0zrhbY7lrUBCKK4rASLL/ZtUQNaPXYi9sBPZ1B4dga3VuTyoUqN999rl+nbM/66ckYEqTsfoNiKvyStF5Nm6Faf9Ez0fZT91VQlUvCvSLhA7NLnRDsAyG1ZOYfY7itZKLvCtglVSoNWgOv6I2wn+f2bs4hXZ2wRfYkUApMLH9t1IM9JCZct5Jfpok5KmF9kN7Fa510EEnuuJK03xcoi0GmSUu2yGN4k1SExm2zmsUHP+4HfTmPv848KhFZbs5VLSJ+cJSZKjyK3EoNvM4YUQuWm0IRm3bFB/sYETXnMY90HDhR7fjG0H074x0wgk+9Wd6rdqkfbizYEz3yvypAEPcB0lCBZQsMFuyvDPshdokhwlmGJXN6uh7SUlEKUCMpJE0sSCVRD3uVYg0ErAOa+1BuIJKOSCp0DCCElu0gnnKctvTK+aW/l011uzw6Ck0KgUu+KU9SfWmuTWNnsIFe8HfxIyiIDblczlqrgB2wks6hdi3Ealtb4B+mseVpeRJhiNWJ2DIiXo4CxD6NZffiUW5BXNpNcDyuSzCQwXQ4eduMR+qy41aHac6FiChvauXe09QNpANlxxlCIC6OVa/priFnLx5X849tnxiCnjGVO0p/altdM2G5Z5LGSMu/rjUK6YxBdi8oRYGhbAtgVOkdicoAFIOs+spVxdAzGWpRtuUIlIB97oXloovzwyMZGXWRy6yhLWJlpSYKSADXoEIQ8NuN+e33UEzeq-----END RSA PRIVATE KEY-----
我的jsch版本是0.1.54,这个版本是解析不了最新版的密钥的。