Sample code for 30+ languages & platforms
Unicode C

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 Unicode C Downloads

Unicode C
#include <C_CkFtp2W.h>
#include <C_CkSecureStringW.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkFtp2W ftp;
    const wchar_t *password;
    HCkSecureStringW securePassword;

    success = FALSE;

    //  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.

    ftp = CkFtp2W_Create();

    CkFtp2W_putHostname(ftp,L"ftp.example.com");
    CkFtp2W_putUsername(ftp,L"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.
    password = L"myPassword";

    //  Place the password into a SecureString, which keeps it encrypted in memory.
    securePassword = CkSecureStringW_Create();
    CkSecureStringW_Append(securePassword,password);

    //  Setting the secure password is equivalent to setting the Password property, but more
    //  protective.
    success = CkFtp2W_SetSecurePassword(ftp,securePassword);
    if (success == FALSE) {
        wprintf(L"%s\n",CkFtp2W_lastErrorText(ftp));
        CkFtp2W_Dispose(ftp);
        CkSecureStringW_Dispose(securePassword);
        return;
    }

    success = CkFtp2W_Connect(ftp);
    if (success == FALSE) {
        wprintf(L"%s\n",CkFtp2W_lastErrorText(ftp));
        CkFtp2W_Dispose(ftp);
        CkSecureStringW_Dispose(securePassword);
        return;
    }

    wprintf(L"Connected using a secure password.\n");

    success = CkFtp2W_Disconnect(ftp);
    if (success == FALSE) {
        wprintf(L"%s\n",CkFtp2W_lastErrorText(ftp));
        CkFtp2W_Dispose(ftp);
        CkSecureStringW_Dispose(securePassword);
        return;
    }



    CkFtp2W_Dispose(ftp);
    CkSecureStringW_Dispose(securePassword);

    }