Sample code for 30+ languages & platforms
Java

SSH Authenticate with Secure Strings

See more SSH Examples

Demonstrates SSH 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: This shows the shape of a genuinely careful credential flow: secrets live outside the source in encrypted form, and are decrypted straight into a SecureString so a plaintext copy never sits in an ordinary string. A SecureString keeps the value encrypted in memory under a randomly generated session key. In production the decryption key itself would come from a secure source — a key vault, an OS keystore, or the environment — rather than being a literal as shown here for illustration.

Chilkat Java Downloads

Chilkat2 Java
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.Crypt2;
import com.chilkatsoft.chilkat2.JsonObject;
import com.chilkatsoft.chilkat2.SecureString;
import com.chilkatsoft.chilkat2.Ssh;

public class ChilkatExample {

    public static void main(String[] args) {
        // This example requires the Chilkat API to have been previously unlocked.
        // See Global Unlock Sample for sample code.

        // Demonstrates SSH 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=="
        // }
        JsonObject json = new JsonObject();
        try {
            json.loadFile("qa_data/passwords/ssh.json");
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            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.
        Crypt2 crypt = new Crypt2();
        crypt.setCryptAlgorithm("aes");
        crypt.setCipherMode("cbc");
        crypt.setKeyLength(128);
        crypt.setEncodedKey("000102030405060708090A0B0C0D0E0F", "hex");
        crypt.setEncodedIV("000102030405060708090A0B0C0D0E0F", "hex");
        crypt.setEncodingMode("base64");

        // Decrypt directly into SecureString objects.  The values remain encrypted in memory, now
        // under a randomly generated session key.
        SecureString ssLogin = new SecureString();
        SecureString ssPassword = new SecureString();
        crypt.decryptSecureENC(json.stringOf("ssh_login"), ssLogin);
        crypt.decryptSecureENC(json.stringOf("ssh_password"), ssPassword);

        Ssh ssh = new Ssh();
        int port = 22;
        try {
            ssh.connect("ssh.example.com", port);

            // Authenticate using the secure strings.
            ssh.authenticateSecPw(ssLogin, ssPassword);
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        System.out.println("SSH authentication successful.");

        ssh.disconnect();
    }
}