AIM: Write A C Program To Implement Conventional Encryption Technique
AIM: Write A C Program To Implement Conventional Encryption Technique
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.
Page No.
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
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.
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.
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
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
Page No.
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.
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.
Page No.
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.
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.
Page No.