37
Computer Networks LAB MANUAL DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING

Computer Networks labmanual.pdf · Implement the data link layer framing methods such as character, character stuffing, and bit stuffing. 2. Implement on a data set of characters

  • Upload
    others

  • View
    27

  • Download
    0

Embed Size (px)

Citation preview

Computer Networks

LAB MANUAL

DEPARTMENT OF COMPUTER SCIENCE &

ENGINEERING

B.Tech. III Year I Sem. L/T/P/C

Course Code: CS506PC 0/0/3/2

Course Objectives:

To Understand the functionalities of various layers of OSI model

To understand the operating System functionalities

Course Outcomes:

Ability to understand the encryption and decryption concepts in Linux environment

Ability to apply appropriate algorithm for the finding of shortest route.

Ability to configure the routing table

System/ Software Requirement

Intel based desktop PCs LAN CONNECTED with minimum of 166 MHZ or faster

processor with at least 64 MB RAM and 100 MB free disk space

Computer Networks Lab:

1. Implement the data link layer framing methods such as character, character

stuffing, and bit stuffing.

2. Implement on a data set of characters the three CRC polynomials – CRC 12,

CRC 16 and CRC CCIP .

3. Implement Dijkstra‘s algorithm to compute the Shortest path thru a graph.

4. Take an example subnet graph with weights indicating delay between nodes.

Now obtain Routing table art each node using distance vector routing

algorithm

b. Take an example subnet of hosts. Obtain broadcast tree for it.

5. Take a 64 bit playing text and encrypt the same using DES algorithm.

6. Write a program to break the above DES coding

c. Using RSA algorithm encrypts a text data and Decrypt the same

Aim:

Implement the data link layer framing methods such as Bit Stuffing.

Theory

Security and Error detection are the most prominent features that are to be provided by any pplication

which transfers data from one end to the other end. One of such a mechanism in tracking errors which

may add up to the original data during transfer is known as Stuffing. It is of two types namely Bit

tuffing and the other Character Stuffing. Coming to the Bit Stuffing, 01111110 is appended within the

original data while transfer of it. The following program describes how it is stuffed at the sender end

and de-stuffed at the reciever end.

Program:

#include

main()

{

int a[15];

int i,j,k,n,c=0,pos=0;

clrscr();

printf("\n Enter the number of bits");

scanf("%d",&n);

printf("\n Enter the bits");

for(i=0;i<n;i++)

scanf("%d",&a[i]);

for(i=0;i<n;i++)

{

if(a[i]==1)

{

c++;

if(c==5)

{

pos=i+1;

c=0;

for(j=n;j>=pos;j--)

{

k=j+1;

a[k]=a[j];

}

a[pos]=0;

n=n+1;

}

}

else

c=0;

}

printf("\n DATA AFTER STUFFING \n");

printf(" 01111110 ");

for(i=0;i<n;i++)

{

printf("%d",a[i]);

}

printf(" 01111110 ");

getch();

}

Output:

Aim:

Implement the data link layer framing methods such as Character Stuffing and also De-stuff it

Theory

Coming to the Character Stuffing, DLESTX and DLEETX are used to denote start and end of

character data with some constraints imposed on repetition of charaters as shown in the program

below clearly.

Program:

#include

#include

#include

void charc(void);

void main()

{

int choice;

while(1)

{

printf("\n\n\n1.character stuffing");

printf("\n\n2.exit");

printf("\n\n\nenter choice");

scanf("%d",&choice);

printf("%d",choice);

if(choice>2)

printf("\n\n invalid option....please renter");

switch(choice)

{

case 1:

charc();

break;

case 2:

exit(0);

}

}

}

void charc(void)

{

char c[50],d[50],t[50];

int i,m,j;

clrscr();

printf("enter the number of characters\n");

scanf("%d",&m);

printf("\n enter the characters\n");

for(i=0;i<m+1;i++)

{

scanf("%c",&c[i]);

}

printf("\n original data\n");

for(i=0;i<m+1;i++)

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

d[0]='d';

d[1]='l';

d[2]='e';

d[3]='s';

d[4]='t';

d[5]='x';

for(i=0,j=6;i<m+1;i++,j++)

{

if((c[i]=='d'&&c[i+1]=='l'&& c[i+2]=='e'))

{

d[j]='d';

j++;

d[j]='l';

j++;

d[j]='e';

j++;

m=m+3;

}

d[j]=c[i];

}

m=m+6;

m++;

d[m]='d';

m++;

d[m]='l';

m++;

d[m]='e';

m++;

d[m]='e';

m++;

d[m]='t';

m++;

d[m]='x';

m++;

printf("\n\n transmitted data: \n");

for(i=0;i<m;i++)

{

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

}

for(i=6,j=0;i<m-6;i++,j++)

{

if(d[i]=='d'&&d[i+1]=='l'&&d[i+2]=='e'&&d[i+3]=='d'&&d[i+4]=='l'&&d[i+5]=='e')

i=i+3;

t[j]=d[i];

}

printf("\n\nreceived data:");

for(i=0;i<j;i++)

{printf("%c",t[i]);

}

}

Output:

Aim:

Implement on a data set of characters the CRC polynomials.

Theory

CRC means Cyclic Redundancy Check. It is the most famous and traditionally successful mechanism

used in error detection through the parity bits installed within the data and obtaining checksum which

acts as the verifier to check whether the data retreived at the reciever end is genuine or not. Various

operations are involved in implementing CRC on a data set through CRC generating polynomials. In

the program, I have also provided the user to opt for Error detection whereby he can proceed for it.

Understand the program below as it is much simpler than pretented to be so.

Program:

#include

#include

#include

#define N strlen(g)

char t[128], cs[128], g[]="100010000";

int a, e, c;

void xor() {

for(c=1;c}

void crc() {

for(e=0;e do {

if(cs[0]=='1') xor();

for(c=0;c cs[c]=t[e++];

}while(e<=a+N-1);

}

void main() {

clrscr();

printf("\nEnter poly : "); scanf("%s",t);

printf("\nGenerating Polynomial is : %s",g);

a=strlen(t);

for(e=a;e

printf("\nModified t[u] is : %s",t);

crc();

printf("\nChecksum is : %s",cs);

for(e=a;e printf("\nFinal Codeword is : %s",t);

printf("\nTest Error detection 0(yes) 1(no) ? : ");

scanf("%d",&e);

if(e==0) {

printf("Enter position where error is to inserted : ");

scanf("%d",&e);

t[e]=(t[e]=='0')?'1':'0';

printf("Errorneous data : %s\n",t);

}

crc();

for (e=0;(e<n-1)&&(cs[e]!='1');e++);

if(e else printf("No Error Detected.");

getch();

}

Output:

Aim:

Implement Dijkstra's algorithm to compute the Shortest path through a graph.

Theory

Dijkstra's algorithm is a non-adaptive routing algorithm which is very widely used to route packets

from source to detination through various routers available during the transmission. It is implemented

at the network layer of the architecture where data packets are sent through routers which maitain

routing tables that help to denote the exact location to where the destined packets need to be

delivered. Major advantage in using Dijkstra's algorithm is that it forwards the data packets from

source to destination through the most optimized path in terms of both the distance and cost observed.

It prompts the user to enter the number of nodes and the source and destination nodes among them. In

addition, the algorithm written below also asks for the neighbours to each node with the distances to

reach to them from each node is also prompted. All this data is stored and used further to calculate

and estimate the best path possible for data packets to reach their destination from source. Program

below explains it in a much better way.

Program:

#include

#include

int n,s,nb,nbs[15],snbs[15],delay[15][15],i,j,temp[15],ze=0;

void min();

void main()

{

clrscr();

printf("Enter the no.of nodes:");

scanf("%d",&n);

printf("\nEnter the source node:");

scanf("%d",&s);

printf("\nEnter the no.of Neighbours to %d:",s);

scanf("%d",&nb);

printf("\nEnter the Neighbours:");

for(i=1;i<=nb;i++)

scanf("%d",&nbs[i]);

printf("\nEnter the timedelay form source to nbs:");

for(i=1;i<=nb;i++)

scanf("%d",&snbs[i]);

for(i=1;i<=nb;i++)

{

printf("\nEnter the timedelay of %d: ",nbs[i]);

for(j=1;j<=n;j++)

scanf("%d",&delay[i][j]);

}

for(i=1;i<=nb;i++)

{

printf("\nThe timedelays of %d: ",nbs[i]);

for(j=1;j<=n;j++)

printf("%3d",delay[i][j]);

}

min();

getch();

}

void min()

{

int sum,k,y=1,store=1;

printf("\n\t\t\tnew- rout");

printf("\n\t\t\ttime-");

printf("\n\t\t\tdelay");

printf("\n");

for(i=1;i<=n;i++)

{

sum=0;

k=1;

for(j=1;j<=nb;j++)

{

temp[k++]=delay[j][i];

}

sum=temp[1]+snbs[1];

for(y=2;y<=nb;y++)

{

if(sum>temp[y]+snbs[y])

{

sum=temp[y]+snbs[y];

store=y;

}

}

if(s==i)

printf("\n\t%d+\t%d =\t%d --",ze,ze,ze);

else

printf("\n\t%d +\t%d =\t%d\t%d",temp[store],snbs[store],sum,nbs[store]);

}

}

Output:

Aim:

Take an example subnet graph with weights indicating delay between nodes. Now obtain

Routing table at each node using Distance Vector Routing.

Theory

Distance Vector routing (DVR) algorithm is unlike Dijkstra's algorithm which is a non-adaptive

routing algorithm and means that it is purely static, that is pre-destined and fixed, not flexible in

networks where congestions are more prone to occur. DVR is an adaptive routing algorithm in which

the information from neighbours is maitained well by each and every node and this helps us to

determine the simplest path possible in a changing network. Though, one of the node may fail, still,

the destined node is reachable through other possible intermediate nodes that are found out by the

DVR algorithm. The perfectly executing program below shows it live below.

Program:

#include

struct node

{

unsigned dist[20];

unsigned from[20];

}rt[10];

int main()

{

int dmat[20][20];

int n,i,j,k,count=0;

printf("\nEnter the number of nodes : ");

scanf("%d",&n);

printf("\nEnter the cost matrix :\n");

for(i=0;i<n;i++)

for(j=0;j<n;j++)

{

scanf("%d",&dmat[i][j]);

dmat[i][i]=0;

rt[i].dist[j]=dmat[i][j];

rt[i].from[j]=j;

}

do

{

count=0;

for(i=0;i<n;i++)

for(j=0;j<n;j++)

for(k=0;k<n;k++)

if(rt[i].dist[j]>dmat[i][k]+rt[k].dist[j])

{

rt[i].dist[j]=rt[i].dist[k]+rt[k].dist[j];

rt[i].from[j]=k;

count++;

}

}while(count!=0);

for(i=0;i<n;i++)

{

printf("\n\nState value for router %d is \n",i+1);

for(j=0;j<n;j++)

{

printf("\t\nnode %d via %d Distance%d",j+1,rt[i].from[j]+1,rt[i].dist[j]);

}

}

printf("\n\n");

}

Aim:

Take an example subnet of hosts. Obtain broadcast tree for it.

Theory

IP addressing is the allocation of unique ID to each and every system connected in a network to

maintan communication among them through out the affixed network. There are 5 classes of IP

Addresses namely A through E with the range varying from one class to the other class.

A subnet is a network allocation to similar systems or same hierarchial systems present in a allocated

network like an organisation. Each and every system can be reachd through a client-server computing

environment where the server acts as the Master and the clients acts as the Slaves to form a Master-

Slave computing environment. Below programs show the calculation of network addresses with

subnet predefinition and subnet generation.

Program:

a)Network Address:

#include

#include

void main()

{

unsigned int compad[4];

unsigned int mask[4];

unsigned int netadr[4];

int i;

clrscr();

printf("Enter the ip address:\n");

scanf("%u%*c%u%*c%u%*c%u%*c",&compad[3],&compad[2],&compad[1],&compad[0]);

printf("Enter the subnet address:\n");

scanf("%u%*c%u%*c%u%*c%u%*c",&mask[3],&mask[2],&mask[1],&mask[0]);

for(i=0;i<4;i++)

{

netadr[i]= compad[i]&mask[i];

}

printf("\nNetwork address is:\n");

printf("%u.%u.%u.%u",netadr[3],netadr[2],netadr[1],netadr[0]);

printf("\nsubnet address is:\n");

printf("%u.%u.%u.%u",mask[3],mask[2],mask[1],mask[0]);

printf("\nip address is:\n");

printf("%u.%u.%u.%u",compad[3],compad[2],compad[1],compad[0]);

getch();

}

Output:

b)Network address with automatic subnet address generation:

#include

#include

void main()

{

unsigned int compad[4];

unsigned int mask[4];

unsigned int netadr[4];

unsigned long int ma=0;

int i,pre;

clrscr();

printf("Enter the ip address:\n");

scanf("%u%*c%u%*c%u%*c%u%*c",&compad[3],&compad[2],&compad[1],&compad[0]);

printf("Enter the prefix:\n");

scanf("%ul",&pre);

for(i=(32-pre);i<32;i++)

ma=ma|(1<<i);

for(i=0;i<4;i++)

{

mask[i]=ma%256;

ma=ma/256;

}

for(i=0;i<4;i++)

{

netadr[i]= compad[i]&mask[i];

}

printf("\nNetwork address is:\n");

printf("%u.%u.%u.%u",netadr[3],netadr[2],netadr[1],netadr[0]);

printf("\nsubnet address is:\n");

printf("%u.%u.%u.%u",mask[3],mask[2],mask[1],mask[0]);

printf("\nip address is:\n");

printf("%u.%u.%u.%u",compad[3],compad[2],compad[1],compad[0]);

getch();

}

Output:

6. 64 BIT PLAYING TEXT AND ENCRYPT THE SAME USING DES ALGORITHM.

#include "custom_header.h"

#include <stdio.h>

#include <stdlib.h>

int key_shift_sizes[] = {-1, 1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1};

int key_permutation[] = {

57, 49, 41, 33, 25, 17, 9,

1, 58, 50, 42, 34, 26, 18,

10, 2, 59, 51, 43, 35, 27,

19, 11, 3, 60, 52, 44, 36,

63, 55, 47, 39, 31, 23, 15,

7, 62, 54, 46, 38, 30, 22,

14, 6, 61, 53, 45, 37, 29,

21, 13, 5, 28, 20, 12, 4 };

int message_permutation[] = {

58, 50, 42, 34, 26, 18, 10, 2,

60, 52, 44, 36, 28, 20, 12, 4,

62, 54, 46, 38, 30, 22, 14, 6,

64, 56, 48, 40, 32, 24, 16, 8,

57, 49, 41, 33, 25, 17, 9, 1,

59, 51, 43, 35, 27, 19, 11, 3,

61, 53, 45, 37, 29, 21, 13, 5,

63, 55, 47, 39, 31, 23, 15, 7 };

int sub_key_permutation[] = {

14, 17, 11, 24, 1, 5,

3, 28, 15, 6, 21, 10,

23, 19, 12, 4, 26, 8,

16, 7, 27, 20, 13, 2,

41, 52, 31, 37, 47, 55,

30, 40, 51, 45, 33, 48,

44, 49, 39, 56, 34, 53,

46, 42, 50, 36, 29, 32 };

int message[] = {

32, 1, 2, 3, 4, 5,

4, 5, 6, 7, 8, 9,

8, 9, 10, 11, 12, 13,

12, 13, 14, 15, 16, 17,

16, 17, 18, 19, 20, 21,

20, 21, 22, 23, 24, 25,

24, 25, 26, 27, 28, 29,

28, 29, 30, 31, 32, 1 };

int array_a1[] = {

14, 4, 13, 1, 2, 15, 11, 8, 3, 10, 6, 12, 5, 9, 0, 7,

0, 15, 7, 4, 14, 2, 13, 1, 10, 6, 12, 11, 9, 5, 3, 8,

4, 1, 14, 8, 13, 6, 2, 11, 15, 12, 9, 7, 3, 10, 5, 0,

15, 12, 8, 2, 4, 9, 1, 7, 5, 11, 3, 14, 10, 0, 6, 13 };

int array_a2[] = {

15, 1, 8, 14, 6, 11, 3, 4, 9, 7, 2, 13, 12, 0, 5, 10,

3, 13, 4, 7, 15, 2, 8, 14, 12, 0, 1, 10, 6, 9, 11, 5,

0, 14, 7, 11, 10, 4, 13, 1, 5, 8, 12, 6, 9, 3, 2, 15,

13, 8, 10, 1, 3, 15, 4, 2, 11, 6, 7, 12, 0, 5, 14, 9 };

int array_a3[] = {

10, 0, 9, 14, 6, 3, 15, 5, 1, 13, 12, 7, 11, 4, 2, 8,

13, 7, 0, 9, 3, 4, 6, 10, 2, 8, 5, 14, 12, 11, 15, 1,

13, 6, 4, 9, 8, 15, 3, 0, 11, 1, 2, 12, 5, 10, 14, 7,

1, 10, 13, 0, 6, 9, 8, 7, 4, 15, 14, 3, 11, 5, 2, 12 };

int array_a4[] = {

7, 13, 14, 3, 0, 6, 9, 10, 1, 2, 8, 5, 11, 12, 4, 15,

13, 8, 11, 5, 6, 15, 0, 3, 4, 7, 2, 12, 1, 10, 14, 9,

10, 6, 9, 0, 12, 11, 7, 13, 15, 1, 3, 14, 5, 2, 8, 4,

3, 15, 0, 6, 10, 1, 13, 8, 9, 4, 5, 11, 12, 7, 2, 14 };

int array_a5[] = {

2, 12, 4, 1, 7, 10, 11, 6, 8, 5, 3, 15, 13, 0, 14, 9,

14, 11, 2, 12, 4, 7, 13, 1, 5, 0, 15, 10, 3, 9, 8, 6,

4, 2, 1, 11, 10, 13, 7, 8, 15, 9, 12, 5, 6, 3, 0, 14,

11, 8, 12, 7, 1, 14, 2, 13, 6, 15, 0, 9, 10, 4, 5, 3 };

int array_a6[] = {

12, 1, 10, 15, 9, 2, 6, 8, 0, 13, 3, 4, 14, 7, 5, 11,

10, 15, 4, 2, 7, 12, 9, 5, 6, 1, 13, 14, 0, 11, 3, 8,

9, 14, 15, 5, 2, 8, 12, 3, 7, 0, 4, 10, 1, 13, 11, 6,

4, 3, 2, 12, 9, 5, 15, 10, 11, 14, 1, 7, 6, 0, 8, 13 };

int array_a7[] = {

4, 11, 2, 14, 15, 0, 8, 13, 3, 12, 9, 7, 5, 10, 6, 1,

13, 0, 11, 7, 4, 9, 1, 10, 14, 3, 5, 12, 2, 15, 8, 6,

1, 4, 11, 13, 12, 3, 7, 14, 10, 15, 6, 8, 0, 5, 9, 2,

6, 11, 13, 8, 1, 4, 10, 7, 9, 5, 0, 15, 14, 2, 3, 12 };

int array_a8[] = {

13, 2, 8, 4, 6, 15, 11, 1, 10, 9, 3, 14, 5, 0, 12, 7,

1, 15, 13, 8, 10, 3, 7, 4, 12, 5, 6, 11, 0, 14, 9, 2,

7, 11, 4, 1, 9, 12, 14, 2, 0, 6, 10, 13, 15, 3, 5, 8,

2, 1, 14, 7, 4, 10, 8, 13, 15, 12, 9, 0, 3, 5, 6, 11 };

int intermediate_permutation[] = {

16, 7, 20, 21,

29, 12, 28, 17,

1, 15, 23, 26,

5, 18, 31, 10,

2, 8, 24, 14,

32, 27, 3, 9,

19, 13, 30, 6,

22, 11, 4, 25 };

int end_permutation[] = {

40, 8, 48, 16, 56, 24, 64, 32,

39, 7, 47, 15, 55, 23, 63, 31,

38, 6, 46, 14, 54, 22, 62, 30,

37, 5, 45, 13, 53, 21, 61, 29,

36, 4, 44, 12, 52, 20, 60, 28,

35, 3, 43, 11, 51, 19, 59, 27,

34, 2, 42, 10, 50, 18, 58, 26,

33, 1, 41, 9, 49, 17, 57, 25 };

void make_keys(unsigned char* key)

{

int count;

for(count = 0; count < 8; count++)

{

key[count] = rand()%255;

}

}

void binary_digits(char input)

{

int count;

for(count = 0; count < 8; count++)

{

char shift_byte = 0x01 << (7 - count);

if(shift_byte & input)

{

printf("1");

}

else

{

printf("0");

}

}

}

void display_keys(key_set key_set)

{

int count;

printf("Y: \n");

for(count = 0; count < 8; count++)

{

printf("%02X : ", key_set.temp[count]);

binary_digits(key_set.temp[count]);

printf("\n");

}

printf("\nX: \n");

for(count = 0; count < 4; count++)

{

printf("%02X : ", key_set.y[count]);

binary_digits(key_set.y[count]);

printf("\n");

}

printf("\nZ: \n");

for(count = 0; count < 4; count++)

{

printf("%02X : ", key_set.z[count]);

binary_digits(key_set.z[count]);

printf("\n");

}

printf("\n");

}

void key_maker(unsigned char* main_key, key_set* key_sets)

{

int m, n;

int shift_size;

unsigned char shift_byte, first_bit, second_bit, third_bit, fourth_bit;

for(m = 0; m < 8; m++)

{

key_sets[0].temp[m] = 0;

}

for(m = 0; m < 56; m++)

{

shift_size = key_permutation[m];

shift_byte = 0x80 >> ((shift_size - 1)%8);

shift_byte &= main_key[(shift_size - 1)/8];

shift_byte <<= ((shift_size - 1)%8);

key_sets[0].temp[m/8] |= (shift_byte >> m%8);

}

for(m = 0; m < 3; m++)

{

key_sets[0].y[m] = key_sets[0].temp[m];

}

key_sets[0].y[3] = key_sets[0].temp[3] & 0xF0;

for(m = 0; m < 3; m++)

{

key_sets[0].z[m] = (key_sets[0].temp[m + 3] & 0x0F) << 4;

key_sets[0].z[m] |= (key_sets[0].temp[m + 4] & 0xF0) >> 4;

}

key_sets[0].z[3] = (key_sets[0].temp[6] & 0x0F) << 4;

for(m = 1; m < 17; m++)

{

for(n = 0; n < 4; n++)

{

key_sets[m].y[n] = key_sets[m - 1].y[n];

key_sets[m].z[n] = key_sets[m - 1].z[n];

}

shift_size = key_shift_sizes[i];

if(shift_size == 1)

{

shift_byte = 0x80;

}

else

{

shift_byte = 0xC0;

}

first_bit = shift_byte & key_sets[m].y[0];

second_bit = shift_byte & key_sets[m].y[1];

third_bit = shift_byte & key_sets[m].y[2];

fourth_bit = shift_byte & key_sets[m].y[3];

key_sets[m].y[0] <<= shift_size;

key_sets[m].y[0] |= (second_bit >> (8 - shift_size));

key_sets[m].y[1] <<= shift_size;

key_sets[m].y[1] |= (third_bit >> (8 - shift_size));

key_sets[m].y[2] <<= shift_size;

key_sets[m].y[2] |= (fourth_bit >> (8 - shift_size));

key_sets[m].y[3] <<= shift_size;

key_sets[m].y[3] |= (first_bit >> (4 - shift_size));

first_bit = shift_byte & key_sets[m].z[0];

second_bit = shift_byte & key_sets[m].z[1];

third_bit = shift_byte & key_sets[m].z[2];

fourth_bit = shift_byte & key_sets[m].z[3];

key_sets[m].z[0] <<= shift_size;

key_sets[m].z[0] |= (second_bit >> (8 - shift_size));

key_sets[m].z[1] <<= shift_size;

key_sets[m].z[1] |= (third_bit >> (8 - shift_size));

key_sets[m].z[2] <<= shift_size;

key_sets[m].z[2] |= (fourth_bit >> (8 - shift_size));

key_sets[m].z[3] <<= shift_size;

key_sets[m].z[3] |= (first_bit >> (4 - shift_size));

for(n = 0; n < 48; n++)

{

shift_size = sub_key_permutation[n];

if(shift_size <= 28)

{

shift_byte = 0x80 >> ((shift_size - 1)%8);

shift_byte &= key_sets[m].y[(shift_size - 1)/8];

shift_byte <<= ((shift_size - 1)%8);

}

else

{

shift_byte = 0x80 >> ((shift_size - 29)%8);

shift_byte &= key_sets[m].z[(shift_size - 29)/8];

shift_byte <<= ((shift_size - 29)%8);

}

key_sets[m].temp[n/8] |= (shift_byte >> n%8);

}

}

}

void evaluate_message(unsigned char* message_piece, unsigned char*

processed_piece, key_set* key_sets, int mode)

{

unsigned char initial_permutation[8], final_permutation[8];

unsigned char row, column, shift_byte;

unsigned char ln[4], rn[4], er[6], ser[4], l[4], r[4];

int count, temp, key_position, shift_size;

memset(initial_permutation, 0, 8);

memset(processed_piece, 0, 8);

for(count = 0; count < 64; count++)

{

shift_size = message_permutation[count];

shift_byte = 0x80 >> ((shift_size - 1)%8);

shift_byte &= message_piece[(shift_size - 1)/8];

shift_byte <<= ((shift_size - 1)%8);

initial_permutation[count/8] |= (shift_byte >> count%8);

}

for(count = 0; count < 4; count++)

{

l[count] = initial_permutation[count];

r[count] = initial_permutation[count + 4];

}

for(temp = 1; temp <= 16; temp++)

{

memcpy(ln, r, 4);

memset(er, 0, 6);

for(count = 0; count < 48; count++)

{

shift_size = message[count];

shift_byte = 0x80 >> ((shift_size - 1)%8);

shift_byte &= r[(shift_size - 1)/8];

shift_byte <<= ((shift_size - 1)%8);

er[count/8] |= (shift_byte >> count%8);

}

if(mode == DECRYPTION_MODE)

{

key_position = 17 - temp;

}

else

{

key_position = temp;

}

for(count = 0; count < 6; count++)

{

er[count] ^= key_sets[key_position].temp[count];

}

for(count = 0; count < 4; count++)

{

ser[count] = 0;

}

row = 0;

row |= ((er[0] & 0x80) >> 6);

row |= ((er[0] & 0x04) >> 2);

column = 0;

column |= ((er[0] & 0x78) >> 3);

ser[0] |= ((unsigned char)array_a1[row*16 + column] << 4);

row = 0;

row |= (er[0] & 0x02);

row |= ((er[1] & 0x10) >> 4);

column = 0;

column |= ((er[0] & 0x01) << 3);

column |= ((er[1] & 0xE0) >> 5);

ser[0] |= (unsigned char)array_a2[row*16 + column];

row = 0;

row |= ((er[1] & 0x08) >> 2);

row |= ((er[2] & 0x40) >> 6);

column = 0;

column |= ((er[1] & 0x07) << 1);

column |= ((er[2] & 0x80) >> 7);

ser[1] |= ((unsigned char)array_a3[row*16 + column] << 4);

row = 0;

row |= ((er[2] & 0x20) >> 4);

row |= (er[2] & 0x01);

column = 0;

column |= ((er[2] & 0x1E) >> 1);

ser[1] |= (unsigned char)array_a4[row*16 + column];

row = 0;

row |= ((er[3] & 0x80) >> 6);

row |= ((er[3] & 0x04) >> 2);

column = 0;

column |= ((er[3] & 0x78) >> 3);

ser[2] |= ((unsigned char)array_a5[row*16 + column] << 4);

row = 0;

row |= (er[3] & 0x02);

row |= ((er[4] & 0x10) >> 4);

column = 0;

column |= ((er[3] & 0x01) << 3);

column |= ((er[4] & 0xE0) >> 5);

ser[2] |= (unsigned char)array_a6[row*16 + column];

row = 0;

row |= ((er[4] & 0x08) >> 2);

row |= ((er[5] & 0x40) >> 6);

column = 0;

column |= ((er[4] & 0x07) << 1);

column |= ((er[5] & 0x80) >> 7);

ser[3] |= ((unsigned char)array_a7[row*16 + column] << 4);

row = 0;

row |= ((er[5] & 0x20) >> 4);

row |= (er[5] & 0x01);

column = 0;

column |= ((er[5] & 0x1E) >> 1);

ser[3] |= (unsigned char)array_a8[row*16 + column];

for(count = 0; count < 4; count++)

{

rn[count] = 0;

}

for(count = 0; count < 32; count++)

{

shift_size = intermediate_permutation[count];

shift_byte = 0x80 >> ((shift_size - 1)%8);

shift_byte &= ser[(shift_size - 1)/8];

shift_byte <<= ((shift_size - 1)%8);

rn[count/8] |= (shift_byte >> count%8);

}

for(count = 0; count < 4; count++)

{

rn[count] ^= l[count];

}

for(count = 0; count < 4; count++)

{

l[count] = ln[count];

r[count] = rn[count];

}

}

for(count = 0; count < 4; count++)

{

final_permutation[count] = r[count];

final_permutation[4 + count] = l[count];

}

for(count = 0; count < 64; count++)

{

shift_size = end_permutation[count];

shift_byte = 0x80 >> ((shift_size - 1)%8);

shift_byte &= final_permutation[(shift_size - 1)/8];

shift_byte <<= ((shift_size - 1)%8);

processed_piece[count/8] |= (shift_byte >> count%8);

}

}

#include<stdio.h> int main() { int array_a1[30], array_a2[30], array_a3[30], array_a4[30], array_a5[30], array_a6[30], array_a7[30], array_a8[30]; int div, count, j, key, m, plaintext, temp, dec = 0; printf("\nEnter a Plain-Text value:\t"); scanf("%d", &plaintext); printf("\nEnter the Key:\t"); scanf("%d", &key); printf("\nEnter the Bit-Stream\n"); for(count = 0; count < plaintext; count++) { scanf("%d", &array_a1[count]); } div = plaintext / 2; temp = div - key; for(count = 0; count <= temp; count++) { array_a3[count] = 0; dec++; } dec = dec - 1; printf("Enter the Key Bit Stream\n"); for(count = 0; count < key; count++) { scanf("%d", &array_a3[dec++]); } for(count = 0; count < 2; count++) { printf("%d", array_a8[count]); }

printf("Left Hand\n"); for(count = 0; count < div; count++) { array_a5[count] = array_a1[count]; printf("%d", array_a1[count]); } printf("Right Hand\n"); for(count = div; count < plaintext; count++) { array_a2[count] = array_a1[count]; printf("%d", array_a1[count]); } for(j = 0, m = div; j < dec, m < plaintext; j++, m++) { if(array_a2[m] == 1 && array_a3[j] == 1) { array_a6[j] = 0; } else if(array_a2[m] == 1 && array_a3[j] == 0) { array_a6[j] = m; } else { array_a6[j] = 0; } } printf("\nFirst XOR\n"); for(count = 0; count < div; count++) { printf("%d", array_a6[count]); } for(j = 0, m = 0; j < div, j++; j++, m++) { if(array_a5[m] = 1 && array_a6[j] == 1) { array_a4[j] = 0; } else if(array_a5[m] = 1 && array_a6[j] == 0) { array_a4[j] = m; } else if(array_a5[m] == 0 && array_a6[j] == 1) { array_a4[j] = 0; } } printf("\nSecond XOR\n"); for(count = 0; count < div; count++) {

printf("%d", array_a4[j]); } return 0; }

b. Write a program to break the above DES coding

#include<stdio.h> int main() { int array_a1[30], array_a2[30], array_a3[30], array_a4[30], array_a5[30], array_a6[30], array_a7[30], array_a8[30]; int div, count, j, key, m, plaintext, temp, dec = 0; printf("\nEnter a Plain-Text value:\t"); scanf("%d", &plaintext); printf("\nEnter the Key:\t"); scanf("%d", &key); printf("\nEnter the Bit-Stream\n"); for(count = 0; count < plaintext; count++) { scanf("%d", &array_a1[count]); } div = plaintext / 2; temp = div - key; for(count = 0; count <= temp; count++) { array_a3[count] = 0; dec++; } dec = dec - 1; printf("Enter the Key Bit Stream\n"); for(count = 0; count < key; count++) { scanf("%d", &array_a3[dec++]); } for(count = 0; count < 2; count++) { printf("%d", array_a8[count]); } printf("Left Hand\n"); for(count = 0; count < div; count++) { array_a5[count] = array_a1[count]; printf("%d", array_a1[count]); } printf("Right Hand\n"); for(count = div; count < plaintext; count++) { array_a2[count] = array_a1[count]; printf("%d", array_a1[count]);

} for(j = 0, m = div; j < dec, m < plaintext; j++, m++) { if(array_a2[m] == 1 && array_a3[j] == 1) { array_a6[j] = 0; } else if(array_a2[m] == 1 && array_a3[j] == 0) { array_a6[j] = m; } else { array_a6[j] = 0; } } printf("\nFirst XOR\n"); for(count = 0; count < div; count++) { printf("%d", array_a6[count]); } for(j = 0, m = 0; j < div, j++; j++, m++) { if(array_a5[m] = 1 && array_a6[j] == 1) { array_a4[j] = 0; } else if(array_a5[m] = 1 && array_a6[j] == 0) { array_a4[j] = m; } else if(array_a5[m] == 0 && array_a6[j] == 1) { array_a4[j] = 0; } } printf("\nSecond XOR\n"); for(count = 0; count < div; count++) { printf("%d", array_a4[j]); } return 0;

}

Enter 10 bits input:1100011100

Your p10 key is :6,7,8,9,10,1,2,3,4,5,

Bits after p10 :1110011000

Output after LS-1 :1100110001

Your p8 key is :6,7,8,9,1,2,3,4,

Your key k1 is :10001100

7. RSA algorithm encrypts a text data and Decrypt the same.

#include<stdio.h>

#include<math.h>

//to find gcd

int gcd(int a, int h)

{

int temp;

while(1)

{

temp = a%h;

if(temp==0)

return h;

a = h;

h = temp;

}

}

int main()

{

//2 random prime numbers

double p = 3;

double q = 7;

double n=p*q;

double count;

double totient = (p-1)*(q-1);

//public key

//e stands for encrypt

double e=2;

//for checking co-prime which satisfies e>1

while(e<totient){

count = gcd(e,totient);

if(count==1)

break;

else

e++;

}

//private key

//d stands for decrypt

double d;

//k can be any arbitrary value

double k = 2;

//choosing d such that it satisfies d*e = 1 + k * totient

d = (1 + (k*totient))/e;

double msg = 12;

double c = pow(msg,e);

double m = pow(c,d);

c=fmod(c,n);

m=fmod(m,n);

printf("Message data = %lf",msg);

printf("\np = %lf",p);

printf("\nq = %lf",q);

printf("\nn = pq = %lf",n);

printf("\ntotient = %lf",totient);

printf("\ne = %lf",e);

printf("\nd = %lf",d);

printf("\nEncrypted data = %lf",c);

printf("\nOriginal Message Sent = %lf",m);

return 0;

}

Output:

Message data=12

P=3

Q=7

N=pq=21

Totient=12

E=5

D=5

Encrypted dat=3

Original data=3