Sample code for 30+ languages & platforms
Android™

Initiate an SSH Key Re-Exchange

See more SSH Examples

Demonstrates the Chilkat Ssh.ReKey method, which initiates an SSH key re-exchange and waits for it to complete. It takes no arguments. Either the client or the server may trigger rekeying at any time.

Background: Rekeying periodically replaces the session keys to limit how much data is protected by any single key. RFC 4253 recommends it after roughly one gigabyte of transfer or one hour, whichever comes first. In practice servers initiate this automatically and Chilkat handles it transparently, so calling ReKey explicitly is rarely necessary.

Chilkat Android™ Downloads

Android™
// 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 Ssh.ReKey method, which initiates an SSH key re-exchange and waits for it to
    //  complete.  It takes no arguments.  In normal use the server initiates rekeying as needed and
    //  Chilkat handles it transparently.

    CkSsh ssh = new CkSsh();

    //  Connect to the SSH server.  Port 22 is the usual SSH port.
    int sshPort = 22;
    success = ssh.Connect("ssh.example.com",sshPort);
    if (success == false) {
        Log.i(TAG, ssh.lastErrorText());
        return;
        }

    //  Normally you would not hard-code the password in source.  You should instead obtain it
    //  from an interactive prompt, environment variable, or a secrets vault.
    String password = "mySshPassword";

    success = ssh.AuthenticatePw("mySshLogin",password);
    if (success == false) {
        Log.i(TAG, ssh.lastErrorText());
        return;
        }

    //  Explicitly initiate a key re-exchange on the live connection.
    success = ssh.ReKey();
    if (success == false) {
        Log.i(TAG, ssh.lastErrorText());
        return;
        }

    Log.i(TAG, "Key re-exchange complete.");

    ssh.Disconnect();

  }

  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."
  }
}