cn lab manual1

45
INDEX S.N o. Practical Date Remar k 1. 2. 3. 4. 5. 6. 7. Implementation of the Data Link Layer framing method such as character stuffing and bit stuffing in C. Implementation of CRC algorithm in C. Implementation of a Hamming (7,4) code to limit the noise. We have to code the 4 bit data in to 7 bit data by adding 3 parity bits. Implementation will be in C. Implementation of LZW compression algorithm in C. Write a socket program in C to implement a listener and a talker. Program to find shortest path routing between n nodes (Dijkstra’s Algorithm).

Upload: kumarishu125

Post on 12-Nov-2014

59 views

Category:

Documents


2 download

DESCRIPTION

iin this doc. cn practicals are stored for exam pracrioce by the help of this doc. student acheive highest marks .

TRANSCRIPT

Page 1: CN Lab Manual1

INDEX

S.No. Practical Date Remark

1.

2.

3.

4.

5.

6.

7.

Implementation of the Data Link Layer framing method such as character stuffing and bit stuffing in C.

Implementation of CRC algorithm in C.

Implementation of a Hamming (7,4) code to limit the noise. We have to code the 4 bit data in to 7 bit data by adding 3 parity bits. Implementation will be in C.

Implementation of LZW compression algorithm in C.

Write a socket program in C to implement a listener and a talker.

Program to find shortest path routing between n nodes (Dijkstra’s Algorithm).

Implementation of RSA Algorithm in C.

Page 2: CN Lab Manual1

Program-1

Implementation of the Data Link Layer framing method such as character stuffing

and bit stuffing in C.

/****************** BIT STUFFING PROGRAM *****************/

#include<stdio.h>

#include<string.h>

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<strlen(a)-4;i++)

{

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

{

t[p++]=a[j];

}

t[p]='\0';

if(strcmp(t,"11111")==0)

{

strcat(fs,"111110");

i=j-1;

}

Page 3: CN Lab Manual1

else

{

r[0]=a[i];

r[1]='\0';

strcat(fs,r);

}

p=0;

}

for(q=i;q<strlen(a);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

Page 4: CN Lab Manual1

/************** CHARACTER STUFFING PROGRAM*************/

#include<stdio.h>

#include<string.h>

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<strlen(a);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);

Page 5: CN Lab Manual1

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

Page 6: CN Lab Manual1

Program-2

Implementation of CRC algorithm in C.

#include< stdlib.h>

#include< conio.h>

#include< stdio.h>

void main()

{

int i,j,n,g,a,arr[20],gen[20],b[20],q[20],s;

clrscr();

printf("Transmitter side:");

printf("\nEnter no. of data bits:");

scanf("%d",&n);

printf("Enter data:");

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

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

printf("Enter size of generator:");

scanf("%d",&g);

do{

printf("Enter generator:");

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

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

}

while(gen[0]!=1);

printf("\n\tThe generator matrix:");

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

printf("%d",gen[j]);

a=n+(g-1);

printf("\n\tThe appended matrix is:");

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

arr[n+i]=0;

Page 7: CN Lab Manual1

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

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

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

q[i]= arr[i];

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

{

if(arr[i]==0)

{

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

arr[j] = arr[j]^0;

}

else

{

arr[i] = arr[i]^gen[0];

arr[i+1]=arr[i+1]^gen[1];

arr[i+2]=arr[i+2]^gen[2];

arr[i+3]=arr[i+3]^gen[3];

}

}

printf("\n\tThe CRC is :");

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

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

s=n+a;

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

q[i]=arr[i];

printf("\n");

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

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

getch();

}

Page 8: CN Lab Manual1

Output

Transmitter side:

Enter no. of data bits:8

Enter data:1 0 1 0 0 0 0 1

Enter size of generator:4

Enter generator:1 0 0 1

The generator matrix:1001

The appended matrix is:10100001000

The CRC is :111

10100001111

Page 9: CN Lab Manual1

Program-3

Implementation of a Hamming (7,4) code to limit the noise. We have to code the 4

bit data in to 7 bit data by adding 3 parity bits.Implementation will be in C.

#include<stdio.h>

#include<stdlib.h>

int main()

{

int e[7],i,j;

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

e[i]=0;

printf("\n enter the 4 bits\n");

for(i=3;i<7;i++)

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

e[0]=(e[4]+e[5]+e[6])%2;

e[1]=(e[3]+e[5]+e[6])%2;

e[2]=(e[3]+e[4]+e[6])%2;

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

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

}

OUTPUT

enter the 4 bits 1010

0101010

Page 10: CN Lab Manual1

Program-4

Implementation of LZW compression algorithm in C.

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#define BITS 12 /* Setting the number of bits to 12, 13*/

#define HASHING_SHIFT (BITS-8) /* or 14 affects several constants. */

#define MAX_VALUE (1 << BITS) - 1 /* Note that MS-DOS machines need to */

#define MAX_CODE MAX_VALUE - 1 /* compile their code in large model if*/

/* 14 bits are selected. */

#if BITS == 14

#define TABLE_SIZE 18041 /* The string table size needs to be a */

#endif /* prime number that is somewhat larger*/

#if BITS == 13 /* than 2**BITS. */

#define TABLE_SIZE 9029

#endif

#if BITS <= 12

#define TABLE_SIZE 5021

#endif

void *malloc();

int *code_value; /* This is the code value array */

unsigned int *prefix_code; /* This array holds the prefix codes */

unsigned char *append_character; /* This array holds the appended chars */

unsigned char decode_stack[4000]; /* This array holds the decoded string */

/*

* Forward declarations

*/

void compress(FILE *input,FILE *output);

void expand(FILE *input,FILE *output);

int find_match(int hash_prefix,unsigned int hash_character);

void output_code(FILE *output,unsigned int code);

Page 11: CN Lab Manual1

unsigned int input_code(FILE *input);

unsigned char *decode_string(unsigned char *buffer,unsigned int code);

/********************************************************************

**

** This program gets a file name from the command line. It compresses the

** file, placing its output in a file named test.lzw. It then expands

** test.lzw into test.out. Test.out should then be an exact duplicate of

** the input file.

**

*************************************************************************/

main(int argc, char *argv[])

{

FILE *input_file;

FILE *output_file;

FILE *lzw_file;

char input_file_name[81];

/*

** The three buffers are needed for the compression phase.

*/

code_value=(int*)malloc(TABLE_SIZE*sizeof(int));

prefix_code=(unsigned int *)malloc(TABLE_SIZE*sizeof(unsigned int));

append_character=(unsigned char *)malloc(TABLE_SIZE*sizeof(unsigned char));

if (code_value==NULL || prefix_code==NULL || append_character==NULL)

{

printf("Fatal error allocating table space!\n");

exit(-1);

}

/*

** Get the file name, open it up, and open up the lzw output file.

*/

if (argc>1)

strcpy(input_file_name,argv[1]);

else

Page 12: CN Lab Manual1

{

printf("Input file name? ");

scanf("%s",input_file_name);

}

input_file=fopen(input_file_name,"rb");

lzw_file=fopen("test.lzw","wb");

if (input_file==NULL || lzw_file==NULL)

{

printf("Fatal error opening files.\n");

exit(-1);

};

/*

** Compress the file.

*/

compress(input_file,lzw_file);

fclose(input_file);

fclose(lzw_file);

free(code_value);

/*

** Now open the files for the expansion.

*/

lzw_file=fopen("test.lzw","rb");

output_file=fopen("test.out","wb");

if (lzw_file==NULL || output_file==NULL)

{

printf("Fatal error opening files.\n");

exit(-2);

};

/*

** Expand the file.

*/

expand(lzw_file,output_file);

fclose(lzw_file);

fclose(output_file);

Page 13: CN Lab Manual1

free(prefix_code);

free(append_character);

}

/*

** This is the compression routine. The code should be a fairly close

** match to the algorithm accompanying the article.

**

*/

void compress(FILE *input,FILE *output)

{

unsigned int next_code;

unsigned int character;

unsigned int string_code;

unsigned int index;

int i;

next_code=256; /* Next code is the next available string code*/

for (i=0;i<TABLE_SIZE;i++) /* Clear out the string table before starting */

code_value[i]=-1;

i=0;

printf("Compressing...\n");

string_code=getc(input); /* Get the first code */

/*

** This is the main loop where it all happens. This loop runs util all of

** the input has been exhausted. Note that it stops adding codes to the

** table after all of the possible codes have been defined.

*/

while ((character=getc(input)) != (unsigned)EOF)

{

if (++i==1000) /* Print a * every 1000 */

{ /* input characters. This */

i=0; /* is just a pacifier. */

printf("*");

}

Page 14: CN Lab Manual1

index=find_match(string_code,character);/* See if the string is in */

if (code_value[index] != -1) /* the table. If it is, */

string_code=code_value[index]; /* get the code value. If */

else /* the string is not in the*/

{ /* table, try to add it. */

if (next_code <= MAX_CODE)

{

code_value[index]=next_code++;

prefix_code[index]=string_code;

append_character[index]=character;

}

output_code(output,string_code); /* When a string is found */

string_code=character; /* that is not in the table*/

} /* I output the last string*/

} /* after adding the new one*/

/*

** End of the main loop.

*/

output_code(output,string_code); /* Output the last code */

output_code(output,MAX_VALUE); /* Output the end of buffer code */

output_code(output,0); /* This code flushes the output buffer*/

printf("\n");

}

/*

** This is the hashing routine. It tries to find a match for the prefix+char

** string in the string table. If it finds it, the index is returned. If

** the string is not found, the first available index in the string table is

** returned instead.

*/

int find_match(int hash_prefix,unsigned int hash_character)

{

int index;

int offset;

Page 15: CN Lab Manual1

index = (hash_character << HASHING_SHIFT) ^ hash_prefix;

if (index == 0)

offset = 1;

else

offset = TABLE_SIZE - index;

while (1)

{

if (code_value[index] == -1)

return(index);

if (prefix_code[index] == hash_prefix &&

append_character[index] == hash_character)

return(index);

index -= offset;

if (index < 0)

index += TABLE_SIZE;

}

}

/*

** This is the expansion routine. It takes an LZW format file, and expands

** it to an output file. The code here should be a fairly close match to

** the algorithm in the accompanying article.

*/

void expand(FILE *input,FILE *output)

{

unsigned int next_code;

unsigned int new_code;

unsigned int old_code;

int character;

int counter;

unsigned char *string;

next_code=256; /* This is the next available code to define */

Page 16: CN Lab Manual1

counter=0; /* Counter is used as a pacifier. */

printf("Expanding...\n");

old_code=input_code(input); /* Read in the first code, initialize the */

character=old_code; /* character variable, and send the first */

putc(old_code,output); /* code to the output file */

/*

** This is the main expansion loop. It reads in characters from the LZW file

** until it sees the special code used to inidicate the end of the data.

*/

while ((new_code=input_code(input)) != (MAX_VALUE))

{

if (++counter==1000) /* This section of code prints out */

{ /* an asterisk every 1000 characters */

counter=0; /* It is just a pacifier. */

printf("*");

}

/*

** This code checks for the special

STRING+CHARACTER+STRING+CHARACTER+STRING

** case which generates an undefined code. It handles it by decoding

** the last code, and adding a single character to the end of the decode string.

*/

if (new_code>=next_code)

{

*decode_stack=character;

string=decode_string(decode_stack+1,old_code);

}

/*

** Otherwise we do a straight decode of the new code.

*/

else

string=decode_string(decode_stack,new_code);

/*

Page 17: CN Lab Manual1

** Now we output the decoded string in reverse order.

*/

character=*string;

while (string >= decode_stack)

putc(*string--,output);

/*

** Finally, if possible, add a new code to the string table.

*/

if (next_code <= MAX_CODE)

{

prefix_code[next_code]=old_code;

append_character[next_code]=character;

next_code++;

}

old_code=new_code;

}

printf("\n");

}

/*

** This routine simply decodes a string from the string table, storing

** it in a buffer. The buffer can then be output in reverse order by

** the expansion program.

*/

unsigned char *decode_string(unsigned char *buffer,unsigned int code)

{

int i;

i=0;

while (code > 255)

{

*buffer++ = append_character[code];

code=prefix_code[code];

if (i++>=MAX_CODE)

Page 18: CN Lab Manual1

{

printf("Fatal error during code expansion.\n");

exit(-3);

}

}

*buffer=code;

return(buffer);

}

/*

** The following two routines are used to output variable length

** codes. They are written strictly for clarity, and are not

** particularyl efficient.

*/

unsigned int input_code(FILE *input)

{

unsigned int return_value;

static int input_bit_count=0;

static unsigned long input_bit_buffer=0L;

while (input_bit_count <= 24)

{

input_bit_buffer |= (unsigned long) getc(input) << (24-input_bit_count);

input_bit_count += 8;

}

return_value=input_bit_buffer >> (32-BITS);

input_bit_buffer <<= BITS;

input_bit_count -= BITS;

return(return_value);

}

void output_code(FILE *output,unsigned int code)

{

static int output_bit_count=0;

static unsigned long output_bit_buffer=0L;

Page 19: CN Lab Manual1

output_bit_buffer |= (unsigned long) code << (32-BITS-output_bit_count);

output_bit_count += BITS;

while (output_bit_count >= 8)

{

putc(output_bit_buffer >> 24,output);

output_bit_buffer <<= 8;

output_bit_count -= 8;

}

}

Output Input String = /WED/WE/WEE/WEB/WET

Character Input Code Output New code value New String

/W / 256 /W

E W 257 WE

D E 258 ED

/ D 259 D/

WE 256 260 /WE

/ E 261 E/

WEE 260 262 /WEE

/W 261 263 E/W

EB 257 264 WEB

/ B 265 B/

WET 260 266 /WET

EOF T

The Compression Process

Input Codes: / W E D 256 E 260 261 257 B 260 T

Page 20: CN Lab Manual1

Input/NEW_CODE

OLD_CODESTRING/

OutputCHARACTER New table entry

/ / /

W / W W 256 = /W

E W E E 257 = WE

D E D D 258 = ED

256 D /W / 259 = D/

E 256 E E 260 = /WE

260 E /WE / 261 = E/

261 260 E/ E 262 = /WEE

257 261 WE W 263 = E/W

B 257 B B 264 = WEB

260 B /WE / 265 = B/

T 260 T T 266 = /WET

The Decompression Process

Page 21: CN Lab Manual1

Program-5

Write a socket program in C to implement a listener and a talker.

These two programs show how you can establish a socket connection using the above functions.

socket_server.c

#include <sys/types.h>

#include <sys/socket.h>

#include <sys/un.h>

#include <stdio.h>

#define NSTRS 3 /* no. of strings */

#define ADDRESS "mysocket" /* addr to connect */

/*

* Strings we send to the client.

*/

char *strs[NSTRS] = {

"This is the first string from the server.\n",

"This is the second string from the server.\n",

"This is the third string from the server.\n"

};

main()

{

char c;

FILE *fp;

int fromlen;

register int i, s, ns, len;

struct sockaddr_un saun, fsaun;

/*

* Get a socket to work with. This socket will

* be in the UNIX domain, and will be a

* stream socket.

*/

if ((s = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {

perror("server: socket");

Page 22: CN Lab Manual1

exit(1);

}

/*

* Create the address we will be binding to.

*/

saun.sun_family = AF_UNIX;

strcpy(saun.sun_path, ADDRESS);

/*

* Try to bind the address to the socket. We

* unlink the name first so that the bind won't

* fail.

*

* The third argument indicates the "length" of

* the structure, not just the length of the

* socket name.

*/

unlink(ADDRESS);

len = sizeof(saun.sun_family) + strlen(saun.sun_path);

if (bind(s, &saun, len) < 0) {

perror("server: bind");

exit(1);

}

/*

* Listen on the socket.

*/

if (listen(s, 5) < 0) {

perror("server: listen");

exit(1);

}

/*

* Accept connections. When we accept one, ns

* will be connected to the client. fsaun will

* contain the address of the client.

*/

Page 23: CN Lab Manual1

if ((ns = accept(s, &fsaun, &fromlen)) < 0) {

perror("server: accept");

exit(1);

}

/*

* We'll use stdio for reading the socket.

*/

fp = fdopen(ns, "r");

/*

* First we send some strings to the client.

*/

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

send(ns, strs[i], strlen(strs[i]), 0);

/*

* Then we read some strings from the client and

* print them out.

*/

for (i = 0; i < NSTRS; i++) {

while ((c = fgetc(fp)) != EOF) {

putchar(c);

if (c == '\n')

break;

}

}

/*

* We can simply use close() to terminate the

* connection, since we're done with both sides.

*/

close(s);

exit(0);

}

Page 24: CN Lab Manual1

socket_client.c

#include <sys/types.h>

#include <sys/socket.h>

#include <sys/un.h>

#include <stdio.h>

#define NSTRS 3 /* no. of strings */

#define ADDRESS "mysocket" /* addr to connect */

/*

* Strings we send to the server.

*/

char *strs[NSTRS] = {

"This is the first string from the client.\n",

"This is the second string from the client.\n",

"This is the third string from the client.\n"

};

main()

{

char c;

FILE *fp;

register int i, s, len;

struct sockaddr_un saun;

/*

* Get a socket to work with. This socket will

* be in the UNIX domain, and will be a

* stream socket.

*/

if ((s = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {

perror("client: socket");

exit(1);

}

/*

* Create the address we will be connecting to.

Page 25: CN Lab Manual1

*/

saun.sun_family = AF_UNIX;

strcpy(saun.sun_path, ADDRESS);

/*

* Try to connect to the address. For this to

* succeed, the server must already have bound

* this address, and must have issued a listen()

* request.

*

* The third argument indicates the "length" of

* the structure, not just the length of the

* socket name.

*/

len = sizeof(saun.sun_family) + strlen(saun.sun_path);

if (connect(s, &saun, len) < 0) {

perror("client: connect");

exit(1);

}

/*

* We'll use stdio for reading

* the socket.

*/

fp = fdopen(s, "r");

/*

* First we read some strings from the server

* and print them out.

*/

for (i = 0; i < NSTRS; i++) {

while ((c = fgetc(fp)) != EOF) {

putchar(c);

if (c == '\n')

break;

}

}

Page 26: CN Lab Manual1

/*

* Now we send some strings to the server.

*/

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

send(s, strs[i], strlen(strs[i]), 0);

/*

* We can simply use close() to terminate the

* connection, since we're done with both sides.

*/

close(s);

exit(0);

}

OUTPUT

We can simply use close () to terminate the connection, since we're done with both sides.

Page 27: CN Lab Manual1

Program-6

Program to find shortest path routing between n nodes(Dijkstra’s Algorithm).

#include<stdio.h>

#include<stdlib.h>

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<nodes;i++)

{

for(j=0;j<nodes;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);

Page 28: CN Lab Manual1

permanent[perm].src=initial;

permanent[perm].dest=initial;

permanent[perm++].length=0;

i=permanent[perm-1].dest-97;

for(j=0;j<nodes;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<nodes;i++)

{

if(i!=initial-97)

{

if(dsp[j][i]>0)

{

l=-1;

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

{

if(permanent[k].dest==(i+97))

l=k;

}

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

{

Page 29: CN Lab Manual1

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<perm;m++)

{

if(permanent[m].dest==temp[tem].src)

n=permanent[m].length;

}

temp[tem++].length=dsp[j][i]+n;

}

else

{

for(m=0;m<perm;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))

{

temp[l].length=n;

temp[l].src=j+97;

temp[l].dest=i+97;

}

}

}

}

Page 30: CN Lab Manual1

}

sort();

}

printf("\nShortest path:\n");

printf("From %c to %c is:",initial,dest);

for(i=0;i<perm-1;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++)

Page 31: CN Lab Manual1

{

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

Page 32: CN Lab Manual1

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

Page 33: CN Lab Manual1

Program-7

Implementation of RSA Algorithm in C.

/********** RSA PROGRAM ***********/

/*d value should be less than 11 bcoz (c^d)modn can't be computed using available

datatypes*/

#include<stdio.h>

#include<string.h>

#include<math.h>

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<z;j++)

{

Page 34: CN Lab Manual1

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<strlen(p1);i++)

{

for(j=1;j<strlen(a);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<strlen(p1);i++)

{

l2=(pow(((double)c[i]),((double)d)));

m=fmod(l2,(double)n);

m1=(int)m;

Page 35: CN Lab Manual1

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

Page 36: CN Lab Manual1

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