Sie sind auf Seite 1von 8


DES Chat App


Yusra Ali Shaikh (13573)
Khizar Waseem (16738)
Muhammad Ahmed (14571)

December 21, 2014

To develop Client-Server chat application in which users can create a group
chat by either creating a server or joining an already created server by some

other user. The main problem is to apply a security algorithm which will
encrypt and decrypt the text messages in order to provide secure data transfer.

Real Life Example

The real life example of this problem can be an e-mail system which takes
messages as input from users, convert it into cipher text (encrypted text) and
then sends it over the internet to reach the receiver. The receiver system then
converts that received cipher text into normal text based on some algorithm or
key and that is how emails are secured from hacking.

(Fig. 1)

To encrypt and decrypt messages we use DES algorithm. Since DES is a block
cipher, that is, the data is encrypted in form of blocks, therefore, the size of a
single message will always be a 64-bit block encrypted with a key of 64-bit key.
Since the size of message and key is fixed, this algorithm has complexity of
O(1) for a single block of text and O(m) for multiple blocks where m is the
number of messages encrypted.

DES (Data Encryption Standard) is a block cipher encryption algorithm. It is a
symmetric key algorithm which means it shares same key with sender and
receiver of the message to encrypt/decrypt. DES takes 64-bit plaintext as input


and encrypts it with a 64-bit key and sends the resulting cipher text to

(Fig. 2)


Key Generation:
A 64-bit binary number is used which goes through a number of steps of
Permutation, Circular Left-Shifts etc. to generate 16 sub-keys. These sub-keys
are used in each of 16 rounds of encryption and decryption.
After generation of 16 sub-keys, a block of 64-bits is encrypted by applying
following steps:
IP(Initial Permutation)
A Function (16 rounds)
Swapping of left and right 32-bits
IP-1(Inverse Initial Permutation)
The decryption part is same as the encryption except the order of sub-keys
application. In decryption, the generated 16 sub-keys are applied in reverse
order i.e. K16 first, then K15, then K14 and so on.

Algorithm Correctness
The correctness of this algorithm can be proved in a way that, the fact of DES
being an encryption algorithm and that too being symmetric key, it will encrypt
a plain text and always decrypt its cipher text into correct plaintext because
the steps of decryption are no different from encryption except that they are
reversed only. Therefore, this algorithms correctness is proved.

Pseudo Code
Cipher (plainBlock[64], RoundKeys[16, 48], cipherBlock[64])
permute (64, 64, plainBlock, inBlock, InitialPermutationTable)
split (64, 32, inBlock, leftBlock, rightBlock)
for (round = 1 to 16)
mixer (leftBlock, rightBlock, RoundKeys[round])
if (round!=16) swapper (leftBlock, rightBlock)
combine (32, 64, leftBlock, rightBlock, outBlock)
permute (64, 64, outBlock, cipherBlock, FinalPermutationTable)


mixer (leftBlock[48], rightBlock[48], RoundKey[48])

copy (32, rightBlock, T1)
function (T1, RoundKey, T2)
exclusiveOr (32, leftBlock, T2, T3)
copy (32, T3, rightBlock)
swapper (leftBlock[32], rigthBlock[32])
copy (32, leftBlock, T)
copy (32, rightBlock, leftBlock)
copy (32, T, rightBlock)
function (inBlock[32], RoundKey[48], outBlock[32])
permute (32, 48, inBlock, T1, ExpansionPermutationTable)
exclusiveOr (48, T1, RoundKey, T2)
substitute (T2, T3, SubstituteTables)
permute (32, 32, T3, outBlock, StraightPermutationTable)
substitute (inBlock[32], outBlock[48], SubstitutionTables[8, 4, 16])
for (i = 1 to 8)
row 2 x inBlock[i x 6 + 1] + inBlock [i x 6 + 6]
col 8 x inBlock[i x 6 + 2] + 4 x inBlock[i x 6 + 3] +
2 x inBlock[i x 6 + 4] + inBlock[i x 6 + 5]

value = SubstitutionTables [i][row][col]

outBlock[[i x 4 + 1] value / 8; value value mod 8
outBlock[[i x 4 + 2] value / 4; value value mod 4
outBlock[[i x 4 + 3] value / 2; value value mod 2
outBlock[[i x 4 + 4] value

Cost Analysis
The cost of implementing this algorithm depends on number of factors, having
said that, weve compared the time and speed of this algorithm on two
different platforms i.e. C# and C++ and both perform differently as we increase
or decrease the size of data. Graphs in following figures show the comparison:


Since DES algorithm is used in chat application, the implementation part breaks
into following two major parts:
Chat App
DES Algorithm
Chat App:
The chat application is a GUI based application developed on C# WinForms
platform. Socket Programming is used to send and receive messages on the LAN
There are 3 classes in this app, Form1.cs, Server.cs and Client.cs.
Form1.cs is the main class i.e. the program starts from this class.
Server.cs class is called when user clicks on Create Server button. This class
has all the functionality required to start server like getting ready to accept
incoming client connections on a certain port number and starting new thread
for each client that connects to server. As the server connects to a client, it
can start chatting with the client and the messages sent/received are
encrypted/decrypted on backend.
Client.cs class is started when user decides to join an existing server on the
network by giving IP(Internet Protocol) and Port number of server. In this class,
an object of DES class is created to apply the algorithm in the chat app.
DES Algorithm:
There is another class called DES.cs in which the DES algorithm is
implemented. The methods are created in a way that they take plain and
cipher texts as input and output encrypted/decrypted text. Client.cs and
Server.cs classes create object of this (DES.cs) class and invoke methods when
user wants to send/receive messages.

Correctness Runs
For checking the correctness of this algorithm we used multiple strings as test
cases which we matched with some of the online DES encryption tools to check
if the algorithm is correctly working in encrypting and decrypting messages.
Test Cases:
Since this is a chat application, we used common messages to start a
conversation like hello, hey etc. and a randomly generated 64-bit binary


key which is shared between client and server as soon as connection is

established between them.
The expected output while testing this algorithm should be the original
message that is passed through algorithm as first it will be encrypted and then
if it decrypts correctly then itll surely return the same message.

Correctness Runs Results

In the test case we passed hello123 string which makes a 64-bit binary as
01101000 01100101 01101100 01101100 01101111 00110001 00110010
00110011 and we encrypted it with a 64-bit key encrypt1 i.e. 01100101
01101110 01100011 01110010 01111001 01110000 01110100 00110001 gives
01100101 01101110 01010100 01000011 01110000 01110101 01001000
01001111 as 64-bit cipher text. Then we decrypted this cipher text with same
key and we got 01101000 01100101 01101100 01101100 01101111
00110001 00110010 00110011 which is same 64-bit binary as the original

Cost Runs
To check the cost, we run test cases with texts H,HEL and HELLO on two
different platforms i.e. C# and C++ and check how they perform in terms of
time as the data size increases.

Cost Runs Results

As depicted in above figures, the performance in C# gets better as data

increases and the time decreases.


1. Fig. 1 (Page 2)


Fig. 2 (Page 3)
Pseudo code (Page 4)

Running Code
Since this program is written in C# language, it requires you to have Visual
Studio IDE to run its code. Below are the instructions to run the code of this
1. First, import the project folder in Visual Studio 2013.
2. Press F5 button to run the code.
3. A form will show up with two buttons, each for Creating or Joining
Server respectively.
4. When you go for Create Server button, a new window will be shown in
which there is an IP address of your network and textArea and textField
for chatting with clients joining this server.
5. If Join Server button is clicked, the user will be shown a new window
where he/she may provide existing servers IP and port number and click
Connect button. This will result in a message that youre connected to
server and Keys are exchanged between client and server as soon as the
connection is established, and the key is used for later
encryption/decryption of messages.
There are following three main classes in this project: