Question
I have the Client Server ( 2 separate programs in C, a client process and a server process. The client process makes a connection to
I have the Client Server ( 2 separate programs in C, a client process and a server process. The
client process makes a connection to the server, and transmits a RSA encoded message to the server. The
server listens for client communication and when it has received the message, it decodes it and displays the
message on the screeN) codes in C, but need to add the following to it:
TO DO : >break up longer strings into 10 char blocks, encode, transmit, and recombine multiple blocks
on the server side. Note: you should transmit the number of blocks to the server before sending.
The code is listed below for both Client and server:
CLIENT CODE
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
void error(char *msg)
{
perror(msg);
exit(0);
}
int main(int argc, char *argv[])
{
int sockfd, portno, n;
mpz_t rsn, rse, pt, ct;
struct sockaddr_in serv_addr;
struct hostent *server;
char buffer[256], psbuffer[256];
if (argc < 3) {
fprintf(stderr,"usage %s hostname port ", argv[0]);
exit(0);
}
portno = atoi(argv[2]);
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
error("ERROR opening socket");
server = gethostbyname(argv[1]);
if (server == NULL) {
fprintf(stderr,"ERROR, no such host ");
exit(0);
}
bzero((char *) &serv_addr, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
bcopy((char *)server->h_addr,
(char *)&serv_addr.sin_addr.s_addr,
server->h_length);
serv_addr.sin_port = htons(portno);
if (connect(sockfd,(struct sockaddr *)&serv_addr,sizeof(serv_addr)) < 0)
error("ERROR connecting");
printf("Please enter the message: ");
bzero(buffer,256);
fgets(buffer,255,stdin);
mpz_init(pt);
mpz_init(ct);
mpz_init_set_str(rsn, "9516311845790656153499716760847001433441357", 10);
mpz_init_set_str(rse, "65537", 10);
mpz_import(pt, strlen(buffer), 1, 1, 0, 0, buffer);
if (mpz_cmp(pt, rsn) > 0)
abort();
mpz_powm(ct, pt, rse, rsn);
mpz_get_str(psbuffer,10,ct);
gmp_printf("Encoded: %Zd ", ct);
n = write(sockfd,psbuffer,strlen(psbuffer));
if (n < 0)
error("ERROR writing to socket");
bzero(buffer,256);
n = read(sockfd,buffer,255);
if (n < 0)
error("ERROR reading from socket");
printf("%s ",buffer);
close(sockfd);
return 0;
}//end client
SERVER CODE
#include
#include
#include
#include
#include
#include
#include
#include
#include
void error(char *msg)
{
perror(msg);
exit(1);
}
int main(int argc, char *argv[])
{
int sockfd, newsockfd, portno, clilen;
mpz_t rsn, rsd, pt, ct;
char buffer[256], psbuffer[256];
struct sockaddr_in serv_addr, cli_addr;
int n;
if (argc < 2) {
fprintf(stderr,"ERROR, no port provided ");
exit(1);
}
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
error("ERROR opening socket");
bzero((char *) &serv_addr, sizeof(serv_addr));
portno = atoi(argv[1]);
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = INADDR_ANY;
serv_addr.sin_port = htons(portno);
if (bind(sockfd, (struct sockaddr *) &serv_addr,
sizeof(serv_addr)) < 0)
error("ERROR on binding");
listen(sockfd,5);
clilen = sizeof(cli_addr);
newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen);
if (newsockfd < 0)
error("ERROR on accept");
bzero(buffer,256);
n = read(newsockfd,buffer,255);
if (n < 0) error("ERROR reading from socket");
mpz_init(pt);
mpz_init(ct);
mpz_init_set_str(rsn, "9516311845790656153499716760847001433441357", 10);
mpz_init_set_str(rsd, "5617843187844953170308463622230283376298685", 10);
mpz_set_str(ct, buffer, 10);
gmp_printf("Encoded: %Zd ", ct);
mpz_powm(pt, ct, rsd, rsn);
gmp_printf("Decoded: %Zd ", pt);
mpz_export(psbuffer, NULL, 1, 1, 0, 0, pt);
printf("Here is the message: %s ",psbuffer);
n = write(newsockfd,"I got your message",18);
if (n < 0) error("ERROR writing to socket");
close(newsockfd);
close(sockfd);
return 0;
}//close server
FOR TESTING ENTER:
Please enter the message: Hi there C424!
Encoded: 7114878424839051301866301460808272575931334
I got your message
Encoded: 7114878424839051301866301460808272575931334
Decoded: 375977598150559964853954922339901706
As String: Hi there C424!
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started