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

B4X
Dim success As Boolean = False

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

Dim sbSshCert As ChilkatStringBuilder
sbSshCert.Initialize
success = sbSshCert.LoadFile("qa_data/sshCert/user_ecdsa_key-cert.pub", "utf-8")
If success = False Then
    Log("Failed to load user_ecdsa_key-cert.pub")
    Return
End If


Dim sbPrivKey As ChilkatStringBuilder
sbPrivKey.Initialize
success = sbPrivKey.LoadFile("qa_data/sshKeys/user_ecdsa_key", "utf-8")
If success = False Then
    Log("Failed to load user_ecdsa_key")
    Return
End If


Dim key As ChilkatSshKey
key.Initialize
'  Provide the password if the user_ecdsa_key is stored in an encrypted format.
key.Password = "secret"
success = key.FromOpenSshPrivateKey(sbPrivKey.GetAsString)
If success = False Then
    Log(key.LastErrorText)
    Return
End If


'  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)

Dim sftp As ChilkatSFtp
sftp.Initialize("sftp")

Dim hostname As String = "sftp.example.com"
Dim port As Int = 22
success = sftp.Connect(hostname, port)
If success <> True Then
    Log(sftp.LastErrorText)
    Return
End If


success = sftp.AuthenticatePk("myLogin", key)
If success <> True Then
    Log(sftp.LastErrorText)
    Return
End If


Log("Public-Key Authentication using an SSH Certificate was Successful!")