Вы находитесь на странице: 1из 23

Encrypted Chat Server

DISSERTATION SUBMITTED AS “MINOR PROJECT” IN


PARTIAL FULFILLMENT OF THE REQUIREMENTS
FOR THE AWARD OF THE DEGREE
BACHELOR OF COMPUTER APPLICATIONS (BCA)
IN
DEPARTMENT OF COMPUTATIONAL SCIENCE
OF
BRAINWARE UNIVERSITY

Submitted By

Name Registration No. with Year Roll No.


UTPAL DEBNATH 17013000577 of 2017-2018 17010301209
ALOK BARIK 17013000379 of 2017-2018 17010301012
SHYAMAL SAHA 17013000522 of 2017-2018 17010301155
RAMKRISHNA MAITY 17013000484 of 2017-2018 17010301117

UNDER THE GUIDANCE OF

SANJUKTA MISHRA
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

BRAINWARE UNIVERSITY
398, RAMAKRISHNAPUR ROAD, BARASAT
KOLKATA-700124
TO WHOM IT MAY CONCERN

This is to certify that the project report entitled

Encrypted Chat Server

Prepared By

Name Registration No. with Year Roll No.


UTPAL DEBNATH 17013000577 of 2017-2018 17010301209
ALOK BARIK 17013000379 of 2017-2018 17010301012
SHYAMAL SAHA 17013000522 of 2017-2018 17010301155
RAMKRISHNA MAITY 17013000484 of 2017-2018 17010301117

Submitted in partial fulfillment of the requirements fort he


4th semester of
BACHELOR OF COMPUTER APPLICATIONS (BCA)
of
Brainware University

Have completed their Minor Project work entitled Encrypted Chat Server. They
have fulfilled these requirements and the project can be successfully implemented. To
far my knowledge goes their work was not submitted to any other
University/Institution for the award of any degree, diploma, fellowship or other
similar titles or prizes and the work has not been published in any journal or
magazine.

Date: 08-06-2019
---------------------------------------------
SANJUKTA MISHRA
Assistant Teacher
Department Of Computer Science and
Engineering(CSE)
BRAINWARE UNIVERSITY
398,RAMAKRISHNAPURROAD,BARASAT
KOLKATA-700124
TO WHOM IT MAY CONCERN

This is to certify that Utpal Debnath, Shyamal Saha, Ramkrishna Maity, Alok Barik,
2nd year(4thSemester) students of the BCA course of Brainware University undertook
a project as ‘MINOR PROJECT’, entitled Encrypted Chat Server under the
supervision of Sanjukta Mishra, Assistant Teacher during 2019 using C Language.
They worked sincerely and regularly on this project and completed their assigned
work satisfactorily.

Date: 08-06-2019

---------------------------------------------
Head of the Department
Department of Computational Science
BRAINWARE UNIVERSITY
398,RAMAKRISHNAPURROAD,BARASAT
KOLKATA-700124

-------------------------------------------- ---------------------------------------------
Project Coordinator External Examiner
BCA
Department of Computer Science and
Engineering
BRAINWARE UNIVERSITY
398,RAMAKRISHNAPURROAD,BARASAT
KOLKATA-700124
DECLARATION

This is here by declared that We, Utpal Debnath, Shyamol Saha, Ramkrishna Maity,
Alok Barik, student of 4th semester of BCA, have carried out our 4th semester project
work entitled Encrypted Chat Server under the guidance of Sanjukta Mishra
Assistant Teacher Department of Computer Science and Engineering Brainware
University is done by ourselves in the fulfillment for the 4th semester of BCA. It is
further declared that the project work has not been submitted to any other academic
university for the award of any degree of examination.

The said project work will not be under any circumstances be used for any university
examination except for BCA of Brainware University.

Date:08-06-2019 Name Roll No. Full Signature


UTPAL DEBNATH 17010301209
ALOK BARIK 17010301012
SHYAMAL SAHA 17010301155
RAMKRISHNA MAITY 17010301117
Computational Science
BCA, Semester 4
BRAINWARE UNIVERSITY
398,RAMAKRISHNAPURROAD,BARASAT
KOLKATA-700124
ACKNOWLEDGEMENTS

We have received the assistance and cooperation of quite a number of people during
our project Encrypted Chat Server. So we hereby take the opportunity to extend our
sincere gratitude to all those who have provided their assistance and cooperation and
valuable suggestion from time to time in spite of their busy schedule.

First, we would like to thank our project guide Sanjukta Mishra. She has not just
helped us but provided her valuable suggestion from time to time. We would like to
extend our sincere gratitude to Sanjukta Mishra and all other faculty members, entire
Technical Assistant team of Department of Computational Science for their
encouraging support and for providing us a pleasant atmosphere during the period of
project development.

Lastly, we would like to thank our parents for their overall support.

Date:08-06-2019 Name Roll No. Full Signature


UTPAL DEBNATH 17010301209
ALOK BARIK 17010301012
SHYAMAL SAHA 17010301155
RAMKRISHNA MAITY 17010301117
Computational Science
BCA, Semester 4
BRAINWARE UNIVERSITY
398,RAMAKRISHNAPURROAD,BARASAT
KOLKATA-700124
List of Figures

FIGURE 1. Example of Caesar Cipher…....................................................................................01


FIGURE 2. How Caesar cipher works …......................................................................................03
FIGURE 3. Flow chart …..............................................................................................................04
FIGURE 4 Output Terminal……………………………………………………………………06
Table of Contents

Serial Topics Page Number


No
1. Abstract 01
2. Introduction 01
3. Metamorphosis
3.1 Statement of the problem 01
3.2 Objective of the study 01
3.3 Development Platforms 01
4. Design
4.1 Server 02
4.2 Client 02
4.3Encryption/Decryption 02
4.3.1 Caesar Cipher 02
4.3.2 How it works 02-03
4.4 Other terms 03
5. System Analysis
5.1 Flow Chart 04
5.2 Project Work 05
5.3 S/w requirements 05
5.4 H/w requirements 05
6. User Manual 06-08
7. Source Code 09-13
8. Conclusion 14
8.1 Limitations 14
8.2 Possible Improvements 14
1. ABSTRACT
A simple client-server chat application has been implemented using socket programming. The
application is developed using C programming. It is encrypted by an encryption algorithm. Its
design is described, limitations are discussed, improvements are proposed and a user manual is
included. The primary objective of this report is to present the principles behind socket
programming and the libraries available for socket programming.

2. INTRODUCTION
Implementing a chat server application which provides a goof opportunity for a beginner to design
and implement a network based system. The design is very simple and implemented in C. The
encryption and decryption method which is used in the project is also easy to understand and
develop and it provides security to both side, server and client. To start chatting client should get
connected to server. It is made up of 2 applications the client application and the server application.
We will focus on TCP socket connections which are a fundamental part of socket programming.
Keywords: sockets, client-server, C network programming-socket functions.

3. METAMORPHOSIS
3.1. STATEMENT OF THE PROBLEM

The client-server communication model is used in a wide variety of software applications. The main
weakness of client-server chat application is that there is no security provided to data which is
transferred between clients. Any unauthorized client can hack the client access and can change the
data. This is the main objective of this project (To develop a secured Client-Server Chat
Application).

3.2. OBJECTIVES OF THE STUDY


The aim of this project is to develop a reliable and secure network programming (Client-Server chat
model) which can perform a multithreaded server client chat application based on socket
programming using Transport Control Protocol (TCP). As security is the key factor while
communicating over a network, Caesar Cipher algorithm is used for the security purpose of the
data. Encryption and decryption is implemented both side for the implementation of strong data
protection.

3.3 DEVELOPMENT PLATFORMS


In hardware requirement we require all those components which will provide us the platform for the
devolvement of the project. The minimum hardware required for the development of this project is
as follows-

Ram- minimum 128 MB


Hard Disk- minimum 5 GB
Processor – Pentium 3
128 MB Ram is used so that we can execute our project in a at least possible RAM. 5 GB hard disk
is used because project takes less space to be executed. Therefore minimum hard disk is used.

1
4. DESIGN
TCP is used as the transport layer protocol, since it provides reliable delivery which is critical for
the given application. TCP does not provide timing guarantee, which is not very important in the
given scenario.

4.1 SERVER
The server is implemented as a singleton class. The main thread opens a server socket on the local
inet address and port 2000, which has been arbitrarily chosen and hard coded. It then waits for
clients to connect to it. When a client connects to the server, it creates a separate thread. The thread
dedicated to the client opens a buffered stream to read input messages from the client and a print
writer stream to send messages to the client.
The message going from a client to the sever are string with at least one word. The thread dedicated
to the client waits for the incoming massage.

4.2 CLIENT
A client is a system that accesses or desires for a service made accessible by a server.
The Client is implemented using two threads, one each for incoming and outgoing messages. The
main thread opens the socket and connects to the server. It then opens input buffered stream and
print writer for incoming and outgoing messages respectively.
When exit is requested, the client sends exit message to the server, waits for its response, and
closes the streams and exits.

4.3 ENCRYPTION / DECRYPTION


This TCP chat server application is secured with an encryption and decryption method, called
Caesar Cipher algorithm.

4.3.1. CAESAR CIPHER


The Caesar cipher is one of the earliest known and simplest ciphers. It is a type of
substitution cipher in which each letter in the plaintext is 'shifted' a certain number of places
down the alphabet. For example, with a shift of 1, A would be replaced by B, B would
become C, and so on. The method is named after Julius Caesar, who apparently used it to
communicate with his generals.

4.3.2. HOW IT WORKS


To pass an encrypted message from one person to another, it is first necessary that both
parties have the 'key' for the cipher, so that the sender may encrypt it and the receiver may
decrypt it. For the Caesar cipher, the key is the necessary for reading the message.

Here is a quick example of the encryption and decryption steps involved with the Caesar
cipher. The text we will encrypt is ‘Hello', with a shift (key) of 1.

2
H e l l O

i f m m p
Basically these things are happening.

Figure: 2
When client get a message from the serve, client will be asked for the key to decrypt the message.
Similarly when client send a message to the server the message will be first encrypted and then send
to the server.

4.4 OTHER TERMS


Socket: Socket is a standard connection protocol that supports data communication over the
network between connected terminals. The standard connection supports the data transmission both
by the TCP and UDP protocols between the terminals.

TCP: TCP is a transport layer protocol used by applications that require guaranteed delivery of
data. Basically, it is a connection-oriented protocol. To communicate over TCP one must first have
to establish a connection between pair of sockets, where one socket is client and the other belongs
to server. After the connection is established between them then they can communicate with each
other.

PORT: Port is a software mechanism that allows the centralized connected Servers to listen for
requests made by clients. Port is actually purposed as a gateway to listen for the requested
parameters by the server terminals or other machines. It is a software address on a system that is on
the network. Entire request response proceeding among this Application is carries through machine
ports.

IP: This refers to Internet Protocol; it is the reasonable network address of device on a network. It is
notational called dotted-decimal (for instance: 128.1.0.1)

3
5. SYSTEM ANALYSIS
5.1 FLOW CHART

client server
Massage from Key Encryption Key Decrypted
client Message message

server server
Decrypted Key Encryption Key Message from
message Message server

Entire process can be classified into these steps-


1. Create TCP socket.
2. Bind the socket to server address.
3. Put the server socket in a passive mode, where it waits for the client to approach the server to
make a connection
4. At this point, connection is established between client and server, and they are ready to
transfer data.
5. Go back to Step 3.

Figure 3

4
5.2 PROJECT WORK
Before sending and receiving any massages from server to client or client to server, we have to
make the connection between them. So the first thing have to do is to connection of two nodes. One
is server and another is client. Firstly we have created two socket in server and client using socket().
Then in the both side (server and client) initialize the address structure as per the server and bind the
socket to the address of the server using the bind(); Then server will listen the connection with the
listen() function. After that connection is established between server and client means we can now
send data.

Here the first massage will go from client to server. As we have implemented encryption method in
the client part, so the message will encrypt the message with the help of a key. And the key will be
known to both the server and the client. In the server side there a decryption method is
implemented, so before server receives the message, there will be a print like “Enter the key to
decrypt the message:”. According to the encryption and decryption method if sever is able to enter
the right key (which was given by client) then only the actual message will be shown. If wrong key
is entered it will not throw any error, it will just print some another words which is meaningless.

Encryption and decryption method is implemented in the both side of server and client. Before
sending any message form server/client to client/server, we have to give a key to encrypt the
message then to decrypt the message.

After that we close the connection by using the close() function.

5.3 SOFTWARE REQUIREMENTS


Software can be defines as program, which run on our computer. It act as petrol in the vehicle.
It provides the relationship between the human and a computer.

Operating System - Linux Mint


Ubuntu
Debian GNU/Linux

5.4 HARDWARE REQUIREMENTS


In hardware requirement we require all those components which will provide us the platform for the
devolvement of the project. The minimum hardware required for the development of this project is
as follows-

Ram- minimum 128 MB


Hard Disk- minimum 5 GB
Processor – Pentium 3
128 MB Ram is used so that we can execute our project in a at least possible RAM. 5 GB hard disk
is used because project takes less space to be executed. Therefore minimum hard disk is used.

5
6. USER MANUAL
6.1 ESTABLISH THE CONNECTION

Starting the server:


gcc serve.c [enter]
./a.out [enter]
Server terminal will open

Establishing connection to server:


gcc client.c [enter]
./a.out[enter]
Then the following terminal will open.

6.2 SENDING AND RECEIVING MESSAGE


Pictorial description step by step-

Step 1: Type the massage and enter the encryption key. End hit enter.

Figure 4

6
Step 2: Program control will go to the Server terminal. Give the decryption key there
(Client’s provided key).

Figure 5

Step 3: After hitting the enter the massage will be decrypted and shown.

Figure 6

7
Step 4: Type message again in the server box. Provide the key and hit enter.

Figure 7

Step 5: Enter the decryption key in the client terminal and decrypted message will be printed.

Figure 8

8
7. SOURCE CODE

Server code
#include "inetu.h"
#include <stdio.h>

main(int argc,char *argv[])


{
int i=0,sockfd,newsockfd,clilen,childpid,n,key,j,u;
char ch, line[MAXLINE],sline[MAXLINE],mm[200];
struct sockaddr_in cli_addr,serv_addr;
pname=argv[0];

if( (sockfd=socket(AF_INET,SOCK_STREAM,0)) <0)


perror("Error in socket call");

bzero((char *)&serv_addr,sizeof(serv_addr));
serv_addr.sin_family=AF_INET;
serv_addr.sin_port=htons(SERV_TCP_PORT);
serv_addr.sin_addr.s_addr=htonl(INADDR_ANY);

if(bind(sockfd,(struct sockaddr *)&serv_addr,sizeof(serv_addr))<0)


perror("Error in bind");

listen(sockfd,5);

printf("\n\n\n\n::::::::::::::::::::::::::::Server Portal:::::::::::::::::::::::::::::::::\n\n\n\n");
for(;;)
{
clilen=sizeof(cli_addr);
newsockfd=accept(sockfd,(struct sockaddr *)&cli_addr,&clilen);
if(newsockfd<0)
perror("Error in accept");
if((childpid=fork())<0)
perror("Error in fork");
else if(childpid==0) /*child*/
{
//close(sockfd);
for(;;)
{
n=readline(newsockfd,line,MAXLINE);

if(n==0)
return;

else if(n<0)
perror("error");

9
printf("\nEnter key to dycrypt the massage: ");
scanf("%d",&key);
for(u = 0; line[u] != '\0'; ++u)
{
ch = line[j]; //encryption code
if((ch >= 'a' || ch >= 'A') && (ch <= 'z' || ch <= 'Z'))
{
ch = ch - key;
line[j] = ch;
}
}
printf("\n\::Message from Client:: IP Address: %s:: %s
",inet_ntoa(cli_addr.sin_addr),line);
printf("\nMessage to client: ");

getchar();

fgets(sline,MAXLINE,stdin);

printf("Enter key to encrypt the massage: ");


scanf("%d",&key);
for(j = 0; sline[j] != '\0'; ++j)
{
ch = sline[j]; //encryption code
if((ch >= 'a' || ch >= 'A') && (ch <= 'z' || ch <= 'Z'))
{
ch = ch - key;
sline[j] = ch;
}
}

n=strlen(sline);
sline[n]=0;
if(writen(newsockfd,sline,n)!=n)
perror("error");
}
close(sockfd);
exit(0);
// memset(mm,'\0',200);
}
close(newsockfd); /*parent*/

}
}

Client code
#include "inetu.h"

main(int argc,char *argv[])


{
10
int sockfd,n,j,key,u;
struct sockaddr_in serv_addr;
char ch, sendline[MAXLINE],recvline[MAXLINE+1];

if((sockfd=socket(AF_INET,SOCK_STREAM,0))<0)
perror("Error in socket");

bzero((char *)&serv_addr,sizeof(serv_addr));
serv_addr.sin_family=AF_INET;
serv_addr.sin_port=htons(SERV_TCP_PORT);
serv_addr.sin_addr.s_addr=htonl(INADDR_ANY);

if(connect(sockfd,(struct sockaddr *)&serv_addr,sizeof(serv_addr)) <0)


perror("Error in connect");

printf("\n\n\n\n::::::::::::::::::::::::::::Client Portal:::::::::::::::::::::::::::::::::\n\n\n\n");
printf("\nEnter message to server: ");
//while(fgets(sendline,MAXLINE,stdin) != NULL)
for(;;)
{
fgets(sendline,MAXLINE,stdin);
printf("Enter key to encrypt the massage: ");
scanf("%d",&key);
for(u = 0; sendline[u] != '\0'; ++u)
{
ch = sendline[j]; //encryption code
if((ch >= 'a' || ch >= 'A') && (ch <= 'z' || ch <= 'Z'))
{
ch = ch + key;
sendline[j] = ch;
}
}

n=strlen(sendline);

if(writen(sockfd,sendline,n) != n)
perror("error");

n=readline(sockfd,recvline,MAXLINE);

if(n<0)
perror("error");

printf("\nEnter key to decrypt the massage: ");


scanf("%d",&key);

for(j = 0; recvline[j] != '\0'; ++j)


{
11
ch = recvline[j];
if((ch >= 'a' || ch >= 'A') && (ch <= 'z' || ch <= 'Z')) //decryption massage
{
ch = ch + key;
recvline[j] = ch;
}
}
getchar();

printf("\n::Message from Server:: IP Address: %s::


%s",inet_ntoa(serv_addr.sin_addr),recvline);

//getchar();

printf("\nEnter message to server: ");

recvline[n]=0;
}

close(sockfd);
exit(0);
}

inetu.h header file

#include<stdio.h>

#include<sys/types.h>

#include<sys/socket.h>

#include<netinet/in.h>

#include<arpa/inet.h>

#include<netdb.h>

#include<fcntl.h>

#include<malloc.h>

#include<stdlib.h>

#include<string.h>

//#define SERV_ADDR "192.168.134.21"

#define SERV_TCP_PORT 3500

12
#define TRUE 1

#define FALSE 0

#define MAXLINE 50

char *pname;

int value,val;

int readline(register int fd,register char *ptr,register int maxlen)


{
int n,rc;
char c;

for(n=1;n<maxlen;n++)
{
if( (rc=read(fd,&c,1) )== 1)
{
*ptr++=c;
if(c=='\n')
break;
}
else if(rc==0)
{
if(n==1)
return 0;
else break;
}
else return(-1);
}
*ptr=0;
return(n);
}

int writen(register int fd,register char *ptr,register int nbytes)


{
int nleft,nwritten;

nleft=nbytes;

while(nleft>0)
{
nwritten=write(fd,ptr,nleft);
nleft=nleft-nwritten;
ptr=ptr+nwritten;
}}

13
8. CONCLUSION

8.1 LIMITATION
There is no graphical user interface (GUI). So there are no chat windows and the user needs to
compile the code through GCC compiler. And only one user can connected at a time with the
server.
The encryption and decryption method used in this code is secure but not the hardest.

8.2 POSSIBLE IMPROVEMENTS


Limitations mentioned above can be addressed in improvements. A graphical user interface (GUI)
will preclude the need to write the name of destination user when sending messages and make it
more users friendly.
A database of users containing username and password can be coupled with the existing design to
maintain user accounts.
A tougher encryption method can be implemented to the program to make the connection more
secure.

14
References:
1. W. Richard Stevens, TCP/IP Illustrated, Volume 1: The Protocols.

2. Pocket Guide to TCP/IP Socket Programming in C: Morgan Kaufmann; C Version.

3. Cryptography and Network Security, Atul Kahate, McGraw Hill Education; Third edition.

4. Let us C, BPB Publications Eight edition.

Website:
[1] https://www.geeksforgeeks.org/tcp -server-client-implementation-in-c/
[2] https://www.tutorialspoint.com/unix_sockets/client_server_model.html
[3] www.wikipedia.com
[4] https://github.com/hasanuzzaman2013/java_windowbuilder

15
16

Вам также может понравиться