Sample code for 30+ languages & platforms
Lianja

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 Lianja Downloads

Lianja
llSuccess = .F.

//  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=="
//  }
loJson = createobject("CkJsonObject")
llSuccess = loJson.LoadFile("qa_data/passwords/ssh.json")
if (llSuccess = .F.) then
    ? loJson.LastErrorText
    release loJson
    return
endif

//  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.
loCrypt = createobject("CkCrypt2")
loCrypt.CryptAlgorithm = "aes"
loCrypt.CipherMode = "cbc"
loCrypt.KeyLength = 128
loCrypt.SetEncodedKey("000102030405060708090A0B0C0D0E0F","hex")
loCrypt.SetEncodedIV("000102030405060708090A0B0C0D0E0F","hex")
loCrypt.EncodingMode = "base64"

//  Decrypt directly into SecureString objects.  The values stay encrypted in memory, now under
//  a randomly generated session key.
loSsLogin = createobject("CkSecureString")
loSsPassword = createobject("CkSecureString")
loCrypt.DecryptSecureENC(loJson.StringOf("ssh_login"),loSsLogin)
loCrypt.DecryptSecureENC(loJson.StringOf("ssh_password"),loSsPassword)

loTunnel = createobject("CkSshTunnel")

lnPort = 22
llSuccess = loTunnel.Connect("ssh.example.com",lnPort)
if (llSuccess = .F.) then
    ? loTunnel.LastErrorText
    release loJson
    release loCrypt
    release loSsLogin
    release loSsPassword
    release loTunnel
    return
endif

//  Authenticate using the secure strings.
llSuccess = loTunnel.AuthenticateSecPw(loSsLogin,loSsPassword)
if (llSuccess = .F.) then
    ? loTunnel.LastErrorText
    release loJson
    release loCrypt
    release loSsLogin
    release loSsPassword
    release loTunnel
    return
endif

? "SSH tunnel authentication successful."

//  ... use the tunnel ...

llWaitForThreadExit = .T.
llSuccess = loTunnel.CloseTunnel(llWaitForThreadExit)
if (llSuccess = .F.) then
    ? loTunnel.LastErrorText
    release loJson
    release loCrypt
    release loSsLogin
    release loSsPassword
    release loTunnel
    return
endif



release loJson
release loCrypt
release loSsLogin
release loSsPassword
release loTunnel