Sample code for 30+ languages & platforms
Tcl

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

Tcl

load ./chilkat.dll

set success 0

#  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=="
#  }
set json [new_CkJsonObject]

set success [CkJsonObject_LoadFile $json "qa_data/passwords/ssh.json"]
if {$success == 0} then {
    puts [CkJsonObject_lastErrorText $json]
    delete_CkJsonObject $json
    exit
}

#  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.
set crypt [new_CkCrypt2]

CkCrypt2_put_CryptAlgorithm $crypt "aes"
CkCrypt2_put_CipherMode $crypt "cbc"
CkCrypt2_put_KeyLength $crypt 128
CkCrypt2_SetEncodedKey $crypt "000102030405060708090A0B0C0D0E0F" "hex"
CkCrypt2_SetEncodedIV $crypt "000102030405060708090A0B0C0D0E0F" "hex"
CkCrypt2_put_EncodingMode $crypt "base64"

#  Decrypt directly into SecureString objects.  The values remain encrypted in memory, now
#  under a randomly generated session key.
set ssLogin [new_CkSecureString]

set ssPassword [new_CkSecureString]

CkCrypt2_DecryptSecureENC $crypt [CkJsonObject_stringOf $json "ssh_login"] $ssLogin
CkCrypt2_DecryptSecureENC $crypt [CkJsonObject_stringOf $json "ssh_password"] $ssPassword

set ssh [new_CkSsh]

set port 22
set success [CkSsh_Connect $ssh "ssh.example.com" $port]
if {$success == 0} then {
    puts [CkSsh_lastErrorText $ssh]
    delete_CkJsonObject $json
    delete_CkCrypt2 $crypt
    delete_CkSecureString $ssLogin
    delete_CkSecureString $ssPassword
    delete_CkSsh $ssh
    exit
}

#  Authenticate using the secure strings.
set success [CkSsh_AuthenticateSecPw $ssh $ssLogin $ssPassword]
if {$success == 0} then {
    puts [CkSsh_lastErrorText $ssh]
    delete_CkJsonObject $json
    delete_CkCrypt2 $crypt
    delete_CkSecureString $ssLogin
    delete_CkSecureString $ssPassword
    delete_CkSsh $ssh
    exit
}

puts "SSH authentication successful."

CkSsh_Disconnect $ssh

delete_CkJsonObject $json
delete_CkCrypt2 $crypt
delete_CkSecureString $ssLogin
delete_CkSecureString $ssPassword
delete_CkSsh $ssh