Android™
Android™
SSH Tunnel Authenticate with Secure Strings
See more SSH Examples
Demonstrates SSH tunnel password authentication using SecureString objects, with the credentials read from an encrypted JSON config file and decrypted directly into secure strings rather than being hard-coded in source.
Background: The
SshTunnel class runs the tunnel on a background thread, so it authenticates once and then serves connections until closed — which makes protecting the stored credential especially worthwhile. Decrypting straight into a SecureString avoids ever holding the password in an ordinary string, and the value stays encrypted in memory under a randomly generated session key. In production the decryption key itself would come from a key vault or OS keystore rather than a literal.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;
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
// Demonstrates SSH tunnel password authentication using SecureString objects, with the
// credentials read from an encrypted config file rather than hard-coded in source.
// Imagine the login and password were previously encrypted and saved in a JSON config file:
// {
// "ssh_login": "2+qylFfC56Ck7OQQt/U2/w==",
// "ssh_password": "5neIq9Jmn0E3p71N6Yc8TA=="
// }
CkJsonObject json = new CkJsonObject();
success = json.LoadFile("qa_data/passwords/ssh.json");
if (success == false) {
Log.i(TAG, json.lastErrorText());
return;
}
// These are the encryption settings used when the credentials were encrypted. In a real
// application the key would come from a secure source, not a literal.
CkCrypt2 crypt = new CkCrypt2();
crypt.put_CryptAlgorithm("aes");
crypt.put_CipherMode("cbc");
crypt.put_KeyLength(128);
crypt.SetEncodedKey("000102030405060708090A0B0C0D0E0F","hex");
crypt.SetEncodedIV("000102030405060708090A0B0C0D0E0F","hex");
crypt.put_EncodingMode("base64");
// Decrypt directly into SecureString objects. The values stay encrypted in memory, now under
// a randomly generated session key.
CkSecureString ssLogin = new CkSecureString();
CkSecureString ssPassword = new CkSecureString();
crypt.DecryptSecureENC(json.stringOf("ssh_login"),ssLogin);
crypt.DecryptSecureENC(json.stringOf("ssh_password"),ssPassword);
CkSshTunnel tunnel = new CkSshTunnel();
int port = 22;
success = tunnel.Connect("ssh.example.com",port);
if (success == false) {
Log.i(TAG, tunnel.lastErrorText());
return;
}
// Authenticate using the secure strings.
success = tunnel.AuthenticateSecPw(ssLogin,ssPassword);
if (success == false) {
Log.i(TAG, tunnel.lastErrorText());
return;
}
Log.i(TAG, "SSH tunnel authentication successful.");
// ... use the tunnel ...
boolean waitForThreadExit = true;
success = tunnel.CloseTunnel(waitForThreadExit);
if (success == false) {
Log.i(TAG, tunnel.lastErrorText());
return;
}
}
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."
}
}