Public 使用 j2ssh-maverick-1.5 进行密钥验证。5.jar

Public Key Authentication using j2ssh-maverick-1.5.5.jar

下面是使用 public 密钥身份验证连接到远程服务器的代码片段。我使用 Putty Key Gen 工具生成了 public 和私钥,并根据需要修改了 .ssh 文件夹中的 authorized_keys 文件。我能够使用 Putty 连接到远程服务器并提供提示的密码。但是,我无法通过以下代码进行连接。它告诉我 -

java.io.IOException: 无法读取 PuTTY 密钥!无效的加密密钥

有什么想法吗?

SocketTransport transport = new SocketTransport(hostname, port);
                    ssh = con.connect(transport, username);

                    FileInputStream in;
                    ByteArrayOutputStream out;
                    try 
                    {
                        in = new FileInputStream("E:\Projects\RBL\Finacle Interface\Finacle\AuthenticationKeys\RBLTestPrivateKey.ppk");
                        out = new ByteArrayOutputStream();
                         int read;
                         while((read = in.read()) > -1)
                           out.write(read);

                         in.close();

                         SshPrivateKeyFile pkf = SshPrivateKeyFileFactory.parse(out.toByteArray());
                         SshKeyPair pair = pkf.toKeyPair("calypso");

                         PublicKeyAuthentication pk = new PublicKeyAuthentication();
                         pk.setPrivateKey(pair.getPrivateKey());
                         pk.setPublicKey(pair.getPublicKey());

                         if(ssh.authenticate(pk)==SshAuthentication.COMPLETE)
                         {
                             Log.info(LOG_CATEGORY, "Authentication completed");
                             session = ssh.openSessionChannel();
                             return session;
                         }

                    } 
                    catch (IOException | InvalidPassphraseException | SshException e1) 
                    {
                        e1.printStackTrace();
                    }

                    /*PasswordAuthentication pwd = new PasswordAuthentication();
                    pwd.setPassword(this.password); 
                    if(ssh.authenticate(pwd)==SshAuthentication.COMPLETE) 
                    {
                        session = ssh.openSessionChannel();
                        return session;
                    }*/
                }
                catch(Exception e)
                {
                    isConnected = false;
                    e.printStackTrace();
                }

如果您将密钥对更改为 OpenSSH 密钥,它可能会起作用...

SshPrivateKeyFile pkf = SshPrivateKeyFileFactory.parse(new FileInputStream("E:\Projects\RBL\Finacle Interface\Finacle\AuthenticationKeys\TestRBL"));
                         SshKeyPair pair = pkf.toKeyPair("calypso");

解决了我的问题