1) Program to simulate bit stuffing using

High Speed Data Link Control(HDLC)

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
int i=0,j=0,count=0,d=0,m=0,n=0,p=0;

//

Initialization variables

char
str[100],dest[100],dest1[100],flag[]="01111110";
clrscr();
printf("Enter the data\n");
scanf("%s",str);
strcpy(dest,flag);
// Initial flag to indicate
the start of data

j=strlen(flag);
n=strlen(str);

// variable j has the length of flag
// variable n

has the length of

input string

m=j;
while(str[i]!='\0')
{
if(str[i]=='1')
count++;

// searching for consuctive five 1's

have occured

else
count=0;
dest[j]=str[i];
if(count==5)
{
j++;
1

if(count==5) { i=i+1. dest1[p]=dest[i]. } p++. count=0.dest[j]='0'.i<m+n+d. } 2 . // This is used to indicate the total stuffed bits count=0. //Destuffing of 0 that inserted after five 11111 printf("\n The destuffed sequence is"). // stuffing of 0 if consective five ones have occured d++. } dest[j]='\0'. //* neglecting the stuffed 0 bit count=0. strcat(dest. for(i=m. else count=0. } i++. dest). j++.i++) { if(dest[i]=='1') count++.flag). //*Addition of flag at the end of data printf("\n Transmitted data is = %s ".

dest1).dest1[p]='\0'. } Output :Enter the data 0111111000111110101 Transmitted data is 0111111001111101000111110010101111110 Destuffed data is 0111111000111110101 3 */ . getch(). printf("= %s".

k=0. // Initialization variables char src[100].h> void main() { int i=0. if(src[i]=='d' && src[i+1]=='l' && src[i+2]=='e') { dest[j]='d'. strcpy(dest. while(src[i]!='\0') // check for occurance of "dle" and adding "dle" { // charcter stuffing dest[j]=src[i]..h> #include<string.&src).c. j=j+c.dest[100]. 4 . // getting the message scanf("%s". printf("Enter the data .end[]="dle _etx".start).j=0.dest1[100]. c=strlen(start).h> #include<conio. clrscr().start[]="dle_stx". \n ")..2) Program to simulate character stuffing and destuffing using HDLC #include<stdio.

dest[j+2]='e'. } dest[j]='\0'. printf("\n Destuffed data is . strcat(dest..end). k++. // Destuffing of data j=0. %s". j++.dest). printf("\n The character stuffed data is : %s\n".dest1).. dest[j+3]='d'. getch(). i++) { if(dest[i]=='d' && dest[i+1]=='l' && dest[i+2]=='e') i=i+3.. } i++. for(i=strlen(start).dest[j+1]='l'. } 5 . dest1[k]=dest[i]. dest[j+4]='l'. dest[j+5]='e'. i=i+2. } dest1[k]='\0'. j=j+5.i<(strlen(dest)strlen(end)) .

candle 6 .... candle The character stuffed data is : dle_stxcandledledle_etx Destuffed data is ..output :Enter the data..

scanf("%s". // adding key t each element encr[i]='\0'. // Initialization variables clrscr(). // getting message which to be encrypt for(i=0. char encr[100].&key).key. i++) encr[i]=encr[i]+key.decr[100]. printf("Enter the key constant ==>").3) Program to show the encryption and decryption of a given message using substitution method #include<stdio.h> #include<conio.h> void main() { int i.h> #include<string. i < strlen(encr) . scanf("%d". // getting key which to be added printf("Enter the message to be encrypted == > ").&encr). 7 .

printf("Enter the key constant ==> ").i++) // subbstract the key constant to decrypt decr[i]=encr[i]-key. getch(). for(i=0. } Output :- Enter the key constant ==> 2 Enter the message to be encrypted == > becbgk The encrypted message is ==> Enter the key constant ==> 2 The decrypted message is ==> becbgk 8 .i<strlen(encr).printf("The encrypted message is ==>"). puts(decr). puts(encr). decr[i]='\0'. printf("The decrypted message is ==>"). scanf("%d".&key).

m.key_ln.k. char code[8]="megabuck".dummy[50][8]. printf("\n welcome to the program of Encryption and decryption "). int v=0.j. 9 . // Any keyword can be used char decode[8]="egmbcuak".h> #include<conio.decr[24][8].4) Encryption and decryption using transposition method #include<stdio.h> #include<string. // Initialization variables char check='a'. clrscr().l=0.z. char encr[24][24].y.h> void main() { int i. char x[500].g.

l++. // getting message which to be encrypt i=strlen(x).j<8. printf("\n").i).i<m.i++) for(j=0. // lenght message is divided by length of code to make 'y' if(i%8==0) // 'y ' or 'y+1' rows m=y.i++) message data 10 // encryption of .i<m.printf("\n"). printf("Enter the message\n").j++) { dummy[i][j]=x[l]. } encrypt: for(. gets(x). y=i/8. // finding the message length and storing in 'i' printf("\n your message length is %d". if(l==k) goto encrypt. for(i=0. } k=i. if(i==0) { printf("As your massege contains no information \n hence program terminates press any key"). else m=y+1. goto end.

i++) { for(j=0.z<m. l=0.i<21. printf("\n").j<8. v++. } check='a'.encr[i][j]). } printf("\n").i++) { for(j=0.j++) if(check==decode[j]) { for(z=0.z<m.for(j=j+1.j<8. for(i=0. printf("\nthe encrypted message is==>").j++) dummy[i][j]='*'. // adding dummy '*' for(i=0. } check++.i<8.i++) { for(j=0.j++) printf("%c".j<8.z++) encr[v][z]=dummy[z][j]. 11 .j<m.z++) decr[z][l]=encr[j][z]. for(i=0.i<21.j++) if(check==code[j]) { for(z=0. printf("\n").

i<m.i++) for(j=0.l++. for(i=0. l=0.j++) if(l<k) { printf("%c".decr[i][j]). } check++. // decryption of message printf("\n the decrypted message is\n"). } end : getch().j<8. l++. } output (if message length is not zero) welcome to the program of Encryption and decryption Enter the message bagalkotcollege your message length is 15 the encrypted message is==> al le oe ao gl t* 12 .

bc kg the decrypted message is bagalkotcollege output (if message length is zero) welcome to the program of Encryption and decryption Enter the message your message length is 0 As your massege contains no information hence program terminates press any key (and program ends) 13 .