VTU - CN [Computer Networks] Lab Programs.

Computer  Networks  Programs::

                          7. Crc.c -  CRC-CCITT

#include<stdio.h>
#include<string.h>
#define N strlen(g)

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

int a, e, c;

void xor() 

{
  for(c=1;c<N;c++) cs[c]=((cs[c]==g[c])?'0':'1');
}

void crc() 

{
  for(e=0;e<N;e++) cs[e]=t[e];
  do
   {
     if(cs[0]=='1') xor();
     for(c=0;c<N-1;c++) cs[c]=cs[c+1];
     cs[c]=t[e++];
    }
while(e<=a+N-1);
}

void main() 

{
  printf("\nEnter poly : "); scanf("%s",t);
  printf("\nGenerating Polynomial is : %s",g);
  a=strlen(t);
  for(e=a;e<a+N-1;e++) t[e]='0';
  printf("\nModified t[u] is :  %s",t);
  crc();
  printf("\nChecksum is : %s",cs);
  for(e=a;e<a+N-1;e++) t[e]=cs[e-a];
  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<N-1) printf("Error detected.");
  else printf("No Error Detected.");
}

----------------------------------------------------------------------------------------------------------

       8. Distvect.c -  Distance Vector Algorithm



#include <stdio.h>

const int MAX = 20, INFINITY = 999;

void find (int x,int y ,int path[MAX][MAX])
{
 printf("%d =>",x);
 if(y!=path[x][y])
  find(path[x][y],y,path);
}

int main() {

 int i, j, k,x,y;
 int d[MAX][MAX],path[MAX][MAX],n;
 printf("Enter the num of nodes (1 - 20): \n");
 scanf("%d", &n);
 printf("Enter the distance matrix (999 if no link): \n");
 for(i = 0; i < n; i++) {
  for(j = 0; j < n; j++) {
   scanf("%d", &d[i][j]);
   if(d[i][j] == INFINITY) path[i][j] = -1;
   else path[i][j] = j;
  }
 }
 for(k = 0; k < n; k++) {
  for(i = 0; i < n; i++) {
   for(j = 0; j < n; j++) {
    if(d[i][j] > d[i][k] + d[k][j]) {
     path[i][j] = k;
     d[i][j] = d[i][k] + d[k][j];
    }
   }
  }
 }
 for(i = 0; i < n; i++) {
  printf("\nRouting table for node %d \n", i);
  printf("Dest     Next hop      Distance \n");
  for(j = 0; j < n; j++) {
   printf("%3d       %3d        %3d\n", j, path[i][j], d[i][j]);
  }
 }
 while(1)
 {
  printf("enter 0 TO EXIT \n");
  scanf("%d",&x);
  if(!x)
   break;
  printf("Enter te 2 nodes \n");
   scanf("%d%d",&x,&y);
  find(x,y,path);
  printf("%d\n",y);
  printf("The distance between 2 nodes is %D ",d[x][y]);
 }
 return 0;
}

----------------------------------------------------------------------------------------------------------

                    11. Rsa.c -  RSA Algorithm

#include<stdio.h>
int z,m,n,e,d,c,flag;

void check()

{
int i;
for(i=3;e%i==0 && z%i==0;i++)
{
flag = 1;
}
flag = 0;
}

void encrypt()

{
int i;
c = 1;
for(i=0;i< e;i++)
c=c*m%n;
c=c%n;
printf("\n\tEncrypted keyword : %d",c);
}

void decrypt()

{
int i;
m = 1;
for(i=0;i< d;i++)
m=m*c%n;
m=m%n;
printf("\n\tDecrypted keyword : %d",m);
}

void main()

{
int p,q,s;
printf("Enter Two Relatively Prime Numbers\t: ");
scanf("%d%d",&p,&q);
n = p*q;
z=(p-1)*(q-1);
printf("\n z value= %d",z);
printf("\n\nEnter e: ",n);
scanf("%d",&e);
check();
while(flag==1);
d = 1;
do
{
s = (d*e)%z;
d++;
}
while(s!=1);
d = d-1;
printf("\n\tPublic Key\t: {%d,%d}",e,n);
printf("\n\tPrivate Key\t: {%d,%d}",d,n);
printf("\n\nEnter The Plain Text\t: ");
scanf("%d",&m);
encrypt();
printf("\n\nEnter the Cipher text\t: ");
scanf("%d",&c);
decrypt();
}
------------------------------------------------------------------------------------------------------------

        12. Leaky.c - Leaky Bucket Algorithm

#include<stdio.h>

#define bucketsize 500

void bktinput(int a,int b)
{
 if(a>bucketsize)
 printf("\nbucket overflow\n");
 else
 {
  while(a>b)
  {

   printf("\n%d bytes outputted",b);

   a=a-b;
   }
 if(a>0)
 printf("\n last %d bytes sent",a);
 printf("\n bucket output successful\n");
}
}
int main()
{
 int op,pktsize;
 printf("enter out rate:\n");
 scanf("%d",&op);
 printf("enter the packet size \n");
 scanf("%d",&pktsize);

  bktinput(pktsize,op);
}


1 comment:

  1. what is the value of "e" in 11th rsa program.

    ReplyDelete