CCode: Our objective in this lab is to introduce security measures that will pre
ID: 3820673 • Letter: C
Question
CCode: Our objective in this lab is to introduce security measures that will prevent the messages from being read by someone monitoring the communication channel. To introduce a measure of security, you will implement a simple substitution cipher to "encrypt" the message before it is transmitted. On the receiving end, the inverse process will be used to retrieve the original message. Specifically, modify your TCP server and client software to encrypt and decrypt messages. For a substitution cipher, one letter is substituted for another. For example, if the message contains an A, then the cipher text would represent that as another letter, such as W. To make the problem manageable, you can mit the input character set to only use upper case letters, which are represented as 65 to 90 in the ASCII format. Your messages will not have any special characters or numbers. You will need check whether an in put character is a space character, w hic h is represented as decimal 32. (You can choose whether or not you want to transpose the space character.) You will also have to check for line feed, carriage return and null characters. These will be transmitted with no changes. Your screenshots should show the original message and the cipher code that is transmitted. On the receiving end, show the received cipher code and the deciphered message. If possible, include an abbreviated Wireshark capture that shows the encrypted message being transmittedExplanation / Answer
Following is the modified code which provides encryption and decryption :
#include<stdio.h> //printf
#include<string.h> //memset
#include<stdlib.h> //exit(0);
#include<arpa/inet.h>
#include<sys/socket.h>
#define BUFLEN 512 //Max length of buffer
#define PORT 8888 //The port on which to listen for incoming data
void die(char *s)
{
perror(s);
exit(1);
}
//substitution to apply
char dec[26] = {'E','C','G','H','B','I','J','K','L','M','N','O','P','Q','R','S','T','D','F','U','V','W','X','Y','Z','A'};
// function to decrypt
void decrypt()
{
int i;
for(i=0;i<BUFLEN;i++)
{
if(buf[i]>=65 && buf[i]<=90)
{
buf[i] = dec[buf[i]-'A'];
}
}
}
int main(void)
{
struct sockaddr_in si_me, si_other;
int s, i, slen = sizeof(si_other) , recv_len;
char buf[BUFLEN];
//create a UDP socket
if ((s=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1)
{
die("socket");
}
// zero out the structure
memset((char *) &si_me, 0, sizeof(si_me));
si_me.sin_family = AF_INET;
si_me.sin_port = htons(PORT);
si_me.sin_addr.s_addr = htonl(INADDR_ANY);
//bind socket to port
if( bind(s , (struct sockaddr*)&si_me, sizeof(si_me) ) == -1)
{
die("bind");
}
//keep listening for data
while(1)
{
printf("Waiting for data...");
fflush(stdout);
//try to receive some data, this is a blocking call
if ((recv_len = recvfrom(s, buf, BUFLEN, 0, (struct sockaddr *) &si_other, &slen)) == -1)
{
die("recvfrom()");
}
//print details of the client/peer and the data received
printf("Received packet from %s:%d ", inet_ntoa(si_other.sin_addr),
ntohs(si_other.sin_port));
printf("Encrypted Data: %s " , buf);
decrypt(); // decrypt message
printf("Decrypted Data: %s " , buf);
//now reply the client with the same data
if (sendto(s, buf, recv_len, 0, (struct sockaddr*) &si_other, slen) == -1)
{
die("sendto()");
}
}
close(s);
return 0;
}
/* THIS IS UDP client Simple udp client */
#include<stdio.h> //printf
#include<string.h> //memset
#include<stdlib.h> //exit(0);
#include<arpa/inet.h>
#include<sys/socket.h>
#define SERVER "127.0.0.1"
#define BUFLEN 512 //Max length of buffer
#define PORT 8888 //The port on which to send data
//substitution to apply
char enc[26] = {'Z','E','B','R','A','S','C','D','F','G','H','I','J','K','L','M','N','O','P','Q','T','U','V','W','X','Y'};
void encrypt() // encryption function
{
int i;
for(i=0;i<BUFLEN;i++)
{
if(buf[i]>=65 && buf[i]<=90)
{
buf[i] = enc[buf[i]-'A'];
}
}
}
void die(char *s)
{
perror(s);
exit(1);
}
int main(void)
{
struct sockaddr_in si_other;
int s, i, slen=sizeof(si_other);
char buf[BUFLEN];
char message[BUFLEN];
if ( (s=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1)
{
die("socket");
}
memset((char *) &si_other, 0, sizeof(si_other));
si_other.sin_family = AF_INET;
si_other.sin_port = htons(PORT);
if (inet_aton(SERVER , &si_other.sin_addr) == 0)
{
fprintf(stderr, "inet_aton() failed ");
exit(1);
}
while(1)
{
printf("Enter message : ");
gets(message);
printf("Original message : %s ",buf);
encrypt(); // encrypt message
printf("Encrypted message : %s ",buf);
//send the message
if (sendto(s, message, strlen(message) , 0 , (struct sockaddr *) &si_other, slen)==-1)
{
die("sendto()");
}
//receive a reply and print it
//clear the buffer by filling null, it might have previously received data
memset(buf,'', BUFLEN);
//try to receive some data, this is a blocking call
if (recvfrom(s, buf, BUFLEN, 0, (struct sockaddr *) &si_other, &slen) == -1)
{
die("recvfrom()");
}
puts(buf);
}
close(s);
return 0;
}