【问题标题】:Public Key Authentication using j2ssh-maverick-1.5.5.jar使用 j2ssh-maverick-1.5.5.jar 的公钥认证
【发布时间】:2019-09-07 15:26:21
【问题描述】:

下面是使用公钥认证连接到远程服务器的代码 sn-p。我已经使用 Putty Key Gen 工具生成了公钥和私钥,并根据需要修改了 .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();
                }

【问题讨论】:

    标签: authentication key public


    【解决方案1】:
    SshPrivateKeyFile pkf = SshPrivateKeyFileFactory.parse(new FileInputStream("E:\\Projects\\RBL\\Finacle Interface\\Finacle\\AuthenticationKeys\\TestRBL"));
                             SshKeyPair pair = pkf.toKeyPair("calypso");
    

    解决了我的问题

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-01-06
        • 2017-07-06
        • 1970-01-01
        • 2021-12-18
        • 2013-02-28
        • 1970-01-01
        • 2012-05-22
        相关资源
        最近更新 更多