'Apache SSHD client: authenticate with identity file

I am trying to write an SSH client using MINA SSHD, connecting with an identity file generated by openSSH (id_rsa and the corresponding public key id_rsa.pub)

The docs say to write something along these lines:

    SshClient client = SshClient.setUpDefaultClient();
    client.start();

    // using the client for multiple sessions...
    try (ClientSession session = client.connect(user, host, port)
                .verify(1000)
                .getSession()) {
        session.addPublicKeyIdentity(/* ...key-pair... */);
        session.auth().verify(1000);
        // ...
    }
    client.stop();

How do I build the argument to the method addPublicKeyIdentity? How do I convert my identity file to a KeyPair?

Many thanks



Solution 1:[1]

You may try to use org.apache.sshd.common.keyprovider.FileKeyPairProvider instead.

Example:

FileKeyPairProvider provider = new FileKeyPairProvider(/*Path to your privateKey*/);
provider.setPasswordFinder(FilePasswordProvider.of(/*your private key passphrase*/));
session.setKeyIdentityProvider(provider);

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 bessonm