1 year ago

#295183

test-img

Keshavram Kuduwa

JSchException: Auth fail | Apache SSHD & JCraft JSch

I'm trying to connect to a mock server using RSK keys generated using KeyPairGenerator.

  Constructor() throws NoSuchAlgorithmException, NoSuchProviderException {
    KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA");
    keyGen.initialize(2048);
    this.pair = keyGen.generateKeyPair();
  }

My SshServer configuration is as follows:

  @BeforeAll
  @SneakyThrows
  public void setup() {
    sshd = SshServer.setUpDefaultServer();
    sshd.setPort(authentication.getPort());
    File keyFile = new File(".tmp/host.ser");
    FileUtils.forceMkdirParent(keyFile);
    sshd.setKeyPairProvider(new SimpleGeneratorHostKeyProvider(keyFile.toPath()));
    sshd.setSubsystemFactories(Collections.singletonList(new SftpSubsystemFactory()));
    sshd.setPasswordAuthenticator(
        (username, password, session) ->
            username.equals(authentication.getUsername())
                && password.equals(authentication.getPassword()));
    String privateKey =
        "-----BEGIN PRIVATE KEY-----\n"
            + Base64.getMimeEncoder().encodeToString(pair.getPrivate().getEncoded())
            + "\n-----END PRIVATE KEY-----";
    String publicKey =
        "-----BEGIN PUBLIC KEY-----\n"
            + Base64.getMimeEncoder().encodeToString(pair.getPublic().getEncoded())
            + "\n-----END PUBLIC KEY-----";
    FileUtils.write(new File(".ssh/id_rsa"), privateKey, Charset.defaultCharset());
    FileUtils.write(new File(".ssh/id_rsa.pub"), publicKey, Charset.defaultCharset());
    sshd.setPublickeyAuthenticator(
        new AuthorizedKeysAuthenticator(new File(".ssh/id_rsa.pub").toPath()));
    sshd.start();

  }

And the following is my JSch configuration:

  @SneakyThrows
  public void open() {
    JSch jsch = new JSch();
    Properties config = new Properties();
    config.put("StrictHostKeyChecking", "no");
    if (StringUtils.isNoneBlank(privateKey)) {
      jsch.addIdentity(
          ".ssh/id_rsa", Objects.nonNull(passphrase) ? passphrase.getBytes() : null);
    }

    session = jsch.getSession(username, server, Objects.nonNull(port) ? port : DEFAULT_PORT);
    session.setConfig(config);
    if (StringUtils.isNoneBlank(password)) session.setPassword(password);
    session.connect();
    channelSftp = (ChannelSftp) session.openChannel("sftp");
    channelSftp.connect();
  }

The exception I'm getting is as follows:

Auth fail
com.jcraft.jsch.JSchException: Auth fail
    at app//com.jcraft.jsch.Session.connect(Session.java:519)
    at app//com.jcraft.jsch.Session.connect(Session.java:183)

For some reason, the RSA keys that I generated aren't working even though they're similar to the system-generated keys. But when I created a private key and public key using ssh-keygen -p -m pem and used the keys from /home/my-home/.ssh/, it worked. It's just not working when I create the keys using KeyPairGenerator. Thanks in advance, any suggestion will be appreciated.

java

jsch

sshd

apache-mina

0 Answers

Your Answer

Accepted video resources