Sample code for 30+ languages & platforms
Android™

Use a PuTTY Key for SSH Authentication

See more SSH Examples

Demonstrates authenticating with an SSH server using a username and a PuTTY (.ppk) private key. The key is imported with FromPuttyPrivateKey — setting Password first if it is encrypted — and passed to AuthenticatePk.

Note: The file paths are relative to the application's current working directory. Supply the paths to your own files.

Background: A PuTTY key can be used directly for authentication with no conversion step, which is convenient when the key was generated by PuTTYgen on Windows. Conceptually the private key takes the place of a password while the username identifies the account, and the corresponding public key must already be installed on the server. Convert to OpenSSH format only if some other tool in your pipeline requires PEM.

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;

    //  This example requires the Chilkat API to have been previously unlocked.
    //  See Global Unlock Sample for sample code.

    //  Demonstrates authenticating with an SSH server using a username and a PuTTY (.ppk) private
    //  key.  The private key serves as the credential; the username identifies the account on the
    //  server.

    CkSsh ssh = new CkSsh();

    //  LoadText is a convenience method that reads any text file into a string.  It does not itself
    //  load the key.
    CkSshKey puttyKey = new CkSshKey();
    String ppkText = puttyKey.loadText("qa_data/ppk/putty_private_secret.ppk");
    if (puttyKey.get_LastMethodSuccess() == false) {
        Log.i(TAG, puttyKey.lastErrorText());
        return;
        }

    //  Set the password before importing an encrypted .ppk.  This should come from a secure source
    //  rather than being hard-coded.
    puttyKey.put_Password("myKeyPassword");

    success = puttyKey.FromPuttyPrivateKey(ppkText);
    if (success == false) {
        Log.i(TAG, puttyKey.lastErrorText());
        return;
        }

    String sshHostname = "ssh.example.com";
    int sshPort = 22;
    success = ssh.Connect(sshHostname,sshPort);
    if (success == false) {
        Log.i(TAG, ssh.lastErrorText());
        return;
        }

    //  The corresponding public key must already be installed on the SSH server for the account.
    success = ssh.AuthenticatePk("mySshLogin",puttyKey);
    if (success == false) {
        Log.i(TAG, ssh.lastErrorText());
        return;
        }

    Log.i(TAG, "Connection and authentication with the SSH server completed.");

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