Unicode C++
Unicode C++
SFTP Authentication using an SSH Certificate
See more SFTP Examples
Demonstrates how to SFTP authenticate using an SSH certificate.Chilkat Unicode C++ Downloads
#include <CkStringBuilderW.h>
#include <CkSshKeyW.h>
#include <CkSFtpW.h>
void ChilkatSample(void)
{
bool success = false;
// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
CkStringBuilderW sbSshCert;
success = sbSshCert.LoadFile(L"qa_data/sshCert/user_ecdsa_key-cert.pub",L"utf-8");
if (success == false) {
wprintf(L"Failed to load user_ecdsa_key-cert.pub\n");
return;
}
CkStringBuilderW sbPrivKey;
success = sbPrivKey.LoadFile(L"qa_data/sshKeys/user_ecdsa_key",L"utf-8");
if (success == false) {
wprintf(L"Failed to load user_ecdsa_key\n");
return;
}
CkSshKeyW key;
// Provide the password if the user_ecdsa_key is stored in an encrypted format.
key.put_Password(L"secret");
success = key.FromOpenSshPrivateKey(sbPrivKey.getAsString());
if (success == false) {
wprintf(L"%s\n",key.lastErrorText());
return;
}
// 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());
CkSFtpW sftp;
const wchar_t *hostname = L"sftp.example.com";
int port = 22;
success = sftp.Connect(hostname,port);
if (success != true) {
wprintf(L"%s\n",sftp.lastErrorText());
return;
}
success = sftp.AuthenticatePk(L"myLogin",key);
if (success != true) {
wprintf(L"%s\n",sftp.lastErrorText());
return;
}
wprintf(L"Public-Key Authentication using an SSH Certificate was Successful!\n");
}