EldoS | Feel safer!

Software components for data protection, secure storage and transfer

How to use SFTP Public key authentication using C++

Also by EldoS: MsgConnect
Cross-platform protocol-independent communication framework for building peer-to-peer and client-server applications and middleware components.
#35649
Posted: 01/22/2016 00:28:32
by Chen Qian (Basic support level)
Joined: 01/22/2016
Posts: 3

How to use SFTP Public key authentication using C++, I can only find the Password authentication in the example. Does anyone share the code? thank you very much.
#35664
Posted: 01/23/2016 06:32:50
by Eugene Mayevski (EldoS Corp.)

While we don't have C++ code for this, I am including the Java code, which is very similar and can easily be translated to C++:

Code
   private TElSSHMemoryKeyStorage keyStorage;

   keyStorage.clear();
         TElSSHKey Key = new TElSSHKey();
            sftpClient.setAuthenticationTypes(SBSSHConstants.SSH_AUTH_TYPE_PASSWORD | SBSSHConstants.SSH_AUTH_TYPE_KEYBOARD);


         if (dlg.getPrivateKey().compareTo("") != 0)
         {
            int err = Key.loadPrivateKey(dlg.getPrivateKey(), dlg.getPrivateKeyPassword());
            if (err == 0)
            {
               keyStorage.add(Key);
               sftpClient.setAuthenticationTypes(sftpClient.getAuthenticationTypes() | SBSSHConstants.SSH_AUTH_TYPE_PUBLICKEY);
            }
            else
            {
               sftpClient.setAuthenticationTypes(sftpClient.getAuthenticationTypes() & (~SBSSHConstants.SSH_AUTH_TYPE_PUBLICKEY));
               log("Private key file could not be loaded due to error "  + err, true);
            }
         }
         else
         {
            sftpClient.setAuthenticationTypes(sftpClient.getAuthenticationTypes() & (~SBSSHConstants.SSH_AUTH_TYPE_PUBLICKEY));
         }
         


Sincerely yours
Eugene Mayevski
#35669
Posted: 01/25/2016 04:14:40
by Chen Qian (Basic support level)
Joined: 01/22/2016
Posts: 3

Thank you very much, i modified to C++ code, it works, share to everybody.

Code
TElSimpleSFTPClient Client(NULL);
Client.set_Address(paramAddress);
Client.set_Port(paramPort);

Client.set_Username(paramUsername);
Client.set_Password(paramPassword);

Client.set_OnKeyValidate(&Client_OnKeyValidate, NULL);
//    Client.set_AuthenticationTypes(SB_SSH_AUTH_TYPE_PASSWORD);
    
Client.set_AuthenticationTypes(SB_SSH_AUTH_TYPE_PUBLICKEY);
    
    TElSSHKey * key = new TElSSHKey();
    key->set_KeyFormat(kfPuTTY);
    int r = key->LoadPrivateKey("location of private key","password");

    TElSSHMemoryKeyStorage *KeyStorage = new TElSSHMemoryKeyStorage(&Client);
    KeyStorage->Clear();
    KeyStorage->Add(key);
    
    Client.set_KeyStorage(KeyStorage);
    
   try
   {
      std::cout << "Connecting " << paramAddress << ":" << paramPort << std::endl;
      Client.Open();
   }
#35670
Posted: 01/25/2016 04:25:27
by Vsevolod Ievgiienko (EldoS Corp.)

Thank you for sharing the code.
#35671
Posted: 01/25/2016 04:35:38
by Eugene Mayevski (EldoS Corp.)

You need to take care of proper disposal of objects after they are used. Eg. TElSSHKey object can be deleted immediately after being added to the keystorage. the KeyStorage object, though, will be used by the Client and should be deleted when the Client is deleted.


Sincerely yours
Eugene Mayevski
#35690
Posted: 01/25/2016 22:24:12
by Chen Qian (Basic support level)
Joined: 01/22/2016
Posts: 3

Well, so nice you are, thank you for reminding. :)
Also by EldoS: BizCrypto
Components for BizTalk® and SQL Server® Integration Services that let you securely store and transfer information in your business automation solutions.

Reply

Statistics

Topic viewed 1928 times

Number of guests: 1, registered members: 0, in total hidden: 0




|

Back to top

As of July 15, 2016 EldoS Corporation will operate as a division of /n software inc. For more information, please read the announcement.

Got it!