Sample code for 30+ languages & platforms
Delphi DLL

Authenticate an SSH Tunnel with a Private Key

See more Socket/SSL/TLS Examples

Demonstrates Socket.SshAuthenticatePk, which authenticates an SSH tunnel session using a private key loaded into an SshKey object. Call SshOpenTunnel first.

The file path is relative to the application's current working directory. An absolute path may also be used. Supply the path appropriate to your own environment.

Background. The SSH server must have the corresponding public key authorized for the account. If the private key is encrypted, set the SshKey.Password property before loading it.

Chilkat Delphi DLL Downloads

Delphi DLL
var
success: Boolean;
socket: HCkSocket;
sshKey: HCkSshKey;
keyText: PWideChar;

begin
success := False;

socket := CkSocket_Create();

success := CkSocket_SshOpenTunnel(socket,'ssh.example.com',22);
if (success = False) then
  begin
    Memo1.Lines.Add(CkSocket__lastErrorText(socket));
    Exit;
  end;

//  Load the SSH private key.  The file path is relative to the application's current working
//  directory.  If the key is encrypted, set the SshKey.Password property before calling
//  FromOpenSshPrivateKey.
sshKey := CkSshKey_Create();
keyText := CkSshKey__loadText(sshKey,'qa_data/id_ed25519');
if (CkSshKey_getLastMethodSuccess(sshKey) = False) then
  begin
    Memo1.Lines.Add(CkSshKey__lastErrorText(sshKey));
    Exit;
  end;
success := CkSshKey_FromOpenSshPrivateKey(sshKey,keyText);
if (success = False) then
  begin
    Memo1.Lines.Add(CkSshKey__lastErrorText(sshKey));
    Exit;
  end;

//  Authenticate the SSH tunnel session using public-key authentication.  The SSH server must have the
//  corresponding public key authorized for the account.
success := CkSocket_SshAuthenticatePk(socket,'sshUser',sshKey);
if (success = False) then
  begin
    Memo1.Lines.Add(CkSocket__lastErrorText(socket));
    Exit;
  end;
Memo1.Lines.Add('SSH public-key authentication succeeded.');

CkSocket_Dispose(socket);
CkSshKey_Dispose(sshKey);