Android™
Android™
Authenticate an SSH Tunnel with a Private Key
See more POP3 Examples
Demonstrates the Chilkat MailMan.SshAuthenticatePk method, which authenticates with the SSH server using public-key authentication after an SSH tunnel has been opened. The first argument is the SSH account name and the second is the SSH private key (an SshKey object). This example opens a tunnel, loads an OpenSSH private key, authenticates with it, and runs a POP3 operation through the tunnel.
Background: SSH public-key authentication replaces a password with a key pair: the public key is installed on the SSH server, and the client proves it holds the matching private key without ever transmitting a secret. This is the preferred method for automated systems — there is no password to embed or rotate, and the key can be revoked server-side. Chilkat reads the key with an
SshKey object, which understands common formats such as OpenSSH and PuTTY.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;
// Demonstrates the MailMan.SshAuthenticatePk method, which authenticates with the SSH server
// using public-key authentication after an SSH tunnel has been opened. The 1st argument is
// the SSH account name and the 2nd is the SSH private key.
CkMailMan mailman = new CkMailMan();
// Configure the POP3 server connection. These connections will travel through the tunnel.
mailman.put_MailHost("pop.example.com");
mailman.put_MailPort(995);
mailman.put_PopSsl(true);
mailman.put_PopUsername("user@example.com");
mailman.put_PopPassword("myPassword");
// Open the SSH tunnel first.
success = mailman.SshOpenTunnel("ssh.example.com",22);
if (success == false) {
Log.i(TAG, mailman.lastErrorText());
return;
}
// Load the SSH private key from an OpenSSH-format key file.
CkSshKey sshKey = new CkSshKey();
String keyText = sshKey.loadText("qa_data/ssh/id_rsa");
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 with the SSH server using the private key.
success = mailman.SshAuthenticatePk("sshUser",sshKey);
if (success == false) {
Log.i(TAG, mailman.lastErrorText());
return;
}
// POP3 operations now travel through the SSH tunnel.
int count = mailman.GetMailboxCount();
if (count < 0) {
Log.i(TAG, "Failed to get the mailbox count.");
}
else {
Log.i(TAG, "Mailbox count: " + String.valueOf(count));
}
success = mailman.SshCloseTunnel();
if (success == false) {
Log.i(TAG, mailman.lastErrorText());
return;
}
// Note: The path "qa_data/ssh/id_rsa" is a relative local filesystem path,
// relative to the current working directory of the running application.
}
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."
}
}