Delphi DLL
Delphi DLL
SFTP Authentication using an SSH Certificate
See more SFTP Examples
Demonstrates how to SFTP authenticate using an SSH certificate.Chilkat Delphi DLL Downloads
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, SshKey, StringBuilder, SFtp;
...
procedure TForm1.Button1Click(Sender: TObject);
var
success: Boolean;
sbSshCert: HCkStringBuilder;
sbPrivKey: HCkStringBuilder;
key: HCkSshKey;
sftp: HCkSFtp;
hostname: PWideChar;
port: Integer;
begin
success := False;
// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
sbSshCert := CkStringBuilder_Create();
success := CkStringBuilder_LoadFile(sbSshCert,'qa_data/sshCert/user_ecdsa_key-cert.pub','utf-8');
if (success = False) then
begin
Memo1.Lines.Add('Failed to load user_ecdsa_key-cert.pub');
Exit;
end;
sbPrivKey := CkStringBuilder_Create();
success := CkStringBuilder_LoadFile(sbPrivKey,'qa_data/sshKeys/user_ecdsa_key','utf-8');
if (success = False) then
begin
Memo1.Lines.Add('Failed to load user_ecdsa_key');
Exit;
end;
key := CkSshKey_Create();
// Provide the password if the user_ecdsa_key is stored in an encrypted format.
CkSshKey_putPassword(key,'secret');
success := CkSshKey_FromOpenSshPrivateKey(key,CkStringBuilder__getAsString(sbPrivKey));
if (success = False) then
begin
Memo1.Lines.Add(CkSshKey__lastErrorText(key));
Exit;
end;
// Indicate that the SSH certificate is to be used for authentication.
// The UseSshCertificate method was added in Chilkat v11.0.0
CkSshKey_UseSshCertificate(key,CkStringBuilder__getAsString(sbSshCert));
sftp := CkSFtp_Create();
hostname := 'sftp.example.com';
port := 22;
success := CkSFtp_Connect(sftp,hostname,port);
if (success <> True) then
begin
Memo1.Lines.Add(CkSFtp__lastErrorText(sftp));
Exit;
end;
success := CkSFtp_AuthenticatePk(sftp,'myLogin',key);
if (success <> True) then
begin
Memo1.Lines.Add(CkSFtp__lastErrorText(sftp));
Exit;
end;
Memo1.Lines.Add('Public-Key Authentication using an SSH Certificate was Successful!');
CkStringBuilder_Dispose(sbSshCert);
CkStringBuilder_Dispose(sbPrivKey);
CkSshKey_Dispose(key);
CkSFtp_Dispose(sftp);
end;