Sample code for 30+ languages & platforms
PowerBuilder

SFTP Authenticate with a SecureString Password

See more SFTP Examples

Demonstrates the Chilkat SFtp.AuthenticateSecPw method, which authenticates using SecureString login and password objects. The first argument is the login and the second is the password.

Background: A SecureString keeps the credential encrypted in memory, reducing the chance of a plaintext password lingering in the process. The behavior otherwise matches AuthenticatePw; only the credential handling is more protective. Populate the SecureString from a value obtained at runtime, not a hard-coded literal.

Chilkat PowerBuilder Downloads

PowerBuilder
integer li_rc
integer li_Success
oleobject loo_Sftp
integer li_Port
string ls_Password
oleobject loo_SecureLogin
oleobject loo_SecurePassword

li_Success = 0

//  Demonstrates the SFtp.AuthenticateSecPw method, which authenticates using SecureString login
//  and password objects.  The 1st argument is the login and the 2nd is the password.

loo_Sftp = create oleobject
li_rc = loo_Sftp.ConnectToNewObject("Chilkat.SFtp")
if li_rc < 0 then
    destroy loo_Sftp
    MessageBox("Error","Connecting to COM object failed")
    return
end if

//  Step 1: Connect the SSH transport.  Port 22 is the usual port.
li_Port = 22
li_Success = loo_Sftp.Connect("sftp.example.com",li_Port)
if li_Success = 0 then
    Write-Debug loo_Sftp.LastErrorText
    destroy loo_Sftp
    return
end if

//  Normally you would not hard-code the password in source.  You should instead obtain it
//  from an interactive prompt, environment variable, or a secrets vault.
ls_Password = "mySshPassword"

//  Place the login and password into SecureString objects.
loo_SecureLogin = create oleobject
li_rc = loo_SecureLogin.ConnectToNewObject("Chilkat.SecureString")

loo_SecureLogin.Append("mySshLogin")
loo_SecurePassword = create oleobject
li_rc = loo_SecurePassword.ConnectToNewObject("Chilkat.SecureString")

loo_SecurePassword.Append(ls_Password)

li_Success = loo_Sftp.AuthenticateSecPw(loo_SecureLogin,loo_SecurePassword)
if li_Success = 0 then
    Write-Debug loo_Sftp.LastErrorText
    destroy loo_Sftp
    destroy loo_SecureLogin
    destroy loo_SecurePassword
    return
end if

//  Step 3: Open the SFTP subsystem.  This must be done after authentication and before any
//  file or directory operations.
li_Success = loo_Sftp.InitializeSftp()
if li_Success = 0 then
    Write-Debug loo_Sftp.LastErrorText
    destroy loo_Sftp
    destroy loo_SecureLogin
    destroy loo_SecurePassword
    return
end if

Write-Debug "Authenticated with secure strings."

loo_Sftp.Disconnect()


destroy loo_Sftp
destroy loo_SecureLogin
destroy loo_SecurePassword