Friday, November 14, 2014

PASSING MESSAGE BETWEEN CLIENT AND SERVER IN SECURE WAY


/****************** CLIENT CODE ****************/



#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>

int main()
{
  int welcomeSocket, newSocket;
 struct sockaddr_in serverAddr;
  struct sockaddr_storage serverStorage;
  socklen_t addr_size;
char ptext[20];
 char ctext[20];
 char alpha[26]="abcdefghijklmnopqrstuvwxyz";
 char keyha[26]="noqfgiazywvxumsdtehbjckplr";
 int i,j;


  /*---- Create the socket. The three arguments are: ----*/
  /* 1) Internet domain 2) Stream socket 3) Default protocol (TCP in this case) */

  welcomeSocket = socket(PF_INET, SOCK_STREAM, 0);
  
  /*---- Configure settings of the server address struct ----*/
  /* Address family = Internet */

  serverAddr.sin_family = AF_INET;

  /* Set port number, using htons function to use proper byte order */

  serverAddr.sin_port = htons(7891);

  /* Set IP address to localhost */

  serverAddr.sin_addr.s_addr = inet_addr("127.0.0.1");

  /* Set all bits of the padding field to 0 */

  memset(serverAddr.sin_zero, '\0', sizeof serverAddr.sin_zero);  

  /*---- Bind the address struct to the socket ----*/

  bind(welcomeSocket, (struct sockaddr *) &serverAddr, sizeof(serverAddr));

  /*---- Listen on the socket, with 5 max connection requests queued ----*/

  if(listen(welcomeSocket,5)==0)
    printf("Listening\n");
  else
    printf("Error\n");

  /*---- Accept call creates a new socket for the incoming connection ----*/

  addr_size = sizeof serverStorage;
  newSocket = accept(welcomeSocket, (struct sockaddr *) &serverStorage, &addr_size);

  /*---- Send message to the socket of the incoming connection ----*/

printf("\n Encryption : ");
 printf("\n Enter the plain text :");
 for(i=0;i<6;i++)
scanf("%c",&ptext[i]);
 for(i=0;ptext[i]!='\0';i++)
   for(j=0;alpha[j]!='\0';j++)
   {
if(ptext[i]==alpha[j])
{
ctext[i]=keyha[j];
break;
}
   }
 ctext[strlen(ptext)]='\0';
  
 send(newSocket,ctext,20,0);
}

I used monoaplhabetic cipher to encrypt the message.


/****************** SERVER CODE ****************/

#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>

int main(){
  int clientSocket,i,j;
  struct sockaddr_in serverAddr;
  socklen_t addr_size;
  char ptext[20];
 char ctext[20];
 char alpha[26]="abcdefghijklmnopqrstuvwxyz";
 char keyha[26]="noqfgiazywvxumsdtehbjckplr";


  /*---- Create the socket. The three arguments are: ----*/
  /* 1) Internet domain 2) Stream socket 3) Default protocol (TCP in this case) */

  clientSocket = socket(PF_INET, SOCK_STREAM, 0);
  
  /*---- Configure settings of the server address struct ----*/
  /* Address family = Internet */

  serverAddr.sin_family = AF_INET;

  /* Set port number, using htons function to use proper byte order */

  serverAddr.sin_port = htons(7891);

  /* Set IP address to localhost */

  serverAddr.sin_addr.s_addr = inet_addr("127.0.0.1");

  /* Set all bits of the padding field to 0 */

  memset(serverAddr.sin_zero, '\0', sizeof serverAddr.sin_zero);  

  /*---- Connect the socket to the server using the address struct ----*/

  addr_size = sizeof serverAddr;
  connect(clientSocket, (struct sockaddr *) &serverAddr, addr_size);

  /*---- Read the message from the server into the buffer ----*/

  recv(clientSocket, ctext, 6, 0);

  /*---- Print the received message ----*/

printf("the cipher text is");
for(i=0;i<6;i++) 
printf(" %c",ctext[i]);
  for(i=0;ctext[i]!='\0';i++)
  for(j=0;keyha[j]!='\0';j++)
  {
if(ctext[i]==keyha[j])
{
 ptext[i]=alpha[j];
 break;
}
  }
 printf("\n The Plain text is : ");
 for(i=0;i<6;i++)

printf(" %c",ptext[i]);


}

I used monoalphabetic cipher to decrypt the algorithm.

THE OUTPUTS ARE:


First server code should be executed and it will give "listening" output. After that we must execute the client code ,so that it will ask us to enter the key or message(I assumed the message as the key),then we will enter the key(or)message. It will show the encrypted message in client  side and also in server side.Now in server side we can observe both encrypted message and decrypted message .
In this way message is securely travelled from the client system to 
server system. 


server listening to client requests.

             client asking to enter the key or message


Message encrypted
  
Message decrypted


By using monoaplhabetic cipher ,i encrypted and decrypted data.

No comments:

Post a Comment