Xbase++
Xbase++
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 Xbase++ Downloads
LOCAL nSuccess
LOCAL oJson
LOCAL oCrypt
LOCAL oSsLogin
LOCAL oSsPassword
LOCAL oSsh
LOCAL nPort
nSuccess := 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=="
// }
oJson := CreateObject("Chilkat.JsonObject")
nSuccess := oJson:LoadFile("qa_data/passwords/ssh.json")
IF (nSuccess == 0)
? oJson:LastErrorText
oJson:destroy()
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.
oCrypt := CreateObject("Chilkat.Crypt2")
oCrypt:CryptAlgorithm := "aes"
oCrypt:CipherMode := "cbc"
oCrypt:KeyLength := 128
oCrypt:SetEncodedKey("000102030405060708090A0B0C0D0E0F", "hex")
oCrypt:SetEncodedIV("000102030405060708090A0B0C0D0E0F", "hex")
oCrypt:EncodingMode := "base64"
// Decrypt directly into SecureString objects. The values remain encrypted in memory, now
// under a randomly generated session key.
oSsLogin := CreateObject("Chilkat.SecureString")
oSsPassword := CreateObject("Chilkat.SecureString")
oCrypt:DecryptSecureENC(oJson:StringOf("ssh_login"), oSsLogin)
oCrypt:DecryptSecureENC(oJson:StringOf("ssh_password"), oSsPassword)
oSsh := CreateObject("Chilkat.Ssh")
nPort := 22
nSuccess := oSsh:Connect("ssh.example.com", nPort)
IF (nSuccess == 0)
? oSsh:LastErrorText
oJson:destroy()
oCrypt:destroy()
oSsLogin:destroy()
oSsPassword:destroy()
oSsh:destroy()
RETURN
ENDIF
// Authenticate using the secure strings.
nSuccess := oSsh:AuthenticateSecPw(oSsLogin, oSsPassword)
IF (nSuccess == 0)
? oSsh:LastErrorText
oJson:destroy()
oCrypt:destroy()
oSsLogin:destroy()
oSsPassword:destroy()
oSsh:destroy()
RETURN
ENDIF
? "SSH authentication successful."
oSsh:Disconnect()
oJson:destroy()
oCrypt:destroy()
oSsLogin:destroy()
oSsPassword:destroy()
oSsh:destroy()