/* server.c . THIS IS TCP server*/ #include <sys/types.h> #include <sys/socket.h
ID: 3820596 • Letter: #
Question
/* server.c . THIS IS TCP server*/
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <string.h>
#include <unistd.h>
#include <stdio.h>
#define SOCKET_ERROR -1
#define BUFFER_SIZE 100
#define MESSAGE "This is the message I'm sending back and forth"
#define QUEUE_SIZE 5
int main(int argc, char* argv[])
{
int hSocket, hServerSocket; /* handle to socket */
struct hostent* pHostInfo; /* holds info about a machine */
struct sockaddr_in Address; /* Internet socket address stuct */
int nAddressSize=sizeof(struct sockaddr_in);
char pBuffer[BUFFER_SIZE];
int nHostPort;
if(argc < 2)
{
printf(" Usage: server host-port ");
return 0;
}
else
{
nHostPort=atoi(argv[1]);
}
printf(" Starting server");
printf(" Making socket");
/* make a socket */
hServerSocket=socket(AF_INET,SOCK_STREAM,0);
if(hServerSocket == SOCKET_ERROR)
{
printf(" Could not make a socket ");
return 0;
}
/* fill address struct */
Address.sin_addr.s_addr=INADDR_ANY;
Address.sin_port=htons(nHostPort);
Address.sin_family=AF_INET;
printf(" Binding to port %d ",nHostPort);
/* bind to a port */
if(bind(hServerSocket,(struct sockaddr*)&Address,sizeof(Address))
== SOCKET_ERROR)
{
printf(" Could not connect to host ");
return 0;
}
/* get port number */
getsockname( hServerSocket, (struct sockaddr *) &Address,(socklen_t *)&nAddressSize);
printf("opened socket as: fd (%d) on port (%d) for stream i/o ",hServerSocket, ntohs(Address.sin_port) );
printf("Server:
sin_family = %d
sin_addr.s_addr = %d
sin_port = %d "
, Address.sin_family
, Address.sin_addr.s_addr
, ntohs(Address.sin_port)
);
printf(" Making a listen queue of %d elements",QUEUE_SIZE);
/* establish listen queue */
if(listen(hServerSocket,QUEUE_SIZE) == SOCKET_ERROR)
{
printf(" Could not listen ");
return 0;
}
for(;;)
{
printf(" Waiting for a connection ");
/* get the connected socket */
hSocket=accept(hServerSocket,(struct sockaddr*)&Address,(socklen_t *)&nAddressSize);
printf(" Got a connection");
strcpy(pBuffer,MESSAGE);
printf(" Sending "%s" to client",pBuffer);
/* number returned by read() and write() is the number of bytes
** read or written, with -1 being that an error occured
** write what we received back to the server */
write(hSocket,pBuffer,strlen(pBuffer)+1);
/* read from socket into buffer */
read(hSocket,pBuffer,BUFFER_SIZE);
if(strcmp(pBuffer,MESSAGE) == 0)
printf(" The messages match");
else
printf(" Something was changed in the message");
printf(" Closing the socket");
/* close socket */
if(close(hSocket) == SOCKET_ERROR)
{
printf(" Could not close socket ");
return 0;
}
}
}
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
I am writing the two functions to encrypt and decrypt the message. use the below two functions to cipher and decipher your message respectively
char* encryptMessage(char* message){
{
char* encMessage;
int j = 0,temp,key = 29;
for(i=0;message[i]!=NULL;i++)
{
if(message[i] == ' ' || message[i] = ' ' || message[i] = ' '){
encMessage[j++]=message[i];
}
else{
if(message[i]>=65 && message[i]<=90)
{
temp=message[i]+key;
if(temp>90)
encMessage[j++]=65+(temp-91);
else
encMessage[j++]=temp;
}
}
}
encMessage[j]='';
return encMessage;
}
char* decryptMessage(char* encMessage){
{
char* decryptMessage;
int j = 0,temp,key = 29;
for(i=0;encMessage[i]!=NULL;i++)
{
if(encMessage[i] == ' ' || encMessage[i] = ' ' || encMessage[i] = ' '){
decryptMessage[j++]=encMessage[i];
}
else{
if(message[i]>=65 && message[i]<=90)
{
temp=encMessage[i]- key;
if(temp<65)
decryptMessage[j++]=91 -(temp-65);
else
decryptMessage[j++]=temp;
}
}
}
decryptMessage[j]='';
return decryptMessage;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.