How to establish a simple ssh connection with c++
Asked Answered
R

2

10

I am trying to make a c++ program which will connect to an ssh server (my laptop). The server is ok because I can get connected via putty. Although the program I wrote so far can not. In my code I am using the library libssh.h and for development I used visual studio 2015. The error I get is: crypt_set_algorithms2: no crypto algorithm function found for 3des-cbc I haven't found anything so far so I hope to your help. The code I use:

#include <libssh/libssh.h>
#include <stdlib.h>
#include <stdio.h> 
int main()
{
    ssh_session my_ssh_session;
    int rc;
    int port = 22;
    int verbosity = SSH_LOG_PROTOCOL;
    char *password;
    // Open session and set options
    my_ssh_session = ssh_new();
    if (my_ssh_session == NULL)
        exit(-1);
    ssh_options_set(my_ssh_session, SSH_OPTIONS_HOST, "192.168.1.6");
    ssh_options_set(my_ssh_session, SSH_OPTIONS_USER, "john");
    ssh_options_set(my_ssh_session, SSH_OPTIONS_LOG_VERBOSITY, &verbosity);
    ssh_options_set(my_ssh_session, SSH_OPTIONS_CIPHERS_C_S,"aes128-ctr");

    //ssh_options_set(my_ssh_session, SSH_OPTIONS_PORT, &port);
    // Connect to server
    rc = ssh_connect(my_ssh_session);
    if (rc != SSH_OK)  
    {
        fprintf(stderr, "Error: %s\n", ssh_get_error(my_ssh_session)); //HERE IS WHERE I GET THE ERROR 
        ssh_free(my_ssh_session);
        exit(-1);
    }
    // Verify the server's identity
    // For the source code of verify_knowhost(), check previous example
/*  if (verify_knownhost(my_ssh_session) < 0)
    {
        ssh_disconnect(my_ssh_session);
        ssh_free(my_ssh_session);
        exit(-1);
    }
*/  
    // Authenticate ourselves
    password = "pass";
    rc = ssh_userauth_password(my_ssh_session, NULL, password);
    if (rc != SSH_AUTH_SUCCESS)
    {
        fprintf(stderr, "Error authenticating with password: %s\n",
            ssh_get_error(my_ssh_session));
        ssh_disconnect(my_ssh_session);
        ssh_free(my_ssh_session);
        exit(-1);
    }


        ssh_disconnect(my_ssh_session);
    ssh_free(my_ssh_session);
}
Ronironica answered 18/11, 2015 at 19:58 Comment(0)
C
6

Try to use different cipher. 3des-cbc is broken and probably disabled on your server already.

There is really nice tutorial with simple session.

Removing the line makes it working for me on Ubuntu (don't know where you found it):

ssh_options_set(my_ssh_session, SSH_OPTIONS_CIPHERS_C_S,"aes128-ctr");

If not, what version of libssh are you using? Isn't it some obsolete one?

Coxalgia answered 18/11, 2015 at 20:30 Comment(2)
Thank you for your answer. Firstly I removed the line you mentioned but nothing happened. In my project I used the libssh 0.7.1. After your answer I tried the version 0.7.0 but I got an error that msvcr120d.dll is missing. After I downloaded and pasted it in my project path I got an error that the application could not start properly.Ronironica
I found I used some Ubuntu-ancient libssh-6.3, so this will probably not be a problem. So the question is what is the server on the other side. Are you able to connect there with some recent openssh?Coxalgia
R
2

Finally got it! I removed the line

ssh_options_set(my_ssh_session, SSH_OPTIONS_CIPHERS_C_S,"aes128-ctr");

Then I used the ssh.dll from libssh v0-7.2 instead of the ssh.dll of v0-7.1 I used before. The ssh.dll is located in libssh-0.7.2\bin. If you take an error like the msvcr120d.dll is missing try to change from debug to release in visual studio.

Ronironica answered 22/11, 2015 at 17:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.