#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    14
    Rep Power
    0

    RSA algorithm in C


    Hi All,

    I have a requirement to do RSA encryption of password in C. I found a piece of code from the link below.

    shanetully.com/2012/04/simple-public-key-encryption-with-rsa-and-openssl/

    It works fine but the decrypted text has junk characters at the end.

    Could someone help me in resolving this issue?

    Thanks in advance.
    Vaishnavi
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    187
    Rep Power
    83
    The OpenSSl function RSA_private_decrypt() returns the sizeof the recovered text. Thus, you can do something like the following:

    Code:
    int iRet = 0;
    iRet =   RSA_private_decrypt(encrypt_len, (unsigned char*)encrypt, (unsigned char*)decrypt, keypair, RSA_PKCS1_OAEP_PADDING);
        if(iRet == -1)
    	{
            ERR_load_crypto_strings();
            ERR_error_string(ERR_get_error(), err);
            fprintf(stderr, "Error decrypting message: %s\n", err);
            goto free_stuff;
        }
    	else decrypt[iRet] = '\0';
            printf("%s\n", decrypt);
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    14
    Rep Power
    0
    Thanks a lot Bob!!! :)

    It helped and now I don't get any junk characters :)
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    14
    Rep Power
    0
    I have another issue.

    I am using this application to send an encrypted message from a client to a server through TCP. I am not much aware of RSA and I have trouble in sharing the key between the client and the server.

    Could someone please help me with this? If any other proven and working code is available, please share the link. It would be much helpful to me.

    Thanks in advance,
    Vaishnavi
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    187
    Rep Power
    83
    Originally Posted by vaishnavi89
    I have another issue.

    I am using this application to send an encrypted message from a client to a server through TCP. I am not much aware of RSA and I have trouble in sharing the key between the client and the server.

    Could someone please help me with this? If any other proven and working code is available, please share the link. It would be much helpful to me.

    Thanks in advance,
    Vaishnavi
    Here's a basic Linux example. It can easily be ported to Windows.
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    14
    Rep Power
    0
    I have already tried this code. It establishes a secure connection. But I have already implemented PAM based authentication. The problem in my application is, the client sends the username and password as plain text to the server. I need to encrypt these credentials alone. I want a code to perform this text/string encryption and decryption using RSA.
  12. #7
  13. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,645
    Rep Power
    4248
    Then you didn't properly understand the example link posted. The client code creates a secure socket using a public cert shared by the server end. Then it sends its data over the encrypted socket link. So you can use that to send a user name and password securely to the other end and then authenticate using PAM or whatever.
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    14
    Rep Power
    0
    Thank you for pointing that out. I used Ubuntu 10.04 earlier and I compiled that code (from the link posted by Bob) successfully.
    Because of some issues I am using Ubuntu 12.04 now. If I compile the same code now (in Ubuntu 12.04), it throws the following errors in the server and the client applications respectively.
    "undefined reference to 'SSLv2_server_method' "
    "undefined reference to 'SSLv2_client_method' "

    I use the below command for compilation.
    gcc -Wall ssl_server.c -lssl -lcrypto -o ssl_server

    What should I do to resolve this error?
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    14
    Rep Power
    0
    I have resolved the above errors by using SSLv3.

    Thanks to all for your support and guidance.
  18. #10
  19. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    14
    Rep Power
    0
    I have to make the client send messages to the server continuously at fixed intervals and the the server should send appropriate acknowledgement. I tried implementing this in the code from this link.

    http://simplestcodings.blogspot.in/2...nssl-in-c.html

    But my attempts were not successful. Could someone please help me in doing this?
  20. #11
  21. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    14
    Rep Power
    0
    Hi friends,

    Could someone help me with my requirement?

    Any appropriate solution would be of great help!!!

    Thanks.
  22. #12
  23. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,645
    Rep Power
    4248
    Originally Posted by vaishnavi89
    I have to make the client send messages to the server continuously at fixed intervals and the the server should send appropriate acknowledgement. I tried implementing this in the code from this link.

    http://simplestcodings.blogspot.in/2...nssl-in-c.html

    But my attempts were not successful. Could someone please help me in doing this?
    Define "not successful". What is failing in your code. Also, did you modify something from the code on that page or what? We aren't mind readers here.
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
  24. #13
  25. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    14
    Rep Power
    0
    I am not able to send even two messages continuously. My approach may not be right. But I am posting the change I made to the code. This piece of code comes at the end of the client code.

    msg = "Hello???";
    printf("Connected with %s encryption\n", SSL_get_cipher(ssl));
    ShowCerts(ssl); /* get any certs */
    SSL_write(ssl, msg, strlen(msg)); /* encrypt & send message */
    bytes = SSL_read(ssl, buf, sizeof(buf)); /* get reply & decrypt */
    buf[bytes] = 0;
    printf("Received: \"%s\"\n", buf);
    sleep(3);
    msg = "Hello!!!!";
    printf("Connected with %s encryption\n", SSL_get_cipher(ssl));
    ShowCerts(ssl); /* get any certs */
    SSL_write(ssl, msg, strlen(msg)); /* encrypt & send message */
    bytes = SSL_read(ssl, buf, sizeof(buf)); /* get reply & decrypt */
    buf[bytes] = 0;
    printf("Received: \"%s\"\n", buf);


    I tried without the 'sleep' also. But I do not get the second message at the server side. I did not make any changes to the server, since it waits for messages after reading a message and sending ACK (this is my understanding of the code, correct me if I am wrong).

    Please help me how to make the client send messages continuously to the server.

IMN logo majestic logo threadwatch logo seochat tools logo