Sample code for 30+ languages & platforms
PureBasic

SFTP Authenticate with a Private Key

See more SFTP Examples

Demonstrates the Chilkat SFtp.AuthenticatePk method, which authenticates the connected session using public-key authentication. The first argument is the username and the second is an SshKey private key. The matching public key must already be authorized for the account on the server.

Note: The private-key path is relative to the application's current working directory. Supply the path to your own key file.

Background: Public-key authentication is the standard for unattended SFTP transfers: the private key stays on the client, the server trusts the corresponding public key, and no reusable password crosses the wire. The SshKey object imports keys in several formats — this example loads an OpenSSH private key. For an encrypted key, set its Password property before importing.

Chilkat PureBasic Downloads

PureBasic
IncludeFile "CkSshKey.pb"
IncludeFile "CkSFtp.pb"

Procedure ChilkatExample()

    success.i = 0

    ;  Demonstrates the SFtp.AuthenticatePk method, which authenticates the connected session using
    ;  public-key authentication.  The 1st argument is the username and the 2nd is an SshKey private
    ;  key.  The matching public key must already be authorized for the account on the server.

    sftp.i = CkSFtp::ckCreate()
    If sftp.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    ;  Step 1: Connect the SSH transport.  Port 22 is the usual port.
    port.i = 22
    success = CkSFtp::ckConnect(sftp,"sftp.example.com",port)
    If success = 0
        Debug CkSFtp::ckLastErrorText(sftp)
        CkSFtp::ckDispose(sftp)
        ProcedureReturn
    EndIf

    ;  Load the SSH private key.  LoadText reads the file's text, which is then imported.
    privKey.i = CkSshKey::ckCreate()
    If privKey.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    keyText.s = CkSshKey::ckLoadText(privKey,"qa_data/id_rsa")
    If CkSshKey::ckLastMethodSuccess(privKey) = 0
        Debug CkSshKey::ckLastErrorText(privKey)
        CkSFtp::ckDispose(sftp)
        CkSshKey::ckDispose(privKey)
        ProcedureReturn
    EndIf

    success = CkSshKey::ckFromOpenSshPrivateKey(privKey,keyText)
    If success = 0
        Debug CkSshKey::ckLastErrorText(privKey)
        CkSFtp::ckDispose(sftp)
        CkSshKey::ckDispose(privKey)
        ProcedureReturn
    EndIf

    success = CkSFtp::ckAuthenticatePk(sftp,"mySshLogin",privKey)
    If success = 0
        Debug CkSFtp::ckLastErrorText(sftp)
        CkSFtp::ckDispose(sftp)
        CkSshKey::ckDispose(privKey)
        ProcedureReturn
    EndIf

    ;  Step 3: Open the SFTP subsystem.  This must be done after authentication and before any
    ;  file or directory operations.
    success = CkSFtp::ckInitializeSftp(sftp)
    If success = 0
        Debug CkSFtp::ckLastErrorText(sftp)
        CkSFtp::ckDispose(sftp)
        CkSshKey::ckDispose(privKey)
        ProcedureReturn
    EndIf

    Debug "Authenticated with a private key."

    CkSFtp::ckDisconnect(sftp)


    CkSFtp::ckDispose(sftp)
    CkSshKey::ckDispose(privKey)


    ProcedureReturn
EndProcedure