Encrypted Communication
Background
The goal of this assignment is to give you practical experience in using cryptographic libraries to
implement encryption functionality in your code, especially in regard to data in transit i.e.
network communications.
To this end you are provided with two pieces of code (server.c and client.c) written in the C
programming language. The code implements a simple socket communication program that
enables two machines (a client and a server) to make a connection to each other and send
messages.
Your objective in carrying out this assignment will be to modify the code to achieve the
following:
Unlimited communication between the client and the server
Termination of the connection when a specific message is sent from the client to the
server
Encryption of the communication between the client and the server using any encryption
technique
Instructions
1. You will find attached the two pieces of C programming language code that implement the
socket program (One for the Client side and the other for the Server side).
2. You can run the program on any platform that works for you (I would suggest using Ubuntu
Linux). After compiling the code, you have to assign a port number (example; Port number =
5000, you can use any port number between 2000 and 65535) to the server when putting in
the command to run it. i.e.
./server 5000
On the client side, you need the host name or IP address of the machine on which the server is
running (if the host name or IP address is X) to run the client and connect to the server
./client X 5000
If you are running both programs on the same machine, the host name of the server will be
localhost
./client localhost 5000
NOTE: A tutorial will be given to explain the details of the program and demonstrate how to
compile and run the code. Please endeavour to attend the tutorial
3. The connection between the client and server closes after one message is sent, modify the
codes such that multiple messages can be sent between both sides without termination.
4. Modify the code to terminate the connection when the client sends the message “End
Session” to the server.
5. Modify the code such that communication between both the client and server is an encrypted
communication (Symmetric or Asymmetric)
6. During the encrypted communication the terminal should display the messages received both
before decryption and after decryption
NOTE: Implement any encryption technique you’ve learnt so far and use any C language
cryptographic library of your choice, preferably the one you identified in your previous
assignment.
Submission
1) Submit your modified code and a report (which should include screenshots of your results for
questions 3, 4, and 5) in separate files via Blackboard by the due date. No zipped files
allowed.
#include
#include
#include
#include
#include
//Defines the structure hostent
void error(char *msg) //Same error function as in server
{
perror(msg);
exit(0);
}
int main(int argc, char *argv[])
{
int sockfd, portno, n;
struct sockaddr_in serv_addr; //The address of the server that client wants
to connect to
struct hostent *server; //Defines the variable server as a pointer to a
structure of type hostent
char buffer[256];
if (argc < 3) {
fprintf(stderr,"usage %s hostname port\n", 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]); //Client attempts to get the hostent
structure for the server
if (server == NULL) {
fprintf(stderr,"ERROR, no such host\n");
exit(0);
}
bzero((char *) &serv_addr, sizeof(serv_addr)); //Initialize serv_addr
serv_addr.sin_family = AF_INET; //Set the fields in serv_addr
bcopy((char *)server->h_addr, //void bcopy(char *s1, char *s2, int length).
server->h_addr is a character string,
(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) //Connect to server. function returns 0 on success and −1 on failure
error("ERROR connecting");
printf("Please enter the message: "); //Prompt user for message after
connection is successful
bzero(buffer,256); //Initialize buffer
fgets(buffer,255,stdin); //Read from stdin into buffer
n = write(sockfd,buffer,strlen(buffer)); //Write buffer into socket.
Returns number of characters written
if (n < 0) //Check for writing errors
error("ERROR writing to socket");
bzero(buffer,256);
n = read(sockfd,buffer,255); //Reads servers response into buffer
if (n < 0)
error("ERROR reading from socket");
printf("%s\n",buffer); //Prints servers response to screen
return 0; //Exit
}
#include //Declarations used in most input and output operations;
#include //Defines a number of data types used in system calls
#include //Defines a number of structures needed for sockets;
#include //Contains constants and structures needed for Internet
domain addresses.
void error(char *msg) // Displays an error message on stderr and then aborts
the program
{
perror(msg);
exit(1);
}
int main(int argc, char *argv[])
{
int sockfd, newsockfd, portno, clilen;
/*sockfd and newsockfd, are array subscripts into the file
descriptor table. They store the values returned by the socket system call and
the accept system call.portno stores the port number on which the server accepts
connections.
clilen stores the size of the address of the client, which is needed
for the
accept system call.
*/
char buffer[256]; //The server reads characters from the socket
connection into the buffer char.
struct sockaddr_in serv_addr, cli_addr; //client and server address
structures, using the sockaddr_ in Internet address structure. This structure is
defined in netinet/in.h.
int n; //The number of characters read or written by the read() and
write() calls
if (argc < 2) { //check that the user has provided a port number
argument and displays an error message
fprintf(stderr,"ERROR, no port provided\n");
exit(1);
}
sockfd = socket(AF_INET, SOCK_STREAM, 0); //Create new streaming IPV4
socket. 0 indicates default protocol, which is TCP. Returns file descriptor
table entry
if (sockfd < 0) //Checks for errors in the creation of the socket. A
negative file descriptor table usually indicates an error.
error("ERROR opening socket");
bzero((char *) &serv_addr, sizeof(serv_addr)); //Set all values in a
buffer to zero, bzero(buf_addr,buf_size)
portno = atoi(argv[1]); //Retrieves the port no provided as a string and
converts it to an integer
serv_addr.sin_family = AF_INET; //Assign values to the variable
serv_addr, which is a structure of type struct sockaddr_in
serv_addr.sin_port = htons(portno); //Converts a port number in host
byte order to a port number in network byte order.
serv_addr.sin_addr.s_addr = INADDR_ANY; //IPv4 address of the server,
which is obtained from the symbolic constant INADDR_ANY.
if (bind(sockfd, (struct sockaddr *) &serv_addr, //Bind operation and
error checking. Second parameter is cast into right type
sizeof(serv_addr)) < 0)
error("ERROR on binding");
listen(sockfd,5); //Socket listens for new connections. 2nd argument is
the number of connections that can be waiting while the process is handling a
particular connection
clilen = sizeof(cli_addr);
newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen);
//Causes the process to block until a new client request comes in
if (newsockfd < 0)
error("ERROR on accept");
bzero(buffer,256); //Initialize buffer
n = read(newsockfd,buffer,255); //Read up to 255 bytes into buffer.
Returns no. of xters read. Blocks until client writes
if (n < 0) error("ERROR reading from socket"); //Check for errors while
reading
printf("Here is the message: %s\n",buffer); //Print message to stdout
n = write(newsockfd,"I got your message",18); //Acknowledge the message
if (n < 0) error("ERROR writing to socket"); //Checks for errors in
writing
return 0; //Terminates
}
Purchase answer to see full
attachment