P. E. S. Institute of Technology Department of Electronics and Communication
P. E. S. Institute of Technology Department of Electronics and Communication
INSTITUTE OF TECHNOLOGY
Department of Electronics and Communication
(Session: Aug 08 Dec 08)
CYCLE OF EXPERIMENTS
Cycle I [CCN programming]
1. Simulate bit/character stuffing & de-stuffing using HDLC
2. Simulate the Shortest Path
3. Algorithm
4. Encryption and Decryption of a given message
5. Find Minimum Spanning Tree of a Subset
6. Compute Polynomial Code Checksum for CRC-CCITT
2/27
Dept of ECE
3/27
Dept of ECE
HDLC Frame
The high level data link control protocol is a bit oriented protocol which uses bit stuffing for data
transparency. HDLC frame is as shown.
The Control field is used for sequence numbers and acknowledgements and other purposes. The
Data field may contain arbitrary information. The Checksum field is a minor variation of the CRC
code using CRC-CCITT as the generator. Frames are delimited by 0111 1110 and this sequence is
not allowed in the data field.
Algorithm for Bit Stuffing
1. Input data sequence
2. Add start of frame to output sequence
3. For every bit in input
a. Append bit to output sequence
b. Is bit a 1?
Yes:
Increment count
If count is 5, append 0 to output sequence and reset count
4/27
Dept of ECE
No:
Set count to 0
4. Add stop of frame bits to output sequence
/* Program to simulate bit stuffing where the flag byte is 01111110 */
# include <stdio.h>
# include <conio.h>
void main()
{
/* Array is already initialised to 01111110 which is the flag byte. Hence a counter 'i'
/* has to point to the eight location in the same array */
char ch, array[50]={"01111110"},recd_array[50];
int counter=0,i=8,j,k;
clrscr();
/* Only the data portion of the frame is inputted */
printf("Enter the original data stream for bit stuffing : \n");
while((ch=getche())!='\r')
{
if (ch=='1')
++counter;
else
counter=0;
array[i++]=ch;
if (counter==5) /* If 5 ones are encountered append a zero */
{
array[i++]='0';
counter=0;
}
}
strcat(array,"01111110"); /* Appending the flag byte at the end of the stream */
/* i now has the value of the flag byte length + data stream length */
printf("\nThe stuffed data stream is : \n");
for (j=0;j<i+8;++j)
printf("%c",array[j]);
/* Destuffing */
counter=0;
printf("\nThe destuffed data stream is : \n");
for (j=8,k=0;j<i+8;++j)
{
if (array[j]=='1')
++counter;
else
counter=0;
recd_array[k++]=array[j];
if (counter==6) /* End if six ones are encountered */
break;
else if (counter==5 && array[j+1]=='0') /* If five ones appear, delete the
following zero */
{
++j;
counter=0;
5/27
Dept of ECE
}
}
for (j=0;j<=k-strlen("01111110");++j)
printf("%c",recd_array[j]); /* Printing the final destuffed array */
getch();
}
Output:
Enter the original data stream for bit stuffing :
011011111111110010
The stuffed data stream is :
011111100110111110111110001001111110
The destuffed data stream is :
011011111111110010
Byte stuffing:
Character based frame synchronization methods are used when the information in a frame
consists of an integer number of characters. For example, asynchronous transmission systems are
used extensively to transmit sequences of printable characters using 8 bit ASCII code . To
delineate a frame of characters, special eight bit codes that do not correspond to printable
characters are used as control characters. In ASCII code all characters with hexadecimal values
less than 20 correspond to non printable characters.
In particular STX (start of text ) control character has hex value 02 and indicates the beginning of
a frame and an ETX ( end of text ) character has HEX value 03 and denotes the end of a frame .
This method works if the frame contains only printable characters. If a frame carries computer
data, then it is possible that an ETX character will appear inside the frame and cause the receiver
to prematurely truncate the frame.This method is not transparent because the frame cannot carry
all possible bit sequences
The use of byte stuffing enables transparent operation. Byte stuffing operates as follows . A
special DLE ( data link escape) control with hex value 10 is introduced. The two character
sequence DLESTX is used to indicate the beginning of a frame and DLE ETX denotes the end of
a frame. The receiver looks for these character pairs to identify the beginning and end of
frames.In order to deal with the occurrence of DLE STX or DLE ETX in the data contained in
the frame, an extra DLE is inserted or stuffed before the occurrence of a DLE inside the frame.
Consequently every legitimate DLE in the data is replaced by two DLEs. The only incidence of
an individual DLE occurs when DLE precedes the STX or the ETX that identify the beginning
and end of the frame
6/27
Dept of ECE
Dept of ECE
{
if (ch==DLE) /* Checking for DLE */
{
array[i++]=DLE;
printf("DLE ");
}
else if (ch==STX) printf("STX ");
else if (ch==ETX) printf("ETX ");
else printf("%c ",ch);
array[i++]=ch;
}
array[i++]=DLE;
array[i++]=ETX;
printf("\nThe stuffed stream is \n");
for (j=0;j<i;++j)
{
if (array[j]==DLE) printf("DLE ");
else if (array[j]==STX) printf("STX ");
else if (array[j]==ETX) printf("ETX ");
else printf("%c ",array[j]);
}
/* Destuffing of character stream */
printf("\nThe destuffed data stream is : \n");
for (j=2;j<i-2;++j)
{
if (array[j]==DLE) /* Checking for DLE */
{ printf("DLE "); ++j; }
else if (array[j]==STX)
printf("STX ");
else if (array[j]==ETX)
printf("ETX ");
else
printf("%c ",array[j]);
}
getch();
}
Output:
Enter the data stream (Ctrl+B->STX, Ctrl+C->ETX, Ctrl+P->DLE) :
A DLE B
The stuffed stream is
DLE STX A DLE DLE B DLE ETX
The destuffed data stream is :
A DLE B
8/27
Dept of ECE
9/27
Dept of ECE
Algorithm
1. Input graph data
2. Make all nodes TENTATIVE.
2. Input source, destination
3. Make source, the working node
4. Make the working node PERMANENT.
5. Check all tentative nodes, which are connected to working node. Update
weight if required.
6. Find TENTATIVE node with smallest weight. Make this the new working
node.
7. If working node is destination, go to step 8 else go to step 4
8. Trace back from destination to source.
/* Program to calculate one Shortest Path Tanenbaum approach*/
# include<stdio.h>
# include<conio.h>
/*Total number of nodes in network*/
# define NUM_OF_NODES 10
/*State of each node*/
# define PERMANENT 1
# define TENTATIVE 0
struct node
{
unsigned int weight; /*weight of node therefore -1 is max value*/
int prev; /*previous node or is connected to*/
int state; /*state of the node*/
};
void main()
{
int table[NUM_OF_NODES][NUM_OF_NODES] =
{ /* A B C D E F G H I J*/
/*A*/ {0,1,0,0,0,4,0,0,0,0},
/*B*/ {1,0,4,0,0,0,0,1,0,0},
/*C*/ {0,4,0,3,2,0,0,0,3,0},
/*D*/ {0,0,3,0,1,0,0,0,0,0},
/*E*/ {0,0,2,1,0,3,0,0,0,1},
/*F*/ {4,0,0,0,3,0,1,0,0,0},
/*G*/ {0,0,0,0,0,1,0,2,0,2},
/*H*/ {0,1,0,0,0,0,2,0,1,0},
/*I*/ {0,0,3,0,0,0,0,1,0,2},
/*J*/ {0,0,0,0,1,0,2,0,2,0}
};/* A B C D E F G H I J*/
/*interpret as A is connected to B at a weight of 1 as
table[A][B]=table[B][A]=1*/
int src,dest,i,working_node;
/*src is source, dest is destination*/
unsigned int min;
struct node nodes[NUM_OF_NODES];
10/27
Dept of ECE
Dept of ECE
{
working_node=nodes[working_node].prev;
printf("<-%c",working_node+65);
}while(nodes[working_node].prev!=-1);
printf("\nAt a total weight of:%d",nodes[dest].weight);
}
Output:
Enter Source:A
Enter Destination:D
Shortest Path got--->
D<-E<-J<-I<-H<-B<-A
At a total weight of: 7
12/27
Dept of ECE
Dept of ECE
This general system is called a mono-alphabetic substitution, with the key being the 26letter string corresponding to the full alphabet. For the key above, the plaintext attack
would be transformed into the ciphertext QZZQEA.
Algorithm for Encryption by Substitution Method
1. Read data to be encoded
2. For every character of data
a. If data is between 'a' and 'z' set encoded data to uppercase character from key
b. If data is between 'A' and 'Z' set encoded data to lowercase character from key
c. If data between '0' and'9' set encoded data to digit from key
d. else copy data into encoded datas array.
3. Print encoded data.
/* Encryption by Substitution method */
/* Program to encrypt given data even numbers sequence is a key on which you change
data change all small letters to big and vice versa*/
# include<string.h>
# include<ctype.h>
# include<stdio.h>
void main()
{
const char sequence[36]="qwertyuiopasdfghjklzxcvbnm4852630791";
char data[100]; /*input data*/
char encoded[100]; /*encoded data*/
int i,len;
printf("\nEnter data to be encoded:");
gets(data);
len=strlen(data);
for(i=0;i<len;++i)
{
if(data[i]>='a' && data[i]<='z')
/*if small letter subtract from 97 so 'a' becomes 0 and convert
sequence[0] to uppercase for 'a'*/
encoded[i]=toupper(sequence[(data[i]-97)]);
else if(data[i]>='A' && data[i]<='Z')
/*if uppercase letter subtract 65 so that 'A' becomes 0 encoded data
is seqence['A'-65]*/
encoded[i]=sequence[(data[i]-65)];
else if(data[i]>='0' && data[i]<='9')
/*numbers are present at an offset of 26.. so index sequence on
char-48 + 26... so now '0' will be at 26 or sequence[26]*/
encoded[i]=sequence[(data[i]-48)+26];
else encoded[i]=data[i];
/*else send punctuation marks or special chars normally*/
}
encoded[len]='\0';
printf("Encoded string : %s",encoded);
}
Output:
Enter data to be encoded: Hello World!
Encoded string : iTSSG vGKSR!
14/27
Dept of ECE
Dept of ECE
Transposition Ciphers
Substitution ciphers preserve the order of the plaintext symbols but disguise them.
Transposition ciphers, in contrast, reorder the letters but do not disguise them. The figure
below depicts a common transposition cipher, the columnar transposition. The cipher is
keyed by a word or phrase not containing any repeated letters. In this example,
MEGABUCK is the key. The purpose of the key is to number the columns, column 1
being under the letter closet to the start of the alphabet and so on. The plain text is written
horizontally, in rows. The ciphertext is read out by columns, starting with the column
whose key letter is the lowest.
Plain text : pleasetransferonemilliondollarstomyswissbankaccountsixtwotwo
MEGABUCK
74512836
pleasetr
ansferon
emillion
dollarst
omyswiss
bankacco
untsixtw
otwoabcd
Ciphertext:
AFLLSKSOSELAWAIATOOSSCTCLNMOMANTESILYNTWRNNTSOWDPAE
DOBUOERIRICXB
Algorithm for Encryption by Transposition Method
1. Get sequence of characters in Cipher i.e. MEGABUCK
2. Get data to be decoded.
3. Arrange data horizontally under MEGABUCK
4. Add '.' to make last row complete
5. For every column of MEGABUCK
a. Find next column to send using sequence
b. Send data under this letter i.e. print the data under this letter
c. Jump back to 5 till all columns arent done
/* Encryption by Transposition method */
# include<string.h>
# include<stdio.h>
void main()
{
16/27
Dept of ECE
char data[100];
char wrd[]="MEGABUCK";
char cipher[8][20]; /*cipher arranges characters under megabuck*/
int seq[8]; /*holds MEGABUCK in alphabetical weights ie
MEGABUCK
6 3 4 0 1 7 2 5*/
int i,j,cnt,c;
/*to get 63401725*/
/*compare each character of word with every other char and keep count of number of characters
is bigger than.. i.e. A will be greater than 0 characters B will be greater than 1 character i.e. A so
1 and so on...*/
for(i=0;i<strlen(wrd);++i)
{
cnt=0;
for(j=0;j<strlen(wrd);++j)
if(wrd[i]>wrd[j])
++cnt;
seq[i]=cnt;
}
printf("\nEnter data:");gets(data);
cnt=strlen(data);
/* Arrange data under megabuck in order */
for(i=0;i<cnt;++i)
/* i%strlen(wrd) will correspond to column number i.e. changes
from 0-7 and back 0-7 as i goes from 0 to cnt-1 resetting to 0 at very 8th char
i/strlen(wrd) will go as 0,1,2,3,4, changing by 1 at every 8th character */
cipher[i%strlen(wrd)][i/strlen(wrd)]=data[i];
if(i%strlen(wrd)!=0) /*if last line is not full*/
{
for(j=i%strlen(wrd);j<strlen(wrd);++j) /*till last char fill with '.'*/
{ cipher[j][i/strlen(wrd)]='.'; ++cnt; }
}
printf("\nEncrypted data : \n");
for(i=0;i<strlen(wrd);++i)
{
for(c=0;c<strlen(wrd);++c)
if(seq[c]==i) break;
/*find index of value to print first under A i.e. 0 so as i=0 find which seq[c] is 0 print that
next we search for i=2 or for 2 in seq[c] this is printed out and so on*/
for(j=0;j<cnt/strlen(wrd) || j==0;++j)
printf("%c",cipher[c][j]);
}
}
Output:
Enter data: PLEASE TRANSFER ONE MILLION DOLLARS TO MY SWISS BANK
ACCOUNT SIX TWO TWO
Encrypted data :
AS WKTOSFMDTI RLL SCIWLANOR AUTENENSSNNWT LLM CXOPROIAYBO
EEIOOSAST
17/27
Dept of ECE
Dept of ECE
if(cipher[j][cnt/strlen(wrd)-1]=='.')
cipher[j][cnt/strlen(wrd)-1]=' ';
}
printf("\nDecrypted data : \n");
for(i=0;i<cnt;++i) /* print out row wise */
printf("%c",cipher[i%strlen(wrd)][i/strlen(wrd)]);
}
}
Output:
Enter data: AS WKTOSFMDTI RLL SCIWLANOR AUTENENSSNNWT LLM
CXOPROIAYBO EEIOOSAST
Decrypted data :
PLEASE TRANSFER ONE MILLION DOLLARS TO MY SWISS BANK ACCOUNT SIX
TWO TWO
19/27
Dept of ECE
In graph theory , a spanning tree is a graph in which there is no loop . In a bridged LAN ,
this means creating a topology in which each LAN can be reached from any other LAN
through one path only(no loops). This is done by automatically disabling certain bridges.
It is important that these bridges are not physically removed , since a topology change
may require a different set of bridges to be disabled, thus reconfiguring the spanning tree
dynamically
A spanning tree is a graph is just a sub-graph that contains all the vertices and is a
tree. A graph may have many spanning trees. For instance, a complete graph of 4 nodes
can have 16 spanning trees. Suppose the edges of the graphs have weights or lengths, the
weight of tree is the sum of its edges. Obviously, different trees have different lengths. A
spanning tree includes all the routers but contains no loops. If each router knows which of
its lines belong to the spanning tree, it can copy an incoming broadcast packet onto all the
spanning tree lines except the one it arrived on. This method makes excellent use of
bandwidth, generating the absolute minimum number of packets necessary to do the job.
The only problem is that each router must have knowledge of some spanning tree for it to
be applicable. Sometimes this information is available (e.g. with link state routing) but
sometimes it is not (e.g. with distance vector routing).
Spanning Trees:
In general a graph will have very many spanning trees. The weight of a subgraph to be
sum of all the edge weights in the subgraph. Different spanning trees have different
weights, the minimum spanning tree is the spanning tree with the minimum weight.
Example: weights added to the graph above
Dept of ECE
Dept of ECE
}
}
void initialise(int total_nodes)
{
int i;
for (i=0;i<total_nodes;++i)
node[i].set = i; /* Set number to indicate loop */
for (i=0;i<edge_count;++i)
e[i].selected=-1; /* Edge not selected initially */
}
void sort_edges()
{
int i,j;
struct edge temp;
for (i=0;i<edge_count-1;++i) /* Sorting by Bubble Sort */
for (j=0;j<edge_count-i-1;++j)
if (e[j].distance>e[j+1].distance)
{
temp=e[j];
e[j]=e[j+1];
e[j+1]=temp;
}
}
void main()
{
int total_vertices,i,j,k,m,n,edges_selected=0,nodel,noder;
clrscr();
printf("Enter the number of vertices : ");
scanf("%d",&total_vertices);
for (i=0;i<total_vertices;++i)
getdata(i,total_vertices);
initialise(total_vertices); /* Initialising all nodes and edges */
sort_edges();
/* Printing sorted order of the edges */
clrscr();
printf("Sorted order of edges : ");
for (i=0;i<edge_count;++i)
printf("\nEdge : %d First Node : %c Second Node : %c Distance :
%d",i,e[i].first_node+65,e[i].second_node+65,e[i].distance);
/* Finding the minimum spanning tree */
i=0; /* Initialise i to beginning of the edge array */
do
{
e[i].selected=1; /* Edge is selected */
nodel=e[i].first_node; /* Node on the left of the edge */
noder=e[i].second_node; /* Node on the right of the edge */
/* If the set is the same do not select that edge */
if (node[nodel].set==node[noder].set)
e[i].selected=-1; /* Deselect the edge */
else
{
22/27
Dept of ECE
Dept of ECE
24/27
Dept of ECE
Dept of ECE
int i=0,pos=0,newpos;
while(pos<length-DEGREE)
{
/* Performing mod-2 division for DEGREE number of bits */
for (i=pos;i<pos+DEGREE+1;++i)
result[i]=mod2add(result[i],ccitt[i-pos]);
newpos=getnext(result,pos);
/* Skipping the mod-2 division if lesser than DEGREE bits are available
for division */
if (newpos>pos+1)
pos=newpos-1;
++pos;
}
}
int getnext(int array[],int pos)
{
int i=pos;
/* Checking of the bits are all zeros and skipping */
while(array[i]==0)
++i;
return i;
}
int mod2add(int x,int y) /* Function to perform mod-2 addition */
{
return (x==y ? 0 : 1);
}
void main()
{
int array[30],ch;
int length,i=0;
clrscr();
/* Inputting data */
printf("Enter the data stream : ");
while((ch=getche())!='\r')
array[i++]=ch-'0';
length=i;
/* Appending zeros */
for (i=0;i<DEGREE;++i)
array[i+length]=0;
length+=DEGREE;
/* Duplicating the data input */
for (i=0;i<length;i++)
result[i]=array[i];
calc_CRC(length); /* Calculation of CRC */
printf("\nThe transmitted frame is : ");
for (i=0;i<length-DEGREE;++i) /* Printing the data */
printf("%d ",array[i]);
for (i=length-DEGREE;i<length;++i) /* Printing the checksum */
printf("%d ",result[i]);
printf("\nEnter the stream for which CRC has to be checked : ");
i=0;
26/27
Dept of ECE
27/27
Dept of ECE