0% found this document useful (0 votes)
85 views50 pages

AIM: Write A C Program To Implement Conventional Encryption Technique

The document describes encryption of a 64-bit plaintext using the Data Encryption Standard (DES) algorithm. It provides details on the key steps of DES encryption: initial permutation of the plaintext, addition of a 48-bit round key, expansion and substitution of bits using S-boxes, and final permutation to obtain the ciphertext. Pseudocode and description of functions for generating round keys from the main key and performing a single round of encryption are also included.

Uploaded by

Kaustubh Satpute
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
85 views50 pages

AIM: Write A C Program To Implement Conventional Encryption Technique

The document describes encryption of a 64-bit plaintext using the Data Encryption Standard (DES) algorithm. It provides details on the key steps of DES encryption: initial permutation of the plaintext, addition of a 48-bit round key, expansion and substitution of bits using S-boxes, and final permutation to obtain the ciphertext. Pseudocode and description of functions for generating round keys from the main key and performing a single round of encryption are also included.

Uploaded by

Kaustubh Satpute
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 50

Page No.

AIM: Write a C Program to implement Conventional encryption technique


using
a) mono alphabetic encryption.
b) transposition cipher technique.
DESCRIPTION:
Subtitution Method: In a substitution cipher each letter or group of letters is
replaced by another letter or group of letters to disguise it.
Encryption:
In mono alphabetic encryption technique key(integer) is added to each
character of plain text to obtain cipher text.
Decryption:
The key is subtracted from each character of cipher text received to obtain
plain text.
Example:
Let plain text be hello and key value is 1 then
Encryption (plain text,key) : h+1,e+1,. i.e. cipher text is ifmmp.
Decryption (cipher text,key) : i-1,f-1,. i.e. plain text is hello .
Transposition Method: in transposition methods letters are reordered based
on key information to obtain cipher text.
Example:
Encryption: Plain text : welcome Key : bacd
Columns : length of key (i.e. 3)
Rows : length of plain text / length of key + 0 ( if length of plain text % length
of key is 0 ) other wise 1
a b c d
w e l c
o m e $
According to key the matrix colums are rearranged as
b a c d
e w l c
m o e $
Joining characters column wise we obtain cipher text as emwolec$
Decryption: Cipher text : emwolec$ Key : bacd
Colums and rows are computed as specified in encryption procedure
Received cipher text is arranged in matrix as

Page No.

b a c d
e w l c
m o e $
Reordering columns based on key as
a b c d
w e l c
o m e $
Joining characters row wise and discarding special symbols we obtain plain
text as welcome

Page No.

PROGRAM:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
void sencrypt(char *pt,char *ct,int k)
{
int i;
for(i=0;pt[i]!='\0';i++)
ct[i]=pt[i]+k;
ct[i]='\0';
}
void sdecrypt(char *ct,char *pt,int k)
{
int i;
for(i=0;ct[i]!='\0';i++)
pt[i]=ct[i]-k;
pt[i]='\0';
}
void tencrypt(char *pt,char *ct,char *k)
{
int c=strlen(k),lpt=strlen(pt),r=lpt/c,i,j,n;
char emat[100][100],ch;
if(lpt%c!=0)
r++;
for(i=0,n=0;i<r;i++)
for(j=0;j<c;j++)
if(n<lpt)
emat[i][j]=pt[n++];
else
emat[i][j]='$';
for(i=0;i<c-1;i++)
for(j=i+1;j<c;j++)
if(k[i]>k[j])
{
ch=k[i];
k[i]=k[j];
k[j]=ch;
for(n=0;n<r;n++)
{
ch=emat[n][i];

Page No.

emat[n][i]=emat[n][j];
emat[n][j]=ch;
}
}
for(i=0,n=0;i<c;i++)
for(j=0;j<r;j++)
ct[n++]=emat[j][i];
ct[n]='\0';
}
void tdecrypt(char *ct,char *pt,char *k)
{
int c=strlen(k),r=strlen(ct)/c,i,j,n,in,tik[100];
char ch,emat[100][100];
for(i=0;i<c;i++)
tik[i]=i;
for(i=0,n=0;i<c-1;i++)
for(j=i+1;j<c;j++)
if(k[i]>k[j])
{
in=tik[i];
tik[i]=tik[j];
tik[j]=in;
ch=k[i];
k[i]=k[j];
k[j]=ch;
}
for(i=0,n=0;i<c;i++)
for(j=0;j<r;j++)
emat[j][tik[i]]=ct[n++];
for(i=0,n=0;i<r;i++)
for(j=0;j<c;j++)
if(emat[i][j]!='$')
pt[n++]=emat[i][j];
else
break;
pt[n]='\0';
}
void main()
{
char pt[100],k[50],ct[100];
int ch;

Page No.

clrscr();
printf("Enter 1 for Transposition method else any other for subsitution
method\n");
scanf("%d",&ch);
printf("Enter plain Text : ");
fflush(stdin);
gets(pt);
printf("Enter key information : ");
fflush(stdin);
gets(k);
if(ch!=1)
sencrypt(pt,ct,atoi(k));
else
tencrypt(pt,ct,strdup(k));
printf("The cipher text is : ");
puts(ct);
if(ch!=1)
sdecrypt(ct,pt,atoi(k));
else
tdecrypt(ct,pt,k);
printf("The plain text is : ");
puts(pt);
getch();
}

OUTPUT:
1)
Enter 1 for Transposition method else any other for subsitution method
0

Page No.

Enter plain Text : hello


Enter key information : 1
The cipher text is : ifmmp
The plain text is : hello
2)
Enter 1 for Transposition method else any other for subsitution method
1
Enter plain Text : welcome
Enter key information : bacd
The cipher text is : emwolec$
The plain text is : welcome

AIM: To encrypt a 64-bit plain text using DES Algorithm


DESCRIPTION:
There are two inputs to the encryption function: the plaintext to be encrypted and
the key. In this case, the plaintext must be 64 bits in length and the key is 56 bits in
length.

Page No.

General Depiction of DES Encryption Algorithm

Internal structure of single round

Page No.

Above figure shows the internal structure of a single round. Again, begin by focusing
on the left-hand side of the diagram. The left and right halves of each 64-bit
intermediate value are treated as separate 32-bit quantities, labeled L (left) and R
(right). As in any classic Feistel cipher, the overall processing at each round can be
summarized in the following formulas:

Li = Ri-1
Ri = Li-1 x F(Ri-1, Ki)
The round key Ki is 48 bits. The R input is 32 bits. This R input is first expanded to 48 bits by
using a table that defines a permutation plus an expansion that involves duplication of 16 of the
R bits. The resulting 48 bits are XORed with Ki. This 48-bit result passes through a substitution
function that produces a 32-bit output, which is permuted again.

PROGRAM:

Page No.

#include<stdio.h>
void ithkey(int *key,int *k48bit,int no)
{
static int pc1[8][7]= {{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}};
static int tk[56],nost,i,j,n,temp;
static int pc2[6][8]= {{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}};
if(no==1)
{
for(i=0,n=0;i<8;i++)
for(j=0;j<7;j++)
tk[n++]=key[pc1[i][j]-1];
}
if(no==1||no==2||no==9||no==16)
nost=1;
else
nost=2;
for(n=0;n<nost;n++)
{
temp=tk[0];
for(i=0;i<27;i++)
tk[i]=tk[i+1];
tk[27]=temp;
temp=tk[28];
for(i=28;i<55;i++)
tk[i]=tk[i+1];
tk[55]=temp;
}
for(i=0,n=0;i<6;i++)
for(j=0;j<8;j++)
k48bit[n++]=tk[pc2[i][j]-1];

Page No.

}
void DESROUND(int *ipdat,int *key)
{
static int exp[8][6]= {{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}};
static int tdat[48],i,j,n;
static int s[8][4][16]=
{
{{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}},
{{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}},
{{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}},
{{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}},
{{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}},
{{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}},
{{4,11,2,14,15,0,8,13,3,12,9,7,5,10,6,1},

Page No.

{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}},
{{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}}
};
static int sbox[8][4],r,c,k;
static int pbox[8][4]= {{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}};
static int pbdat[32];
for(i=0,n=0;i<8;i++)
for(j=0;j<6;j++)
tdat[n++]=ipdat[exp[i][j]+31];
for(i=0;i<48;i++)
tdat[i]=(tdat[i]&!key[i])|(!tdat[i]&key[i]);
for(i=0,j=0;i<8;i++,j+=6)
{
r=tdat[j]*2+tdat[j+5];
c=tdat[j+1]*8+tdat[j+2]*4+tdat[j+3]*2+tdat[j+4];
n=s[i][r][c];
for(k=3;n!=0;k--)
{
sbox[i][k]=n%2;
n=n/2;
}
for(;k>=0;k--)
sbox[i][k]=0;
}
for(i=0,n=0;i<8;i++)
for(j=0;j<4;j++)
tdat[n++]=sbox[i][j];
for(i=0,n=0;i<8;i++)

Page No.

for(j=0;j<4;j++)
pbdat[n++]=tdat[pbox[i][j]-1];
for(i=0;i<32;i++)
pbdat[i]=(pbdat[i]&!ipdat[i])|(!pbdat[i]&ipdat[i]);
for(i=0;i<32;i++)
ipdat[i]=ipdat[i+32];
for(i=0;i<32;i++)
ipdat[i+32]=pbdat[i];
}
void DESEncrypt(int *data,int *key)
{
int ip[8][8]= {{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 ipdat[64],i,j,n;
int k48bit[48];
int fp[8][8]= {{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}};
for(i=0,n=0;i<8;i++)
for(j=0;j<8;j++)
ipdat[n++]=data[ip[i][j]-1];
for(i=0;i<16;i++)
{
ithkey(key,k48bit,i+1);
printf("\nKey %d is ",i+1);
for(j=0;j<48;j++)
printf("%d",k48bit[j]);
DESROUND(ipdat,k48bit);
}

Page No.

for(i=0;i<32;i++)
ipdat[i]=ipdat[i]+ipdat[i+32]-(ipdat[i+32]=ipdat[i]);
for(i=0,n=0;i<8;i++)
for(j=0;j<8;j++)
data[n++]=ipdat[fp[i][j]-1];
}
void main()
{
int
key[64]={0,0,0,1,0,0,1,1,0,0,1,1,0,1,0,0,0,1,0,1,0,1,1,1,0,1,1,1,1,0,0,1,1,0,0,
1,1,0,1,1,1,0,1,1,1,1,0,0,1,1,0,1,1,1,1,1,1,1,1,1,0,0,0,1};
int
data[64]={0,0,0,0,0,0,0,1,0,0,1,0,0,0,1,1,0,1,0,0,0,1,0,1,0,1,1,0,0,1,1,1,1,0,0
,0,1,0,0,1,1,0,1,0,1,0,1,1,1,1,0,0,1,1,0,1,1,1,1,0,1,1,1,1};
int i;
clrscr();
printf("The Plain Text is\n");
for(i=0;i<64;i++)
printf("%d",data[i]);
printf("\nThe key used for Encryption is\n");
for(i=0;i<64;i++)
printf("%d",key[i]);
DESEncrypt(data,key);
printf("\nThe Cipher Text is\n");
for(i=0;i<64;i++)
printf("%d",data[i]);
getch();
}
OUTPUT:
The Plain Text is
0000000100100011010001010110011110001001101010111100110111101
111
The key used for Encryption is
0001001100110100010101110111100110011011101111001101111111110
001
Key 1 is 000110110000001011101111111111000111000001110010
Key 2 is 011110011010111011011001110110111100100111100101
Key 3 is 010101011111110010001010010000101100111110011001
Key 4 is 011100101010110111010110110110110011010100011101

Page No.

Key 5 is 011111001110110000000111111010110101001110101000
Key 6 is 011000111010010100111110010100000111101100101111
Key 7 is 111011001000010010110111111101100001100010111100
Key 8 is 111101111000101000111010110000010011101111111011
Key 9 is 111000001101101111101011111011011110011110000001
Key 10 is 101100011111001101000111101110100100011001001111
Key 11 is 001000010101111111010011110111101101001110000110
Key 12 is 011101010111000111110101100101000110011111101001
Key 13 is 100101111100010111010001111110101011101001000001
Key 14 is 010111110100001110110111111100101110011100111010
Key 15 is 101111111001000110001101001111010011111100001010
Key 16 is 110010110011110110001011000011100001011111110101
The Cipher Text is
1000010111101000000100110101010000001111000010101011010000000
101

AIM: Write a C program to implement data link layer framing methods


character stuffing and bit stuffing.
DESCRIPTION:
Character Stuffing:
In this technique message is added with a starting delimiter (stxdle)
and ending delimiter (dleetx).
In the text if there exists a dle pattern then another dle is to be
inseeted next to it.

Page No.

Example:
Let the message text be weldlecome
The message to be transmitted is stxdleweldledlecomedleetx
Bit Stuffing:
In this technique message is added with a flag 01111110 before and
after of it.
In the message itself if there are five consecutive 1s then 0 is added
next to them.
Example:
Let the binary text be 111101111100
The message to be transmitted is 01111110111101111100001111110

PROGRAM:
#include<stdio.h>
void charStuff(char *txt,char *sdat)
{
int ln=strlen(txt),i,j;
sdat[0]='\0';
strcat(sdat,"stxdle");
for(i=0,j=6;i<ln;i++,j++)

Page No.

{
sdat[j]=txt[i];
if(!strncmp(txt+i,"dle",3))
{
sdat[j+1]='\0';
strcat(sdat,"ledle");
j+=5;
i+=2;
}
}
sdat[j]='\0';
strcat(sdat,"dleetx");
}
void bitStuff(char *txt,char *sdat)
{
int ln=strlen(txt),i,j;
sdat[0]='\0';
strcat(sdat,"01111110");
for(i=0,j=8;i<ln;i++,j++)
{
sdat[j]=txt[i];
if(!strncmp(txt+i,"11111",5))
{
sdat[j+1]='\0';
strcat(sdat,"11110");
j+=5;
i+=4;
}
}
sdat[j]='\0';
strcat(sdat,"01111110");
}
void main()
{
int ch;
char txt[500],sdat[500];
clrscr();
printf("Enter 1 for Character stuffing, any other for Bit stuffing\n");
scanf("%d",&ch);
printf("Enter data to be transmitted\n");
fflush(stdin);
gets(txt);
if(ch==1)
charStuff(txt,sdat);

Page No.

else
bitStuff(txt,sdat);
printf("After Framing :\n");
printf("The data to be transmitted is\n");
puts(sdat);
getch();
}

OUTPUT:
1)
Enter 1 for Character stuffing, any other for Bit stuffing
1
Enter data to be transmitted
weldlecome
After Framing :
The data to be transmitted is
Stxdleweldledlecomedleetx
2)

Page No.

Enter 1 for Character stuffing, any other for Bit stuffing


0
Enter data to be transmitted
111101111100
After Framing :
The data to be transmitted is
01111110111101111100001111110

AIM: Write a C Program to find CRC for a given message using standardized
generating polynomials CRC-12, CRC-16, CRC-32, CRC-CCITT.
DESCRIPTION:
Here
M(x) : Message Polynomial.
G(x) : Generating Polynomial.
The algorithm for computing the checksum is as follows:
1. Let r be the degree of G(x). Append r zero bits to the low-order end of
the frame so it now contains m + r bits and corresponds to the
polynomial xrM(x).
2. Divide the bit string corresponding to G(x) into the bit string
corresponding to xrM(x), using modulo 2 division.

Page No.

3. Subtract the remainder (which is always r or fewer bits) from the bit
string corresponding to xrM(x) using modulo 2 subtraction. The result is
the checksummed frame to be transmitted. Call its polynomial T(x).
Example:

PROGRAM:

#include<stdio.h>
void genverChecksum(int *M,int m,int *G,int n,int *csum)
{
int i,j;
for(i=0;i<n;i++)
csum[i]=M[i];
for(i=0;i<m;i++)
{
if(csum[0]==1)
for(j=0;j<n;j++)
csum[j]=(csum[j]+G[j])%2;
for(j=0;j<n-1;j++)

Page No.

csum[j]=csum[j+1];
csum[n-1]=M[i+n];
}
}
void main()
{
int M[500],r[100],i,j,m,n,*csum,c,ch[]={13,17,33,17};
int G[4][33]={{1,1,0,0,0,0,0,0,0,1,1,1,1},
{1,1,0,0,0,0,0,0,0,0,0,0,0,0,1,0,1},
{1,0,0,0,0,0,1,0,0,1,0,0,0,0,0,0,1,0,0,0,1,1,1,0,1,1,0,1,1,0,1,1,1},
{1,0,0,0,1,0,0,0,0,0,0,1,0,0,0,0,1}};
clrscr();
printf("Enter data in 0's and 1's (-1 at end)\n");
for(m=0;1;m++)
{
scanf("%d",&M[m]);
if(M[m]==-1)
break;
}
printf("\t\t****MENU*****\n"
"\t\t1.CRC-12\n"
"\t\t2.CRC-16\n"
"\t\t3.CRC-32\n"
"\t\t4.CRC-CCIT\n"
"\t\tEnter your choice\n");
scanf("%d",&c);
if(c<1||c>4)
{
printf("Choose between 1 to 4 Only\n");
getch();
exit(0);
}
n=ch[c-1];
for(i=0;i<n-1;i++)
M[m+i]=0;
csum=(int *)malloc(sizeof(int)*n);
genverChecksum(M,m,G[c-1],n,csum);
printf("The Checksum is\n");
for(i=0;i<n-1;i++)
{
M[m+i]=csum[i];
printf("%d ",csum[i]);
}
printf("\nThe Transmitted data is\n");
for(i=0;i<m+n-1;i++)

Page No.

printf("%d ",M[i]);
printf("\nAt Receivers side : \n");
printf("The Received data is\n");
for(i=0;i<m+n-1;i++)
printf("%d ",M[i]);
genverChecksum(M,m,G[c-1],n,csum);
printf("\nObtained Remainder is\n");
for(i=0;i<n-1;i++)
printf("%d ",csum[i]);
getch();
}

OUTPUT:
Enter data in 0's and 1's (-1 at end)
1 0 1 0 -1
****MENU*****
1.CRC-12
2.CRC-16
3.CRC-32
4.CRC-CCIT
Enter your choice
2
The Checksum is
0000000000111100
The Transmitted data is
10100000000000111100
At Receivers side :
The Received data is
10100000000000111100
Obtained Remainder is

Page No.

0000000000000000

AIM: Write a C Program to Encrypt and Decrypt using RSA public key
encryption.
DESCRIPTION:
The steps involved in RSA Encryption are

Page No.

Example:

PROGRAM:
#include<stdio.h>
void main()
{
int p,q,n,fn,e,d;

Page No.

int M,C;
clrscr();
printf("Enter any two prime numbers\n");
scanf("%d%d",&p,&q);
n=p*q;
fn=(p-1)*(q-1);
for(e=2;e<fn && gcd(fn,e)!=1;e++);
for(d=2;d<fn && (d*e)%fn!=1;d++);
printf("The Public key[Ku] is {%d,%d}\n",e,n);
printf("The Private key[Kr] is {%d,%d}\n",d,n);
printf("Enter Plain Text less than %d\n",n);
scanf("%d",&M);
C=power(M,e,n);
printf("The Cipher Text is %d\n",C);
printf("For Cipher Text %d\nThe Plain Text is %d\n",C,power(C,d,n));
getch();
}
int gcd(int x,int y)
{
if(x<y)
return gcd(y,x);
if(y==0)
return x;
return gcd(y,x%y);
}
int power(int x,int p,int n)
{
int e[100];
long y=x;
int i,j;
for(i=0;p!=0;i++,p/=2)
e[i]=p%2;
for(j=i-2; j>=0 ;j--)
{
y = (y*y) % n;
if(e[j] == 1)
y = (y*x) % n;
}
return (int)y;
}

Page No.

OUTPUT:
Enter any two prime numbers
41 43
The Public key[Ku] is {11,1763}
The Private key[Kr] is {611,1763}
Enter Plain Text less than 1763
1670
The Cipher Text is 393
For Cipher Text 393
The Plain Text is 1670

Page No.

AIM: Write a C Program to implement Dijkstras Algorithm to find shortest


path from given source node to all nodes.
DESCRIPTION:
The Dijkstras Algorithm creates a shortest path tree from a graph.
The Algorithm divides the nodes into two sets : tentative and
permanent.
It finds the neighbours of current node, makes them tentative and
examines them if they pass the criteria makes them permanent.
The Algorithm can be defined using flow chart

Page No.

Example:

PROGRAM:
#include<stdio.h>
struct nodedata
{
int s;
int w;
int n;
};
void main()
{
int a[100][100],i,j,n,sn,mi,min,c[100];
struct nodedata nd[100];

Page No.

clrscr();
printf("Enter how many nodes the graph consists of\n");
scanf("%d",&n);
printf("Enter adjacency matrix values (-1 for no link)\n");
for(i=0;i<n;i++)
{
nd[i].s=-1;
nd[i].w=0;
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
}
printf("Enter source node in Upper Case\n");
fflush(stdin);
sn=getchar()-65;
mi=sn;
nd[sn].n=-1;
while(mi!=-1)
{
nd[mi].s=1;
for(i=0;i<n;i++)
if(a[mi][i]!=-1 && nd[i].s!=1)
{
if(nd[i].s==-1 || nd[mi].w+a[mi][i] < nd[i].w )
{
nd[i].w=nd[mi].w+a[mi][i];
nd[i].n=mi;
}
nd[i].s=0;
}
min=32765;
mi=-1;
for(i=0;i<n;i++)
if(nd[i].s==0 && min>nd[i].w)
{
min=nd[i].w;
mi=i;
}
}
printf("The Paths from node %c to all nodes are\n",sn+65);
for(i=0;i<n;i++)
{
if(sn==i)
continue;
mi=i;
printf("\nCost is %d Path is ",nd[mi].w);
for(j=0;mi!=-1;j++)

Page No.

{
c[j]=mi;
mi=nd[mi].n;
}
for(j--;j>=0;j--)
printf("%c ",c[j]+65);
}
getch();
}

OUTPUT:
Enter how many nodes the graph consists of
8
Enter adjacency matrix values (-1 for no link)
-1 2 -1 -1 -1 -1 6 -1
2 -1 7 -1 2 -1 -1 -1
-1 7 -1 3 -1 3 -1 -1
-1 -1 3 -1 -1 -1 -1 2
-1 2 -1 -1 -1 2 1 -1
-1 -1 3 -1 2 -1 -1 2
6 -1 -1 -1 1 -1 -1 4
-1 -1 -1 2 -1 2 4 -1
Enter source node in Upper Case
A
The Paths from node A to all nodes are
Cost is 2 Path is A B
Cost is 9 Path is A B C

Page No.

Cost
Cost
Cost
Cost
Cost

is
is
is
is
is

10 Path is A B E F H D
4 Path is A B E
6 Path is A B E F
5 Path is A B E G
8 Path is A B E F H

Note:
The Graph Considered is

AIM: Write a C Program to implement Distance vector routing Algorithm.


DESCRIPTION:
It is a dynamic routing Algorithm.
It operates by maintaining routing tables at each router and these
routing tables actually considered as vector tables which contains two
values one is the preferred outgoing line to use for destination and the
other one is estimated delay.
The metrics used in the algorithm use number of hops and delay time.
Consider the router knows the delay time to each of its neighbours.
Once every t milli seconds each router sends to each neighbor a list
of estimated delays to each destination.
It also receives a similar list from each neighbor.
Based on these lists it updates its own routing table.

Page No.

PROGRAM:
#include<stdio.h>
void main()
{
int vt[100][100],n,m,i,j,md,mdi,c,neigh[100];
clrscr();
printf("Enter no of nodes of subnet : ");
scanf("%d",&n);
printf("Enter node whose Routing Table to be updated : ");
fflush(stdin);
c=getchar();
printf("Enter no of neighbors : ");
scanf("%d",&m);
printf("Enter neighbors of node %c\n",c);
for(i=0;i<m;i++)
{
fflush(stdin);
neigh[i]=getchar();
}

Page No.

for(i=0;i<m;i++)
for(i=0;i<m;i++)
{
printf("Enter vector of neighbor %c : ",neigh[i]);
for(j=0;j<n;j++)
scanf("%d",&vt[i][j]);
printf("Enter its Delay : ");
scanf("%d",&vt[i][n]);
}
for(i=0;i<n;i++)
{
md=vt[0][i]+vt[0][n];
mdi=0;
for(j=1;j<m;j++)
if(md>vt[j][i]+vt[j][n])
{
md=vt[j][i]+vt[j][n];
mdi=j;
}
vt[m][i]=md;
vt[m+1][i]=neigh[mdi];
}
vt[m][c-65]=0;
vt[m+1][c-65]=c;
printf("The new routing table of node %c is\n",c);
printf("Delay Line\n");
for(i=0;i<n;i++)
{
printf("%5d",vt[m][i]);
printf(" %c\n",vt[m+1][i]);
}
getch();
}

Page No.

OUTPUT:
Enter no of nodes of subnet : 12
Enter node whose Routing Table to be updated : J
Enter no of neighbors : 4
Enter neighbors of node J
A
I
H
K
Enter vector of neighbor A : 0 12 25 40 14 23 18 17 21 9 24 29
Enter its Delay : 8
Enter vector of neighbor I : 24 36 18 27 7 20 31 20 0 11 22 33
Enter its Delay : 10
Enter vector of neighbor H : 20 31 19 8 30 19 6 0 14 7 22 9
Enter its Delay : 12
Enter vector of neighbor K : 21 28 36 24 22 40 31 19 22 10 0 9
Enter its Delay : 6
The new routing table of node J is
Delay Line
8A
20 A
28 I
20 H
17 I

Page No.

30
18
12
10
0
6
15

I
H
H
I
J
K
K

AIM: Write a C Program to implement any minimium cost spanning tree


Algorithm to obtain broadcast tree for a subnet.
DESCRIPTION:
Prims algorithm is used to obtain a minimum cost spanning tree
( Broadcast tree ) for a given subnet.
The input is a undirected weighted graph from that we have to obtain
the spanning tree with minimum cost.
At first we will select a edge with minimum cost and we will includes it
in spanning tree.
Next we will select another edge with minimum cost such that its
inclusion in spanning tree doesnt affect the property of a tree.
The above step is repeated until the spanning tree consists of n-1
edges where n is no. of nodes.
Now we will compute minimum cost using sum of the costs of all the
edges of the spanning tree.
Example:
The subnet of nodes is

Page No.

Its minimum cost spanning tree is

PROGRAM:
#include<stdio.h>
void main()
{
int a[100][100],i,j,n,k,l,v[100],mc,min=32765;
clrscr();
printf("Enter how many nodes in subnet\n");
scanf("%d",&n);
printf("Enter adjacency matrix values (-1 for no link)\n");
for(i=0;i<n;i++)
{
v[i]=0;
for(j=0;j<n;j++)
{
scanf("%d",&a[i][j]);
if(a[i][j]!=-1 && min>a[i][j])
{
min=a[i][j];
k=i;
l=j;
}
}
}
printf("The links of broadcast tree are\n");
printf("%c - %c with delay %d\n",k+65,l+65,min);
v[k]=v[l]=1;

Page No.

mc=min;
while(1)
{
min=32765;
for(i=0;i<n;i++)
if(v[i]==1)
for(j=0;j<n;j++)
if(a[i][j]!=-1 && v[j]==0 && min>a[i][j])
{
min=a[i][j];
k=i;
l=j;
}
if(min==32765)
break;
v[l]=1;
printf("%c - %c with delay %d\n",k+65,l+65,min);
mc+=min;
}
printf("The total delay of broadcast tree is %d\n",mc);
getch();
}

Page No.

OUTPUT:
Enter how many nodes in subnet
6
Enter adjacency matrix values (-1 for no link)
-1 2 3 -1 -1 -1
2 -1 6 5 3 -1
3 6 -1 -1 2 -1
-1 5 -1 -1 1 2
-1 3 2 1 -1 4
-1 -1 -1 2 4 -1
The links of broadcast tree are
D - E with delay 1
D - F with delay 2
E - C with delay 2
C - A with delay 3
A - B with delay 2
The total delay of broadcast tree is 10

Page No.

AIM: Write a C Program to implement Parity check code i.e. appending parity
bit to message.
DESCRIPTION:
This is familiar method generally used to detect single bit errors
In this method a additional parity bit is appended to message string to
make no. of 1s in message either even ( even parity ) or odd ( odd
parity ).
For even parity the parity bit is XOR of message bits and for odd parity
NXOR of message bits.
It can also detect multi bit errors in message when no. of bit errors is
odd.
Example:
For message string 100101
After appending parity bit
For even parity 1001011
For odd parity 1001010

Page No.

PROGRAM:
#include<stdio.h>
void main()
{
int d[500],n,c,i;
clrscr();
printf("Enter data in 0's and 1's (-1 at end)\n");
for(n=0;1;n++)
{
scanf("%d",&d[n]);
if(d[n]==-1)
break;
}
d[n]=0;
printf("Type 1 for ODD Parity\n");
scanf("%d",&c);
for(i=0;i<n;i++)
d[n]=(d[n]+d[i])%2;
if(c==1)
d[n]=!d[n];
printf("The Message to be Transmitted is\n");
for(i=0;i<=n;i++)
printf("%d ",d[i]);
getch();
}

Page No.

OUTPUT:
1)
Enter data in 0's and 1's (-1 at end)
1 0 0 1 0 1 -1
Type 1 for ODD Parity
1
The Message to be Transmitted is
1001010
2)
Enter data in 0's and 1's (-1 at end)
1 0 0 1 0 1 -1
Type 1 for ODD Parity
0
The Message to be Transmitted is
1001011

Page No.

AIM: Write a C Program to implement Hamming code to add redundant bits


in message to correct single bit errors.
DESCRIPTION:
Hamming code is used to generate the redundancy bits which are
combined with message bits to form a code word.
The positions of the redundant bits in a code word are powers of 2.
The no. of redundant bits r for a message length n is determined using
expression
2r>=n+r+1 ( minimum r value )
Steps for finding redundant bits:
Consider 7-bit message where n=7, r=4
R

Redundant bits whose positions are powers of 2


Redundant bit values are obtained by
R1=XOR(1,3,5,7,9,11)
R2=XOR(2,3,6,7,11)
R3=XOR(4,5,6,7)
R4=XOR(8,9,10,11)

Page No.

PROGRAM:
#include<stdio.h>
#include<math.h>
void main()
{
int r=0,n,m[100],i,j,k,rb[100][100];
clrscr();
printf("Enter message in 0's and 1's (-1 at end)\n");
for(n=0;1;n++)
{
scanf("%d",&m[n]);
if(m[n]==-1) break;
}
while(pow(2,r)<n+r+1)
r++;
for(i=0;i<n+r+1;i++)
{
k=i;
for(j=0;k!=0;j++,k/=2)
rb[i][j]=k%2;
for(;j<r;j++)
rb[i][j]=0;
}
printf("The redundancy bits are kept at positions\n");
for(i=0,k=1;i<r;i++,k*=2)
{
printf("%d ",k);
for(j=n+i;j>=k;j--)
m[j]=m[j-1];
m[k-1]=0;
}
for(i=0,k=1;i<r;i++,k*=2)
{
for(j=1;j<n+r+1;j++)
if(rb[j][i]==1)
m[k-1]=(m[k-1]+m[j-1])%2;
}

Page No.

printf("\nThe message to be transmitted is\n");


for(i=0;i<n+r;i++)
printf("%d ",m[i]);
getch();
}
OUTPUT:
Enter message in 0's and 1's (-1 at end)
1 0 0 1 1 1 1 0 -1
The redundancy bits are kept at positions
1248
The message to be transmitted is
001100111110

Page No.

AIM: Write a C Program to determine no. of packets received by each node


when a packet is generated at source node using selective flooding.
DESCRIPTION:
A static routing technique is flooding.
In this every incoming packet is sent out on every outgoing line except
the one it arrived on.
Flooding obviously generates vast numbers of duplicate packets.
In fact, an infinite number unless some measures are taken to damp
the process.
So for this a variation of flooding that is slightly more practical is
selective flooding.
In this algorithm the routers do not send every incoming packet out on
every line, only on those lines that are going approximately in the right
direction.
i.e. in sending a westbound packet on an eastbound line unless the
topology is extremely peculiar.
Example:

Page No.

PROGRAM:
#include<stdio.h>
void main()
{
int a[100][100],i,j,n,sn,ci,nop[100],q[100],f=-1,r=0;
clrscr();
printf("Enter how many nodes the graph consists of\n");
scanf("%d",&n);
printf("Enter adjacency matrix values (0 for no link, 1 for link)\n");
for(i=0;i<n;i++)
{
nop[i]=0;
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
}
printf("Enter source node in Upper Case\n");
fflush(stdin);
sn=getchar()-65;
q[r++]=sn;
nop[sn]=1;
while(f+1<r)
{
ci=q[++f];
for(i=0;i<n;i++)
if(a[ci][i]!=0)
{
if(nop[i]==0)
q[r++]=i;
nop[i]+=nop[ci];
a[ci][i]=a[i][ci]=0;
}
}
printf("After selective flooding a packet at node %c the packets at each node
are\n",sn+65);
for(i=0;i<n;i++)
printf("%c %d\n",i+65,nop[i]);
getch();
}

Page No.

OUTPUT:
Enter how many nodes the graph consists of
8
Enter adjacency matrix values (0 for no link, 1 for link)
01000010
10101000
01010100
00100001
01000110
00101001
10001001
00010110
Enter source node in Upper Case
A
After selective flooding a packet at node A the packets at each node are
A1
B1
C1
D2
E2
F4
G1
H1

Page No.

Step by step DVR Algorithm


The number of nodes is obtained and stored in a variable
The distance between all nodes are accepted in adjacency matrix
Each nod is estimated and displayed using estimate table
According to the input node the shortest distance between the neighbors route is calculated
For given node the shortest estimated between each node is displayed
Finally the stop Distance Vector routing Protocol algorithm
Source code in C language programming DVR CS1305- NETWORK LAB
#include<stdio.h>
#include<ctype.h>
int graph[12][12];
int e[12][12];
int ad[12];
int no,id,adc,small,chosen;
char nodes[12]={a b c d e f g h i j k l};
int main()
{
int i,j,k,ch1,ch2=0;
adc=0;
printf("enter the no nodes");
scanf("%d",&no);
printf("enter the value for adjacency matrix");
for(i=0;i<no;i++)
{
for(j=0;j<no;j++)
{
printf(" enter values for %d,%d position:",(i+1),(j+1));
scanf("%d",&graph[i][j]);
}
}
printf("enter initial estimates");
for(i=0;i<no;i++)
{
printf("estimate for node %c \n",nodes[i]);
for(j=0;j<no;j++)
{
printf("to node%c",nodes[j]);
scanf("%d",&e[i][j]);
}
}
do
{
printf("\n menu:\n 1.routing info for node");
printf("2.estimated table\n ");
scanf("%d",&ch1);
switch(ch1)
{
case 1:
printf("\n which node should routing table be built(1-a)(2-b)...");
scanf("%d",&id);
id--;
adc=0;
printf("neighbours for particular node");
for(i=0;i<no;i++)
{

Page No.

if(graph[id][i]==1)
{
ad[adc]=i;
adc++;
printf("%c",nodes[i]);
}
}
for(i=0;i<no;i++)
{
if(id!=i)
{
small=100;
chosen=-1;
for(j=0;j<adc;j++)
{
int total=e[ad[j]][i]+e[id][ad[j]];
if(total<small)
{
small=total;
chosen=j;
}}
e[id][i]=small;
printf("\n shortest estimate to %c is %d",nodes[i],small);
printf("\n next hop is %c",nodes[ad[chosen]]);
} else
e[id][i]=0;
}
break;
case 2:
printf("\n");
for(i=0;i<no;i++)
{ for(j=0;j<no;j++)
printf("%d",e[i][j]);
printf("\n");
} break;
}
printf("\n do u want to continue (1-yes,2-no)");
scanf("%d",&ch2);
}
while(ch2==1);
return 0; }

Page No.

Output for Distance Vector Routing Protocol CS1305- NETWORK LAB


[it28@localhost studentwebsite]$ cc distancevector.c
[it28@localhost studentwebsite]$ ./a.out
Enter the no of nodes 4
Enter the values for adjacency matrix:
Enter the value for 1,1 position:0
Enter the value for 1,2 position:1
Enter the value for 1,3 position:1
Enter the value for 1,4 position:0
Enter the value for 2,1 position:1
Enter the value for 2,2 position:0
Enter the value for 2,3 position:0
Enter the value for 2,4 position:1
Enter the value for 3,1 position:1
Enter the value for 3,2 position:0
Enter the value for 3,3 position:0
Enter the value for 3,4 position:1
Enter the value for 4,1 position:0
Enter the value for 4,2 position:1
Enter the value for 4,3 position:1
Enter the value for 4,4 position:0
Enter the initial estimates for node a
To node a 0
To node b 32
To node c 25
To node d 66
Estimate for node b
To node a 10
To node b 0
To node c 23
To node d 56
Enter the initial estimates for node c
To node a 12
To node b 23
To node c 0
To node d 41
Enter the initial estimates for node d
To node a 13
To node b 53
To node c 62
To node d 0
Menu
1.Routing Information for a node
2.Estimate table
Enter the choice(1,2):1
Which node should routing table be built(1-a)(2-b)1
The neighbors for the particular node b c
Shortest estimate to b is 32
Next hop is b
Shortest estimate to c is 25
Shortest estimate to d is 66
Nest hop is c
Do you want to continue (1-yes,2-no)
Menu
1. Routing Information for a node
2. Estimate table

Page No.

Enter the choice(1,2):2


0 32 25 66
10 0 23 56
12 23 0 41
13 53 62 0

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy