Android™
Android™
Authenticate an SSH Tunnel with a Private Key
See more Socket/SSL/TLS Examples
Demonstrates Socket.SshAuthenticatePk, which authenticates an SSH tunnel session using a private key loaded into an SshKey object. Call SshOpenTunnel first.
The file path is relative to the application's current working directory. An absolute path may also be used. Supply the path appropriate to your own environment.
Background. The SSH server must have the corresponding public key authorized for the account. If the private key is encrypted, set the SshKey.Password property before loading it.
Chilkat Android™ Downloads
// Important: Don't forget to include the call to System.loadLibrary
// as shown at the bottom of this code sample.
package com.test;
import android.app.Activity;
import com.chilkatsoft.*;
import android.widget.TextView;
import android.os.Bundle;
public class SimpleActivity extends Activity {
private static final String TAG = "Chilkat";
// Called when the activity is first created.
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
boolean success = false;
CkSocket socket = new CkSocket();
success = socket.SshOpenTunnel("ssh.example.com",22);
if (success == false) {
Log.i(TAG, socket.lastErrorText());
return;
}
// Load the SSH private key. The file path is relative to the application's current working
// directory. If the key is encrypted, set the SshKey.Password property before calling
// FromOpenSshPrivateKey.
CkSshKey sshKey = new CkSshKey();
String keyText = sshKey.loadText("qa_data/id_ed25519");
if (sshKey.get_LastMethodSuccess() == false) {
Log.i(TAG, sshKey.lastErrorText());
return;
}
success = sshKey.FromOpenSshPrivateKey(keyText);
if (success == false) {
Log.i(TAG, sshKey.lastErrorText());
return;
}
// Authenticate the SSH tunnel session using public-key authentication. The SSH server must have the
// corresponding public key authorized for the account.
success = socket.SshAuthenticatePk("sshUser",sshKey);
if (success == false) {
Log.i(TAG, socket.lastErrorText());
return;
}
Log.i(TAG, "SSH public-key authentication succeeded.");
}
static {
System.loadLibrary("chilkat");
// Note: If the incorrect library name is passed to System.loadLibrary,
// then you will see the following error message at application startup:
//"The application <your-application-name> has stopped unexpectedly. Please try again."
}
}