B4X
B4X
SFTP Authentication using X.509 Certificates
See more SFTP Examples
Demonstrates how to authenticate with an SSH/SFTP server using an certificate's private key.Note: See X.509v3 Certificates for SSH Authentication for more information.
Chilkat B4X Downloads
Dim success As Boolean = False
' This example assumes the Chilkat API to have been previously unlocked.
' See Global Unlock Sample for sample code.
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
' Load the cert + private key from a .pfx.
' Note: Chilkat provides methods for loading certs and private keys from many sources, including smart cards and USB tokens (HSM's)
Dim cert As ChilkatCert
cert.Initialize("cert")
success = cert.LoadPfxFile("qa_data/pfx/example.pfx", "pfx_password")
If success <> True Then
Log(cert.LastErrorText)
Return
End If
' Get the cert's private key (as PEM) to be used for SSH authentication.
' (The public key is installed on the server.)
Dim privKeyPem As String = cert.GetPrivateKeyPem
If cert.LastMethodSuccess = False Then
Log(cert.LastErrorText)
Return
End If
Dim key As ChilkatSshKey
key.Initialize
' Load a private key from a PEM string:
success = key.FromOpenSshPrivateKey(privKeyPem)
If success <> True Then
Log(key.LastErrorText)
Return
End If
' Authenticate with the SSH server.
success = sftp.AuthenticatePk("myLogin", key)
If success <> True Then
Log(sftp.LastErrorText)
Return
End If
Log("Public-Key Authentication Successful!")