Sample code for 30+ languages & platforms
DataFlex

Set the FTP Password from a SecureString

See more FTP Examples

Demonstrates the Chilkat Ftp2.SetSecurePassword method, which sets the login password from a SecureString. The only argument is the SecureString. It is equivalent to setting the Password property but avoids holding the password as an ordinary immutable string.

Background: An ordinary string password can linger in memory (and in memory dumps) because strings are immutable and copied freely. A SecureString keeps the value encrypted under a session key and clears it deterministically, reducing that exposure — a sensible upgrade for a credential the process holds for the life of the connection. Populate it from a runtime source rather than a hard-coded literal.

Chilkat DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoFtp
    String sPassword
    Variant vSecurePassword
    Handle hoSecurePassword
    String sTemp1

    Move False To iSuccess

    //  Demonstrates the Ftp2.SetSecurePassword method, which sets the login password from a
    //  SecureString.  The only argument is the SecureString.  This avoids holding the password as an
    //  ordinary immutable string.

    Get Create (RefClass(cComChilkatFtp2)) To hoFtp
    If (Not(IsComObjectCreated(hoFtp))) Begin
        Send CreateComObject of hoFtp
    End

    Set ComHostname Of hoFtp To "ftp.example.com"
    Set ComUsername Of hoFtp To "myFtpLogin"

    //  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.
    Move "myPassword" To sPassword

    //  Place the password into a SecureString, which keeps it encrypted in memory.
    Get Create (RefClass(cComChilkatSecureString)) To hoSecurePassword
    If (Not(IsComObjectCreated(hoSecurePassword))) Begin
        Send CreateComObject of hoSecurePassword
    End
    Get ComAppend Of hoSecurePassword sPassword To iSuccess

    //  Setting the secure password is equivalent to setting the Password property, but more
    //  protective.
    Get pvComObject of hoSecurePassword to vSecurePassword
    Get ComSetSecurePassword Of hoFtp vSecurePassword To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoFtp To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComConnect Of hoFtp To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoFtp To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Showln "Connected using a secure password."

    Get ComDisconnect Of hoFtp To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoFtp To sTemp1
        Showln sTemp1
        Procedure_Return
    End



End_Procedure