0

Here my main aim is to exchange public key at the client and server . Then use them to send message to each other by encrypting and the other decrypts using its private key. Now I have generated rsa keypair in c at both client and server and got the public and private keys in string format,exchanged them but i don't know how to use them to encrypt the message. please help. this is my client code:

#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <time.h>

#include <openssl/rsa.h>
#include <openssl/pem.h>
#include <openssl/err.h>

#define KEY_LENGTH  2048
#define PUB_EXP     3
#define PRINT_KEYS
#define WRITE_TO_FILE

int main(int argc, char *argv[])
{
struct sockaddr_in serv;
int sockfd,newsockfd,portno=3000,test_v;
char buffer_c[4096],publickey[4096];

size_t pri_len;            // Length of private key
size_t pub_len;            // Length of public key
char   *pri_key;           // Private key
char   *pub_key;           // Public key
char   msg[KEY_LENGTH/8];  // Message to encrypt
char   *encrypt = NULL;    // Encrypted message
char   *decrypt = NULL;    // Decrypted message
char   *err;               // Buffer for any error messages
char   *pub2;   

// Generate key pair
printf("Generating RSA (%d bits) keypair...", KEY_LENGTH);
fflush(stdout);
RSA *keypair = RSA_generate_key(KEY_LENGTH, PUB_EXP, NULL, NULL);

// To get the C-string PEM form:
BIO *pri = BIO_new(BIO_s_mem());
BIO *pub = BIO_new(BIO_s_mem());

PEM_write_bio_RSAPrivateKey(pri, keypair, NULL, NULL, 0, NULL, NULL);
PEM_write_bio_RSAPublicKey(pub, keypair);

pri_len = BIO_pending(pri);
pub_len = BIO_pending(pub);

pri_key = malloc(pri_len + 1);
pub_key = malloc(pub_len + 1);

BIO_read(pri, pri_key, pri_len);
BIO_read(pub, pub_key, pub_len);

pri_key[pri_len] = '\0';
pub_key[pub_len] = '\0';

#ifdef PRINT_KEYS
    printf("\nCLIENTPRIVATEKEY:\N%s\n", pri_key);
#endif
printf("done.\n");

if((sockfd=socket(AF_INET,SOCK_STREAM,0))<0)
{
    printf("socket not created\n");
    exit(0);
}
else
    printf("socket created\n");

memset(&serv,'0',sizeof(serv));
memset(&buffer_c,'0',sizeof(buffer_c));

serv.sin_family=AF_INET;
serv.sin_addr.s_addr=htonl(INADDR_ANY);
serv.sin_port=htons(portno);


if(connect(sockfd,(struct sockaddr*)&serv,sizeof(serv))<0)
{
    printf("not connected.\n");
    exit(0);
}
else
    printf("connected\n");

//send the pub-key
strcpy(buffer_c,pub_key);
test_v=send(sockfd,buffer_c,strlen(buffer_c),0);
if(test_v<0)
{
    printf("error sending.\n");
    exit(0);
}
else
{
    printf("client sent pub-key\n");
}

test_v=recv(sockfd,publickey,4095,0);
if(test_v<0)
{
    printf("error recving.\n");
    exit(0);
}
else
{
    publickey[test_v]='\0';
    printf("server pub-key:\n%s",publickey);

    * pub2 = publickey; 
}

 // Get the message to encrypt
    printf("Message to encrypt: ");
    fgets(msg, KEY_LENGTH-1, stdin);
    msg[strlen(msg)-1] = '\0';

// Encrypt the message
    encrypt = malloc(RSA_size(keypair));
    int encrypt_len;
    err = malloc(130);
    if((encrypt_len = RSA_public_encrypt(strlen(msg), (unsigned char*)msg, (unsigned char*)encrypt,keypair,RSA_PKCS1_OAEP_PADDING))==-1)        {
    ERR_load_crypto_strings();
    ERR_error_string(ERR_get_error(), err);
    fprintf(stderr, "Error encrypting message: %s\n", err);
    goto free_stuff;
    }   

close(sockfd);

    free_stuff:
    RSA_free(keypair);
    BIO_free_all(pub);
    BIO_free_all(pri);
    free(pri_key);
    free(pub_key);
    free(encrypt);
    free(decrypt);
    free(err);
return 0;

}

3
  • Use openssl... And look at examples Commented Nov 8, 2013 at 17:46
  • 1
    Once you have some actual code, ask about the code, and show what you have tried. SO is not really for "give me some vague hints" or "please write some code for me" or "find some links for me" kind of questions. Commented Nov 8, 2013 at 17:51
  • what i want is to know any possible functions which can help me use the sent public key encrypt the message Commented Nov 8, 2013 at 18:12

1 Answer 1

0

You actually don't use the public key to cipher the message, but rather to cipher a new random key which will be used to cipher the message using a symmetric key algorithm, such as AES, due to the high complexity involved using asymmetric key encryption algorithms. Of course you can do it, if you want...

In order to cipher it, you have to perform some module arithmetic calculation:

enc_message = message^public_key mod product_of_2_chosen_primes

And to decode it:

message = enc_message^private_key mod product_of_2_chosen_primes

Take a look at RSA. Btw, since you are dealing with extremely large numbers, you will have to take a look at some Big Number library

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.