Monday, January 26, 2015

Missing passphrase in JSch

I have a key pair being setup with passphrase, when I use this key to establish a SFTP connection through following code snippet, I still got an authentication error.
  ...

  JSch jsch = new JSch();
  jsch.addIdentity(privateKey);

  ...
I was wondering why this could happened? Am I making any mistake when generating the key pair? Even though stacktrace below showing me some clue but I still couldn't find out the real root cause.
com.jcraft.jsch.JSchException: USERAUTH fail
   at com.jcraft.jsch.UserAuthPublicKey.start(UserAuthPublicKey.java:118)
   at com.jcraft.jsch.Session.connect(Session.java:463)
   at com.jcraft.jsch.Session.connect(Session.java:183)
Or maybe I have configure the passphrase wrongly? This took me sometimes reading on the Java Secure Channel documentation. Found out that I have overlook on the addIdentity() which could actually take the passphrase as 2nd parameter. Without further ado, the code snippet above have been updated as shown below and test run it:
  ...

  JSch jsch = new JSch();
  jsch.addIdentity(privateKey, "Passphrase");

  ...
Tada! I have the SFTP connection established successfully.

No comments: