BIT STUFFING FLOW CHART
/****************** BIT STUFFING PROGRAM *****************/
#include #include
main() { char a[20],fs[50]="",t[6],r[5]; int i,j,p=0,q=0; clrscr(); printf("enter bit string : "); scanf("%s",a); strcat(fs,"01111110");
if(strlen(a)<5) { strcat(fs,a); } else { for(i=0;i { for(j=i;j { t[p++]=a[j]; } t[p]='\0'; if(strcmp(t,"11111")==0) { strcat(fs,"111110"); i=j-1; } else { r[0]=a[i]; r[1]='\0'; strcat(fs,r); } p=0; } for(q=i;q { t[p++]=a[q]; } t[p]='\0'; strcat(fs,t); } strcat(fs,"01111110"); printf("After stuffing : %s",fs);
getch(); }
BIT STUFFING OUTPUT
Enter bit string : 10101111110
After stuffing : 0111111010101111101001111110
Enter bit string : 1011111011110111110
After stuffing : 0111111010111110011110111110001111110
CHARACTER STUFFING FLOW CHART
/************** CHARACTER STUFFING PROGRAM*************/
#include #include
main() { char a[30],fs[50]="",t[3],sd,ed,x[3],s[3],d[3],y[3]; int i,j,p=0,q=0; clrscr(); printf("Enter characters to be stuffed : "); scanf("%s",a); printf("\nEnter a character that represents starting delimiter : "); scanf(" %c",&sd); printf("\nEnter a character that represents ending delimiter : "); scanf(" %c",&ed); x[0]=s[0]=s[1]=sd; x[1]=s[2]='\0'; y[0]=d[0]=d[1]=ed; d[2]=y[1]='\0'; strcat(fs,x);
for(i=0;i { t[0]=a[i]; t[1]='\0'; if(t[0]==sd) strcat(fs,s); else if(t[0]==ed) strcat(fs,d); else strcat(fs,t); } strcat(fs,y); printf("\nAfter stuffing : %s",fs);
getch(); }
CHARACTER STUFFING OUTPUT
Enter characters to be stuffed : goodday
Enter a character that represents starting delimiter : d
Enter a character that represents ending delimiter : g
After stuffing : dggooddddayg
CRC-12 FLOW CHART
/********************* CRC - 12 PROGRAM******************/
#include const char * bindiv(const char *,const char *); const char * binsub(const char *,const char *); int f=0,ll=0;
main() { char *a,p[13]="1100000001011",g[30],g1[30],yy[30]="",td[30],*aa; int l=0,i; clrscr(); printf("enter transfered data : "); scanf("%s",g); printf("enter received data : "); scanf("%s",td); strcpy(g1,g); strcat(g,"000000000000"); printf("\n%s ) %s (",p,g); a=bindiv(g,p);
if(strlen(a)<12) { for(i=strlen(a);i<12;i++) { yy[l++]='0'; } yy[l]='\0'; }
strcat(yy,a); strcat(g1,yy); printf("\ncrc is %s",yy); printf("\n ------------------"); strcat(td,yy); printf("\n\n%s ) %s (",p,td); ll=0; aa=bindiv(td,p); strcpy(a,aa); printf("\n %s",a); printf("\n -----------------");
if(f==1) printf("\ndata transfered correctly"); else printf("\ndata transfered incorrectly"); getch();
}
const char * bindiv(const char *s,const char *d) { int i,j,k=0,x=13,h,p=0,l; char q[15]="",b[30],*w; for(i=0;i { if((i+x)>strlen(s)) x=(i+x)-strlen(s)+1; for(j=i;j<(i+x);j++) { b[k++]=s[j]; } b[k]='\0'; if(ll!=0) printf("\n %s",b); ll=1; if(strlen(b)==12) { break; } printf("\n %s",d); printf("\n -----------------"); w=binsub(b,d);
k=0;i=j-1; for(l=0;l { if(w[l]=='1') break; } if(l==strlen(w)) { f=1; return(w); }
for(h=l;h { q[p++]=w[h]; } q[p]='\0'; x=13-strlen(q); strcpy(b,""); strcat(b,q); k=strlen(q); p=0; } return(b); }
const char * binsub(const char *x,const char *y) { int i,j=0;
char w[15]="",e[3],f[3],n[3]; e[0]='1'; e[1]='\0'; f[0]='0'; f[1]='\0';
for(i=0;i { if((x[i]=='1')&&(y[i]=='1')) strcat(w,f); else if((x[i]=='0')&&(y[i]=='0')) strcat(w,f); else strcat(w,e); }
n[0]='\0'; n[1]='\0'; strcat(w,n); return(w); }
CRC-16 FLOW CHART
/*********************** CRC – 16 PROGRAM ******************/
#include const char * bindiv(const char *,const char *); const char * binsub(const char *,const char *); int f=0,ll=0;
main() { char *a,p[20]="10001000000100001", g[30],g1[30],yy[30]="",td[30],*aa; int l=0,i; clrscr(); printf("enter transfered data : "); scanf("%s",g); printf("enter received data : "); scanf("%s",td); strcpy(g1,g); strcat(g,"0000000000000000"); printf("\n%s ) %s (",p,g); a=bindiv(g,p); if(strlen(a)<16) { for(i=strlen(a);i<16;i++) { yy[l++]='0'; } yy[l]='\0'; }
strcat(yy,a); strcat(g1,yy); printf("\n ------------------"); printf("\ncrc is %s",yy); strcat(td,yy); printf("\n\n%s ) %s (",p,td); ll=0; aa=bindiv(td,p); strcpy(a,aa); printf("\n %s",a); printf("\n -----------------"); if(f==1) printf("\ndata transfered correctly"); else printf("\ndata transfered incorrectly"); getch();
}
const char * bindiv(const char *s,const char *d) { int i,j,k=0,x=17,h,p=0,l; char q[25]="",b[30],*w; for(i=0;i { if((i+x)>strlen(s)) x=(i+x)-strlen(s)+1; for(j=i;j<(i+x);j++) { b[k++]=s[j]; }
b[k]='\0'; if(ll!=0) printf("\n %s",b); ll=1; if(strlen(b)==16) { break; } printf("\n %s",d); printf("\n -----------------"); w=binsub(b,d);
k=0;i=j-1; for(l=0;l { if(w[l]=='1') break; } if(l==strlen(w)) { f=1; return(w); } for(h=l;h { q[p++]=w[h]; } q[p]='\0'; x=17-strlen(q); strcpy(b,""); strcat(b,q); k=strlen(q); p=0; } return(b); }
const char * binsub(const char *x,const char *y) { int i,j=0; char w[25]="",e[3],f[3],n[3]; e[0]='1'; e[1]='\0'; f[0]='0'; f[1]='\0'; for(i=0;i { if((x[i]=='1')&&(y[i]=='1')) strcat(w,f); else if((x[i]=='0')&&(y[i]=='0')) strcat(w,f); else strcat(w,e); } n[0]='\0'; n[1]='\0'; strcat(w,n); return(w); }
CRC-16 OUTPUT
Enter transfered data : 11011 Enter received data : 11011
10001000000100001 ) 110110000000000000000 ( 10001000000100001 ------------------------- 10100000001000010 10001000000100001 -------------------------- 10100000110001100 10001000000100001 -------------------------- 1010001101011010 -------------------------- crc is 1010001101011010
10001000000100001 ) 110111010001101011010 ( 10001000000100001 -------------------------- 10101010000101001 10001000000100001 -------------------------- 10001000000100001 10001000000100001 -------------------------- 00000000000000000 -------------------------- Data transferred correctly
Shortest Path Routing (Dijkstra’s Algorithm) Flow Chart
1.start 2.read data from the user: 3.nodes=no. of nodes; 4.dsp[nodes][nodes]=adjacency matrix of the graph;src=source;dest=destination 5.two struct arrays permanent, temp are used to 6.store the permanent and tentavie nodes. 7.A function sort is used to sort the temp 8.array in decreasing order.
permanent[0].src=src; permanent[0].dest=src; permanent[0].length=0;
9.store all the neighbouring nodes to src in temp
SORT while temp array is not null for the node recently added to permanent list, find the neighbouring nodes. if a node is not already present in the temp add the node to the temp array if the length of the node is smaller than the previous value then update the temp to the new value, else ignore the new value sort and find the destination node in the permanent array. trace the path to the source and store it an array display the shortest path from src to dest and the total delay to reach the destination. End sort
Bubble Sort Technique: sort temp each item is compared to all the other items. if the item is lesser than swap place the last item i.e., the node with less delay in the permanent array. Return
/**********SHORTEST PATH(Dijkstra’s Algorithm)*************/
#include
void sort(void);
static int dsp[10][10],nodes; struct{ char src; char dest; int length; }stemp,permanent[10]={' ',' ',0},temp[10]={' ',' ',-1}; static int perm,tem;
void main() { int i,j,k,l,m,n=0,point; char initial,dest,path[10]={' '}; clrscr(); printf("\t\t Shortest Path (Dijkstra's algorithm)"); printf("\n*******************************************************"); printf(“\nEnter the number of nodes:”); scanf(“%d”,&nodes); printf(“\nEnter the adjacency matrix for the graph:\n”); for(i=0;i { for(j=0;j scanf(“%d”,&dsp[I][j]); } fflush(stdin); printf("\n enter the source node:"); scanf("%c",&initial);fflush(stdin); printf("\n Enter the destination node:"); scanf("%c",&dest); permanent[perm].src=initial; permanent[perm].dest=initial; permanent[perm++].length=0; i=permanent[perm-1].dest-97; for(j=0;j { if(i!=j) { if(dsp[i][j]>0) { temp[tem].src=permanent[perm-1].src; temp[tem].dest=j+97; temp[tem++].length=dsp[i][j]; } } } sort(); while(tem>=0) { j=permanent[perm-1].dest-97; for(i=0;i { if(i!=initial-97) { if(dsp[j][i]>0) { l=-1; for(k=0;k { if(permanent[k].dest==(i+97)) l=k; } for(k=0;k<=tem;k++) { if(temp[k].dest==(i+97)) l=k; } if(l<0) { temp[tem].src=j+97; temp[tem].dest=i+97; for(m=0;m { if(permanent[m].dest==temp[tem].src) n=permanent[m].length; } temp[tem++].length=dsp[j][i]+n; }
else { for(m=0;m { if(permanent[m].dest==j+97) { n=permanent[m].length+dsp[j][i];break; } else n=dsp[j][i]; } if((n { temp[l].length=n; temp[l].src=j+97; temp[l].dest=i+97; } } } } } sort(); } printf("\nShortest path:\n"); printf("From %c to %c is:",initial,dest);
for(i=0;i { if(permanent[i].dest==dest) { point=i;n=i; break; } } i=0; for(j=perm;j>0;j--) { if(permanent[j-1].dest==permanent[point].src) { path[i++]=permanent[point].dest; point=j-1; } } path[i]=initial; for(j=i;j>=0;j--) printf("%c ",path[j]); printf("\t length=%d",permanent[n].length); getch(); }
void sort() { int i,j,k; for(i=0;i<=tem;i++) { k=1; for(j=0;j<=tem;j++) { if((temp[j].length <= temp[j+1].length)) { stemp=temp[j]; temp[j]=temp[j+1]; temp[j+1]=stemp; k=0; } } if(k) break; } permanent[perm++]=temp[tem-1]; temp[tem-1].src=' ';temp[tem-1].dest=' '; temp[tem-1].length=-1; tem--; }
Network topology: ------------------------ 1 1 b c 1 a 1 e 3 f 2 2 d
Output of execution1: ----------------------------
Shortest Path (Dijkstra's algorithm) ******************************************************************* Enter the number of nodes:6 Enter the adjacency matrix for the graph(-1 for no edge): 0 1 -1 2 -1 -1 1 0 1 -1 -1 -1 -1 1 0 1 1 -1 2 -1 1 0 2 -1 -1 -1 1 2 0 3 -1 -1 -1 -1 3 0
Enter the source node:a
Enter the destination node:f
Shortest path: From a to f is:a b c e f length=6
Network Topology: B 7 C 2 2 3 3 A E 2 F D 6 1 2 2 G 4 H
Output of execution2: ---------------------------- Shortest Path (Dijkstra's algorithm) ******************************************************************* Enter the number of nodes:8 Enter the adjacency matrix for the graph(-1 for no edge): Enter the adjacency matrix for the graph 0 2 -1 -1 -1 -1 6 -1 2 0 7 -1 2 -1 1 -1 -1 7 0 3 -1 3 -1 -1 -1 -1 3 0 -1 -1 -1 2 -1 2 -1 -1 0 2 1 -1 -1 -1 3 -1 2 0 -1 2 6 -1 -1 -1 1 -1 0 4 -1 -1 -1 2 -1 2 4 0 Enter the source node:a
Enter the destination node:d
Shortest path: From a to d is:a b e f h d length=10
Distance Vector Routing Flow Chart
START
read data from user: nodes=no. of nodes;src=node for which distance vector is to be computed;neighb=no. of neighbours to src; neigh[]={src,neighbouring node names}; topology[nodes][nodes]=distance vectors from all neighbours are read from user and stored. for nodes that are not neighbours infinity is stored as the value. dv[] stores the distance vector of src
for(I=0;I
if(I==neigh[n])
yes
add1=topology[src][I]; add add1 to the distance vector of I
compare the values in distace vector of the source. to that of I .if the value in distance vector of I less than the original then store the new value and the line to use as I .
display the distance vector of src
ISTANCE VECTOR ROUTING /*************** DISTANCE VECTOR ROUTING **************/
#include #define e 10000 #define nodes1 40
int topology[nodes1][nodes1]; static int l,n,neighb,nodes; static char neigh[nodes1];
struct { char name; int delay; } dv[nodes1];
void main() { int i,j,k[nodes1*nodes1],add=0,src1; char src='a'; clrscr(); printf("\t\t\tDistance Vector Routing\n"); printf("***********************************************************"); printf("\nEnter the number of nodes:"); scanf("%d",&nodes);fflush(stdin); printf("\nEnter the node for which Distance Vector table is needed:"); scanf("%c",&src);fflush(stdin); printf("\nEnter the no. of neighbours to %c:",src); scanf("%d",&neighb); fflush(stdin); printf("\nenter the names of the neighbours(in alphabetic order):"); neigh[0]=src; for(i=1;i { scanf("%c",&neigh[i]); fflush(stdin); } printf("\n enter the distance vectors of the source and the neighbouring nodes\n"); printf("starting with source,then with neighbouring nodes in alphabetical order:\n");
for(i=0;i { scanf("%d",&k[i]); } src1=src; l=nodes;n=1; for(i=0;i { if(i==src1-97) { for(j=0;j { if(k[j]<0) topology[i][j]=e; else topology[i][j]=k[j]; } } else if(i==neigh[n]-97) { for(j=0;j { if(k[l]<0) topology[i][j]=e; else topology[i][j]=k[l]; l++; } n++; } else { for(j=0;j topology[i][j]=e; } } i=src1-97; for(j=0;j { dv[j].name=src1; dv[j].delay=topology[i][j]; } k[nodes*nodes]=e; n=1;
for(i=0;i { if((i==neigh[n]-97)) { add=topology[src1-97][i]; for(j=0;j { k[j]=add+topology[i][j]; }
for(j=0;j { if(k[j] { dv[j].name=i+97; dv[j].delay=k[j]; } } if(i!=src1-97) n++;
printf("\n%c's distance vector after receiving %c's vector:\n",src1,i+97); for(j=0;j printf("To %c:\tfrom: %c \t %d\n",j+97,dv[j].name,dv[j].delay); getch();
} } }
Network Graph for Output1: B A D C
Output from first execution: ------------------------------------
Distance Vector Routing ********************************************************************** Enter the number of nodes:4
Enter the node for which Distance Vector table is needed:d
Enter the no. of neighbours to d:2
enter the names of the neighbours(in alphabetic order):b c
enter the distance vectors of the source and the neighbouring nodes starting with source,then with neighbouring nodes in alphabetical order: -1 1 2 0 1 0 1 1 1 1 0 2
d's distance vector after receiving b's vector: To a: from: b 2 To b: from: d 1 To c: from: d 2 To d: from: d 0
d's distance vector after receiving c's vector: To a: from: b 2 To b: from: d 1 To c: from: d 2 To d: from: d 0
Network Graph for Output2:
A B C D
E F G H
I J K L
Output from second execution: ----------------------------------------
Distance Vector Routing ********************************************************************** Enter the number of nodes:12
Enter the node for which Distance Vector table is needed:j
Enter the no. of neighbours to j:4
enter the names of the neighbours(in alphabetic order):a h i k
enter the distance vectors of the source and the neighbouring nodes starting with source,then with neighbouring nodes in alphabetical order: 8 -1 -1 -1 -1 -1 -1 12 10 0 6 -1 0 12 25 40 14 23 18 17 21 9 24 29 20 31 19 8 30 19 6 0 14 7 22 9 24 36 18 27 7 20 31 20 0 11 22 33 21 28 36 24 22 40 31 19 22 10 0 9
j's distance vector after receiving a's vector: To a: from: j 8 To b: from: a 20 To c: from: a 33 To d: from: a 48 To e: from: a 22 To f: from: a 31 To g: from: a 26 To h: from: j 12 To i: from: j 10 To j: from: j 0 To k: from: j 6 To l: from: a 37
j's distance vector after receiving h's vector: To a: from: j 8 To b: from: a 20 To c: from: h 31 To d: from: h 20 To e: from: a 22 To f: from: a 31 To g: from: h 18 To h: from: j 12 To i: from: j 10 To j: from: j 0 To k: from: j 6 To l: from: h 21
j's distance vector after receiving i's vector: To a: from: j 8 To b: from: a 20 To c: from: i 28 To d: from: h 20 To e: from: i 17 To f: from: i 30 To g: from: h 18 To h: from: j 12 To i: from: j 10 To j: from: j 0 To k: from: j 6 To l: from: h 21
j's distance vector after receiving k's vector: To a: from: j 8 To b: from: a 20 To c: from: i 28 To d: from: h 20 To e: from: i 17 To f: from: i 30 To g: from: h 18 To h: from: j 12 To i: from: j 10 To j: from: j 0 To k: from: j 6 To l: from: k 15
BROAD CASTING FLOW CHART
/************** BROAD CASTING PROGRAM ************/
#include int max(); int distance[20]; int n;
main() { int adj[20][20],adj1[20][20],flag[30]; int i,j,root,x; int source,count=1,y=0; printf("enter no of nodes"); scanf("%d",&n);
for(i=0;i { for(j=0;j { scanf("%d",&adj[i][j]); } }
printf("enter the source for broadcasting"); scanf("%d",&source); for(i=0;i { flag[i]=0; } for(root=0;root { for(i=0;i { distance[i]=adj[root][i]; } x=min(); for(i=0;i { if(distance[i]==x) { adj1[root][i]=x; adj1[i][root]=x; } else { adj1[root][i]=0; } } }
for(i=0;i { for(j=0;j { if(adj1[i][j]!=0) { adj1[j][i]=adj[i][j]; } } }
printf("given adjacency matrix is"); for(i=0;i { for(j=0;j { printf("%d",adj[i][j]); } printf("\n"); } printf("minimal spanning tree");
for(i=0;i { for(j=0;j { printf("%d ",adj1[i][j]); } printf("\n"); } root=source; flag[root]=1;
while(count!=y) { for(i=0;i { if(adj1[root][i]!=0 && flag[root]==1 && flag[i]!=1) { printf("%d sends message to %d \n",root,i); flag[i]=1; } } if(root { root++; } else { root=0; } for(i=0;i { if(flag[i]==0) { break; } } if(i==n) { count=y; } } }
int min() { int i,j=0; int mini; int distance1[10]; for(i=0;i { if(distance[i]!=0) { distance1[j]=distance[i]; j++; } } mini=distance1[0]; for(i=1;i { if(distance1[i] { mini=distance1[i]; } } return(mini); }
BROAD CASTING OUTPUT
enter no of nodes2 enter the adjacency matrix 0 2 2 0 enter the source for broadcasting1 given adjacency matrix is 0 2 2 0
minimal spanning tree is 0 2 2 0 1 sends message to 0
enter no of nodes3 enter the adjacency matrix 0 1 2 1 0 5 2 5 0 enter the source for broadcasting2 given adjacency matrix is 0 1 2 1 0 5 2 5 0
1
2 5
minimal spanning tree is 0 1 2 1 0 0 2 0 0 1 2
2 sends message to 0 0 sends message to 1
enter no of nodes4 enter the adjacency matrix 0 6 8 7 6 0 5 0 8 5 0 4 7 0 4 0 enter the source for broadcasting2 given adjacency matrix is 0 6 8 7 6 0 5 0 8 5 0 4 7 0 4 0
4
7 8 5
6
minimal spanning tree is 0 6 0 0 6 0 5 0 0 5 0 4 0 0 4 0
4
5
6
2 sends message to 1 2 sends message to 3 1 sends message to 0 enter no of nodes5 enter the adjacency matrix 0 2 0 5 0 2 0 3 6 0 0 3 0 7 9 5 6 7 0 8 0 0 9 8 0 enter the source for broadcasting2 given adjacency matrix is 0 2 0 5 0 2 0 3 6 0 0 3 0 7 9 5 6 7 0 8 0 0 9 8 0
3
2 6 7 9
5 8
minimal spanning tree is 0 2 0 5 0 2 0 3 0 0 0 3 0 0 0 5 0 0 0 8 0 0 0 8 0
3 2
5 8
2 sends message to 1 1 sends message to 0 0 sends message to 3 3 sends message to 4
enter the no of nodes6 enter the adjacency matrix 0 2 0 7 6 0 2 0 3 0 0 0 0 3 0 4 0 0 7 0 4 0 5 0 6 0 0 5 0 7 0 0 0 0 7 0 enter the source for broadcasting3 given adjacency matrix is 0 2 0 7 6 0 2 0 3 0 0 0 0 3 0 4 0 0 7 0 4 0 5 0 6 0 0 5 0 7 0 0 0 0 7 0
6 7
2 8 5
3 4
minimal spanning tree is 0 2 0 0 0 0 2 0 3 0 0 0 0 3 0 4 0 0 0 0 4 0 5 0 0 0 0 5 0 7 0 0 0 0 7 0 3 sends message to 2 3 sends message to 4 4 sends message to 5 2 sends message to 1 1 sends message to 0 32 bits 32 bits 32 bits 32 bits K (i) 48 bits True
***** DATA ENCRYPTION STANDARD PROGRAM *****
#include #include
void keygen(); int len,x,y = 0,l[40],r[40],r1[48],str1[20], r2[48],i3, tmp3 [64],tmp4[64]; int ip [] = {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 e [] = {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 c1[100]; int s[64]; int round=1;
int k [] = {0,1,0,1,0,1,0,0,1,0,1,0,1,0,1,1,0,0,0,0,0,1, 1,1,1,1,1,0,0,0,0,0,0, 0,1,1,1,1,1,0,1,1,1,1,1,1,1,0,0, 1,1,1,1,1,1,1,1,0,0,0,0,0,1,1};
int pc1 [] = {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 pc2[]={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 tmp5[8],flag=0,t,t1,i,j=0,c2[100],c[100],d[100], key[100],k2[20][100],count=1;
int s1[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}}; int s2[4][16]={{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 s3[4][16]={{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 s4[4][16]={{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 s5[4][16]={{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 s6[4][16]={{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 s7[4][16]={{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 s8[4][16]={{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 p[32]={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 g3,w[8],i4,d1=0,g,g1=0,g2,size,ch1[8],a1[4],b1[8][4], b[8][6],i1,j1,k1=0,tz[4],tmp,tmp1,t11,temp,z=0,m,count1=0,c11[32],res[32]; int iip[]={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};
char ch[20]; int str[64];
void main() { clrscr(); printf("enter the string\n"); gets(ch); size=strlen(ch);
for(g=0;g { ch1[g]=ch[g]; }
for(g=0;g { g1=0; while(ch1[g]>0) { w[g1++]=ch1[g]%2; ch1[g]=ch1[g]/2; } for(g3=g1;g3<8;g3++) w[g3]=0; for(g2=7;g2>=0;g2--) { str[d1++]=w[g2]; } }
for(g3=0;g3<64;g3++) s[g3]=str[g3]; for(g2=0;g2<64;g2++) printf("%d",str[g2]); keygen(); /*16 key generation*/ /*initial permutation*/ for(x=0;x<64;x++) { c1[x]=str[ip[x]-1]; }
for(x=0;x<64;x++) { printf("%d",c1[x]); if(((x%7)==0)&&(x!=0)) printf("\n"); }
/*dividing the whole string into l & r*/
for(x=0;x<32;x++) l[x]=c1[x]; y=0; for(x=32;x<64;x++) r[y++]=c1[x];
/*round starts here*/ while(round<=16) {
/*expansion permutation e table*/ for(x=0;x<48;x++) { r1[x]=r[e[x]-1]; } printf("r1: "); for(x=0;x<48;x++) printf("%d",r1[x]); printf("\n");
/*xor r1 with key*/ for(x=0;x<48;x++) { if(r1[x]==k2[round][x]) r2[x]=0; else r2[x]=1; } printf("\nkey"); for(x=0;x<48;x++) printf("%d",k2[round][x]); printf("\n"); for(x=0;x<48;x++) printf("%d",r2[x]);
/*substitution s-box*/ k1=0; for(i1=0;i1<8;i1++) { for(j1=0;j1<6;j1++) { b[i1][j1]=r2[k1++]; } } count1=0; for(i1=0;i1<8;i1++) { count1++; tz[0]=b[i1][0]; tz[1]=b[i1][5]; tmp=(tz[0]*2)+tz[1]; k1=0; printf("\n"); for(j1=1;j1<5;j1++) { tz[k1]=b[i1][j1]; printf("%d",tz[k1]); k1++; } tmp1=(tz[0]*8)+(tz[1]*4)+(tz[2]*2)+tz[3]; switch(count1) { case 1 : temp=s1[tmp][tmp1]; break; case 2 : temp=s2[tmp][tmp1]; break; case 3 : temp=s3[tmp][tmp1]; break; case 4 : temp=s4[tmp][tmp1]; break; case 5 : temp=s5[tmp][tmp1]; break; case 6 : temp=s6[tmp][tmp1]; break; case 7 : temp=s7[tmp][tmp1]; break; case 8 : temp=s8[tmp][tmp1]; break; }
printf("temp is %d",temp); t11=0; if(temp==0) { for(m=0;m<4;m++) b1[i1][m]=0; } else { while(temp>0) { a1[t11]=temp%2; temp=temp/2; t11++; } for(m=t11;m<4;m++) a1[m]=0; z=0; for(m=3;m>=0;m--) { b1[i1][z++]=a1[m]; } } } k1=0; printf("b1 is\n"); for(i1=0;i1<8;i1++) { for(j1=0;j1<4;j1++) printf("%d",b1[i1][j1]); printf("\n"); }
for(i1=0;i1<8;i1++) { for(j1=0;j1<4;j1++) c11[k1++]=b1[i1][j1]; }
/*permutation after substitution*/
for(i1=0;i1<32;i1++) res[i1]=c11[p[i1]-1]; for(i1=0;i1<32;i1++) printf("%d",c11[i1]); printf("after substn\n"); for(i1=0;i1<32;i1++) printf("%d",res[i1]); printf("xor l[i] with res[i]\n");
for(i1=0;i1<32;i1++) { if(l[i1]==res[i1]) res[i1]=0; else res[i1]=1; } for(i1=0;i1<32;i1++) l[i1]=r[i1]; for(i1=0;i1<32;i1++) r[i1]=res[i1]; printf("left and right for next round\n"); for(i1=0;i1<32;i1++) printf("%d",l[i1]); printf("\n"); for(i1=0;i1<32;i1++) printf("%d",r[i1]); round++; }
for(i3=0;i3<32;i3++) { tmp3[i3]=r[i3]; } i4=0; for(i3=32;i3<64;i3++) { tmp3[i3]=l[i4++]; } printf("\nafter swapping\n");
for(i3=0;i3<64;i3++) printf("%d",tmp3[i3]);
for(i3=0;i3<64;i3++) { tmp4[i3]=tmp3[iip[i3]-1]; } printf("output is\n");
for(i3=0;i3<64;i3++) printf("%d",tmp4[i3]); j=0;
for(i=0;i<64;i++) { tmp5[j]=(tmp4[i]*128)+(tmp4[i+1]*64)+(tmp4[i+2]*32)+(tmp4[i+3]*16)+(tmp4[i+4]*8)+(tmp4[i+5]*4)+(tmp4[i+6]*2)+tmp4[i+7];
i=i+7; j++; }
printf("\n"); for(i=0;i<8;i++) printf("%c",tmp5[i]); getch(); }
void keygen() { for(i=0;i<56;i++) { key[i]=k[pc1[i]-1]; } for(i=0;i<28;i++) { c[i]=key[i]; } for(i=28;i<56;i++) { d[j++]=key[i]; } while(count<17) { t=c[0]; t1=d[0]; for(i=0;i<27;i++) { c[i]=c[i+1]; d[i]=d[i+1]; } if(i==27) { c[i]=t; d[i]=t1; } if((count==1)||(count==2)||(count==9)||(count==16)) count++; else if(flag==1) { count++; flag=0; } else flag=1; if(flag==0) { for(i=0;i<28;i++) c2[i]=c[i]; j=0; for(i=28;i<56;i++) c2[i]=d[j++]; for(i=0;i<48;i++) k2[count-1][i]=c2[pc2[i]-1]; } } }
Enter the string of 8 chars
SREERAMA
010100110101001001000101010001010101001001000001010011010100000111111111 0001001 1010011 0011101 1010000 0000000 0000001 0000000 0010011 r1: 100000000000000000000000001000000000000010100110
key 010000011101011110001101101111100110001110101010 110000011101011110001101100111100110001100001100 1000temp is 15 1110temp is 11 1111temp is 8 0110temp is 0 0011temp is 7 0011temp is 5 0110temp is 8 0110temp is 11 b1 is 1111 1011 1000 0000 0111 0101 1000 1011 11111011100000000111010110001011after substn 01101000100011101110101110001011xor l[i] with res[i] left and right for next round 00000000000000000100000000010011 10010111100111011010011101100110r1: 010010101111110011111011110100001110101100001101
key111111100010010011101000000101110110110100110111 101101001101100000010011110001111000011000111010 0110temp is 1 0110temp is 8 0000temp is 13 1001temp is 7 1000temp is 6 1100temp is 1 1100temp is 5 1101temp is 3 b1 is 0001 1000 1101 0111 0110 0001 0101 0011 00011000110101110110000101010011after substn 10000110010111110011100110000010xor l[i] with res[i] left and right for next round 10010111100111011010011101100110 10000110010111110111100110010001r1: 110000001100001011111110101111110011110010100011
key110110101100111000111000011011110000100111010100 000110100000110011000110110100000011010101110111 0011temp is 1 0000temp is 0 1001temp is 15 0011temp is 3 1010temp is 12 0001temp is 15 1010temp is 5 1011temp is 0 b1 is 0001 0000 1111 0011 1100 1111 0101 0000 00010000111100111100111101010000after substn 10010111011101010010000100001110xor l[i] with res[i] left and right for next round 10000110010111110111100110010001 00000000111010001000011001101000r1: 000000000001011101010001010000001100001101010000
key110011001011101100011110110000011110000111011111 110011001010110001001111100000010010001010001111 1001temp is 11 0101temp is 11 1000temp is 4 0111temp is 3 0000temp is 4 1001temp is 13 0101temp is 0 0111temp is 4 b1 is 1011 1011 0100 0011 0100 1101 0000 0100 10111011010000110100110100000100after substn 11010000110011010110001000101010xor l[i] with res[i] left and right for next round 00000000111010001000011001101000 01010110100100100001101110111011r1: 101010101101010010100100000011110111110111110110
key001001101011111001001111011001111001011010001101 100011000110101011101011011010001110101101111011 0001temp is 12 0011temp is 14 0101temp is 9 0101temp is 1 1101temp is 0 0111temp is 8 0110temp is 10 1101temp is 5 b1 is 1100 1110 1001 0001 0000 1000 1010 0101 11001110100100010000100010100101after substn 11010100100010001000110100110001xor l[i] with res[i] left and right for next round 01010110100100100001101110111011 11010100011000000000101101011001r1: 111010101000001100000000000001010110101011110011
key011010110111110001100010110110100001010111101111 100000011111111101100010110111110111111100011100 0000temp is 4 1111temp is 5 1110temp is 2 0001temp is 6 1011temp is 9 1011temp is 7 1110temp is 9 1110temp is 12 b1 is 0100 0101 0010 0110 1001 0111 1001 1100 01000101001001101001011110011100after substn 00101011011000001111000000111101xor l[i] with res[i] left and right for next round 11010100011000000000101101011001 01111101111100101110101110000110r1: 001111111011111110100101011101010111110000001100
key111010001110110111111000000011101101101110101101 110101110101001001011101011110111010011110100001 1010temp is 3 1010temp is 7 0100temp is 3 1110temp is 14 1111temp is 9 1101temp is 13 1111temp is 1 0000temp is 2 b1 is 0011 0111 0011 1110 1001 1101 0001 0010 00110111001111101001110100010010after substn 01110111010000100111001001011110xor l[i] with res[i] left and right for next round 01111101111100101110101110000110 10100011001000100111100100000111r1: 110100000110100100000100001111110010100000001111
key110101001110011100011011010100100111110111110001 000001001000111000011111011011010101010111111110 0000temp is 0 0100temp is 6 1100temp is 5 1111temp is 9 1101temp is 9 1010temp is 13 1011temp is 12 1111temp is 8 b1 is 0000 0110 0101 1001 1001 1101 1100 1000 00000110010110011001110111001000after substn 11111101000100010010000001011001xor l[i] with res[i] left and right for next round 10100011001000100111100100000111 10000000111000111100101111011111r1: 110000000001011100000111111001010111111011111111
key101111100100101111100110111011001111100001010111 011111100101110011100001000010011000011010101000 1111temp is 8 0010temp is 10 1001temp is 15 0000temp is 3 0001temp is 12 1100temp is 14 1101temp is 10 0100temp is 9 b1 is 1000 1010 1111 0011 1100 1110 1010 1001 10001010111100111100111010101001after substn 11011101111011010000110100001101xor l[i] with res[i] left and right for next round 10000000111000111100101111011111 01111110110011110111010000001010r1: 001111111101011001011110101110101000000001010100
key101110100111001100101001101001111100011011111010 100001011010010101110111000111010100011010101110 0000temp is 15 1101temp is 0 1010temp is 5 1011temp is 11 0011temp is 12 1010temp is 3 1101temp is 10 0111temp is 2 b1 is 1111 0000 0101 1011 1100 0011 1010 0010 11110000010110111100001110100010after substn 10000101111001111010011001000011xor l[i] with res[i] left and right for next round 01111110110011110111010000001010 00000101000001000110110110011100r1: 000000001010100000001000001101011011110011111000
key100010010001011101111101100111011001111101000011 100010011011111101110101101010000010001110111011 0001temp is 1 1101temp is 9 1110temp is 2 1010temp is 5 0101temp is 13 0001temp is 1 0111temp is 13 1101temp is 5 b1 is 0001 1001 0010 0101 1101 0001 1101 0101 00011001001001011101000111010101after substn 10100011000111000111100000100111xor l[i] with res[i] left and right for next round 00000101000001000110110110011100 11011101110100110000110000101101r1: 111011111011111010100110100001011000000101011011
key110001010101101011011101100111101100011001110100 001010101110010001111011000110110100011100101111 0101temp is 15 0111temp is 1 1000temp is 2 1101temp is 7 0011temp is 1 1010temp is 4 1110temp is 6 0111temp is 13 b1 is 1111 0001 0010 0111 0001 0100 0110 1101 11110001001001110001010001101101after substn 10101000110100001101111000101110xor l[i] with res[i] left and right for next round 11011101110100110000110000101101 10101101110101001011001110110010r1: 010101011011111010101001010110100111110110100101
key000101111111101111100000010110011110111111000100 010000100100010101001001000000111001001001100001 1000temp is 3 0010temp is 7 1010temp is 5 0100temp is 6 0000temp is 2 1100temp is 6 0100temp is 4 0000temp is 2 b1 is 0011 0111 0101 0110 0010 0110 0100 0010 00110111010101100010011001000010after substn 01000100011100110101001010011010xor l[i] with res[i] left and right for next round 10101101110101001011001110110010 10011001101000000101111010110111r1: 110011110011110100000000001011111101010110101111
key100110100111110111100011101110001110010010011001 010101010100000011100011100101110011000100110110 1010temp is 12 1010temp is 2 0001temp is 7 0001temp is 15 0010temp is 12 1001temp is 14 0010temp is 2 1011temp is 13 b1 is 1100 0010 0111 1111 1100 1110 0010 1101 11000010011111111100111000101101after substn 11011101111001011001110001101100xor l[i] with res[i] left and right for next round 10011001101000000101111010110111 01110000001100010010111111011110r1: 001110100000000110100010100101011111111011111100
key111110010110011101001101111010110111011000000111 110000110110011011101111011111101000100011111011 1000temp is 15 1011temp is 6 1101temp is 11 0111temp is 8 1111temp is 6 0100temp is 2 0001temp is 11 1101temp is 5 b1 is 1111 0110 1011 1000 0110 0010 1011 0101 11110110101110000110001010110101after substn 01000110101001001000111111110111xor l[i] with res[i] left and right for next round 01110000001100010010111111011110 11011111000001001101000101000000r1: 011011111110100000001001011010100010101000000001
key001011110011110010111110001010111001101110110011 010000001101010010110111010000011011000110110010 1000temp is 3 0110temp is 8 1001temp is 13 1011temp is 11 1000temp is 8 1101temp is 11 0011temp is 14 1001temp is 6 b1 is 0011 1000 1101 1011 1000 1011 1110 0110 00111000110110111000101111100110after substn 10010101011110110010011101100011xor l[i] with res[i] left and right for next round 11011111000001001101000101000000 11100101010010100000100010111101 after swapping 1110010101001010000010001011110111011111000001001101000101000000output is 1100100110010000111000011001010110001001010000011101101011001001
/********** RSA PROGRAM ***********/ /*d value should be less than 11 bcoz (c^d)modn can't be computed using available datatypes*/
#include #include #include
void main() { char a[]={"0ABCDEFGHIJKLMNOPQRSTUVWXYZ"}; int n,i,j,s,n2,k1,p,q,d,m1,e1,l5,z,p2[30],s1,c[30]; unsigned long int l3,m,l4,k2; double l2,l1,l6; float e,l; char p1[30]; clrscr();
printf("enter two prime numbers p and q\n"); scanf("%d %d",&p,&q); do{ n=p*q; if(n<26) { printf("\n n value is not large enough.\nplease select p, q value such that p*q is greater than 26"); scanf("%d %d",&p,&q);} }while(n<26);
z=((p-1)*(q-1)); printf("enter the value of d:\n"); scanf("%d",&d); for(j=1;j { if((j*d)%z==1) break; } e=j; printf("%d %d %f\n",n,z,e); printf("ENCRYPTION-CIPHERTEXT"); printf("enter the plain text\n"); scanf("%s",p1);
for(i=0;i { for(j=1;j { if(a[j]==p1[i]) { s=j; break; } else continue; } printf("%d",s); e1=(int)e; l1=pow(((double)s),((double)e1)); k2=fmod(l1,(double)n); printf("\n%lu\n",k2); c[i]=(int)k2; printf("cipher:%d\n",c[i]); } printf("\n"); for(i=0;i { l2=(pow(((double)c[i]),((double)d))); m=fmod(l2,(double)n); m1=(int)m; printf(" %c\n",a[m1]);} getch(); }
RSA OUTPUT
enter two prime numbers p and q 3 11 enter the value of d: 7 33 20 3.000000 ENCRYPTION-CIPHERTEXTenter the plain text SUZANNE 19 28 cipher:28 21 21 cipher:21 26 20 cipher:20 1 1 cipher:1 14 5 cipher:5 14 5 cipher:5 5 26 cipher:26
S U Z A N N E
RSA output:
enter two prime numbers p and q 5 13 enter the value of d: 7 65 48 7.000000 ENCRYPTION-CIPHERTEXTenter the plain text NAINA 14 14 cipher:14 1 1 cipher:1 9 9 cipher:9 14 14 cipher:14 1 1 cipher:1
N A I N A
|