Sample code for 30+ languages & platforms
PHP 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 PHP Downloads

Chilkat2 PHP
<?php

use Chilkat2\SFtp;
use Chilkat2\SshKey;
use Chilkat2\StringBuilder;

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

$sbSshCert = new StringBuilder();
try {
    $sbSshCert->LoadFile('qa_data/sshCert/user_ecdsa_key-cert.pub', 'utf-8');
} catch (\Chilkat2\Exception $e) {
    echo "Failed to load user_ecdsa_key-cert.pub\n";
    exit;
}

$sbPrivKey = new StringBuilder();
try {
    $sbPrivKey->LoadFile('qa_data/sshKeys/user_ecdsa_key', 'utf-8');
} catch (\Chilkat2\Exception $e) {
    echo "Failed to load user_ecdsa_key\n";
    exit;
}

$key = new SshKey();
// Provide the password if the user_ecdsa_key is stored in an encrypted format.
$key->Password = 'secret';
try {
    $key->FromOpenSshPrivateKey($sbPrivKey->GetAsString());

    // 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 = new SFtp();

    $hostname = 'sftp.example.com';
    $port = 22;
    $sftp->Connect($hostname, $port);

    $sftp->AuthenticatePk('myLogin', $key);
} catch (\Chilkat2\Exception $e) {
    echo $e->getMessage(), "\n";
    exit;
}

echo "Public-Key Authentication using an SSH Certificate was Successful!\n";