Sample code for 30+ languages & platforms
Delphi ActiveX Requires Chilkat v11.0.0+

SFTP Authentication using an SSH Certificate

See more SFTP Examples

Demonstrates how to SFTP authenticate using an SSH certificate.

Chilkat Delphi ActiveX Downloads

Delphi ActiveX
var
success: Integer;
sbSshCert: TChilkatStringBuilder;
sbPrivKey: TChilkatStringBuilder;
key: TChilkatSshKey;
sftp: TChilkatSFtp;
hostname: WideString;
port: Integer;

begin
success := 0;

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

sbSshCert := TChilkatStringBuilder.Create(Self);
success := sbSshCert.LoadFile('qa_data/sshCert/user_ecdsa_key-cert.pub','utf-8');
if (success = 0) then
  begin
    Memo1.Lines.Add('Failed to load user_ecdsa_key-cert.pub');
    Exit;
  end;

sbPrivKey := TChilkatStringBuilder.Create(Self);
success := sbPrivKey.LoadFile('qa_data/sshKeys/user_ecdsa_key','utf-8');
if (success = 0) then
  begin
    Memo1.Lines.Add('Failed to load user_ecdsa_key');
    Exit;
  end;

key := TChilkatSshKey.Create(Self);
//  Provide the password if the user_ecdsa_key is stored in an encrypted format.
key.Password := 'secret';
success := key.FromOpenSshPrivateKey(sbPrivKey.GetAsString());
if (success = 0) then
  begin
    Memo1.Lines.Add(key.LastErrorText);
    Exit;
  end;

//  Indicate that the SSH certificate is to be used for authentication.
//  The UseSshCertificate method was added in Chilkat v11.0.0
key.UseSshCertificate(sbSshCert.GetAsString());

sftp := TChilkatSFtp.Create(Self);

hostname := 'sftp.example.com';
port := 22;
success := sftp.Connect(hostname,port);
if (success <> 1) then
  begin
    Memo1.Lines.Add(sftp.LastErrorText);
    Exit;
  end;

success := sftp.AuthenticatePk('myLogin',key.ControlInterface);
if (success <> 1) then
  begin
    Memo1.Lines.Add(sftp.LastErrorText);
    Exit;
  end;

Memo1.Lines.Add('Public-Key Authentication using an SSH Certificate was Successful!');