Sample code for 30+ languages & platforms
Pascal (Lazarus/Delphi)

Use a PuTTY Key for SSH Authentication

See more SSH Examples

Demonstrates authenticating with an SSH server using a username and a PuTTY (.ppk) private key. The key is imported with FromPuttyPrivateKey — setting Password first if it is encrypted — and passed to AuthenticatePk.

Note: The file paths are relative to the application's current working directory. Supply the paths to your own files.

Background: A PuTTY key can be used directly for authentication with no conversion step, which is convenient when the key was generated by PuTTYgen on Windows. Conceptually the private key takes the place of a password while the username identifies the account, and the corresponding public key must already be installed on the server. Convert to OpenSSH format only if some other tool in your pipeline requires PEM.

Chilkat Pascal (Lazarus/Delphi) Downloads

Pascal (Lazarus/Delphi)
program ChilkatDemo;

// Demonstrates using the Chilkat Pascal wrapper via the C bridge DLL.
// Builds as a console application under Lazarus (FPC) or Delphi.

{$IFDEF FPC}
  {$MODE DELPHI}
{$ENDIF}
{$APPTYPE CONSOLE}

uses
  {$IFDEF UNIX}
  cthreads,
  {$ENDIF}
  SysUtils,
  CkDllLoader,
  Chilkat.Ssh,
  Chilkat.SshKey;

// ---------------------------------------------------------------------------

procedure RunDemo;
var
  success: Boolean;
  ssh: TSsh;
  puttyKey: TSshKey;
  ppkText: string;
  sshHostname: string;
  sshPort: Integer;

begin
  success := False;

  //  This example requires the Chilkat API to have been previously unlocked.
  //  See Global Unlock Sample for sample code.

  //  Demonstrates authenticating with an SSH server using a username and a PuTTY (.ppk) private
  //  key.  The private key serves as the credential; the username identifies the account on the
  //  server.

  ssh := TSsh.Create;

  //  LoadText is a convenience method that reads any text file into a string.  It does not itself
  //  load the key.
  puttyKey := TSshKey.Create;
  ppkText := puttyKey.LoadText('qa_data/ppk/putty_private_secret.ppk');
  if (puttyKey.LastMethodSuccess = False) then
    begin
      WriteLn(puttyKey.LastErrorText);
      Exit;
    end;

  //  Set the password before importing an encrypted .ppk.  This should come from a secure source
  //  rather than being hard-coded.
  puttyKey.Password := 'myKeyPassword';

  success := puttyKey.FromPuttyPrivateKey(ppkText);
  if (success = False) then
    begin
      WriteLn(puttyKey.LastErrorText);
      Exit;
    end;

  sshHostname := 'ssh.example.com';
  sshPort := 22;
  success := ssh.Connect(sshHostname,sshPort);
  if (success = False) then
    begin
      WriteLn(ssh.LastErrorText);
      Exit;
    end;

  //  The corresponding public key must already be installed on the SSH server for the account.
  success := ssh.AuthenticatePk('mySshLogin',puttyKey);
  if (success = False) then
    begin
      WriteLn(ssh.LastErrorText);
      Exit;
    end;

  WriteLn('Connection and authentication with the SSH server completed.');

  ssh.Disconnect();


  ssh.Free;
  puttyKey.Free;

end;

// ---------------------------------------------------------------------------

begin

  try
    RunDemo;
  except
    on E: Exception do
      WriteLn('Unhandled exception: ', E.ClassName, ': ', E.Message);
  end;

  WriteLn;
  {$IFDEF MSWINDOWS}
  WriteLn('Press Enter to exit...');
  ReadLn;
  {$ENDIF}
end.