CN Lab Manual
CN Lab Manual
IMPLEMENTATION IN C/C++-:
#include <iostream.h>
#include <conio.h>
#include <ctype.h>
int SQR(int x)
{
int yes=0, i=1;
while(i<=17 && yes==0)
{
if(i*i==x)
{
yes=1;
sr=i;
}
else
i++;
}
return yes;
}
void main()
{
clrscr();
int choice, n;
cout<<"CAESAR CIPHER \n";
cout<<"1. Code your text \n";
cout<<"2. Decode a cipher \n";
cin>>choice;
char A[300], W;
switch (choice)
{
case 1:
clrscr();
do
{
cout<<"Enter the total number of letters in your message (perfect
square) ";
cin>>n;
} while(SQR(n)==0);
cout<<"Enter your message : \n";
for(int o=0; o<n; o++)
{
do
{
cin>>A[o];
} while (isalnum(A[o])==0);
}
int z=0;
clrscr();
for(int y=1; y<=2*sr; y+=2)
{
for(int q=1; q<=sr; q++)
{
gotoxy(y, q);
cout<<(char)toupper(A[z]);
z++;
}
cout<<endl;
}
cout<<"\nOR\nThe coded text is \n";
for(int qw=0; qw<sr; qw++)
{
for(int c=0; c<n; c+=sr)
cout<<(char)toupper(A[c+qw]);
}
break;
case 2:
clrscr();
do
{
cout<<"Enter the number of characters in your code ";
cin>>n;
} while(SQR(n)==0);
cout<<"Enter the cipher text as it appears in rows \n";
for(int b=0; b<n; b++)
{
cin>>A[b];
cout<<"\nThe decoded text is \n";
for(int h=0; h<sr; h++)
{
for(int c=0; c<n; c+=sr)
cout<<A[c+h];
} }
break;
}
getch();
}
EXPERIMENT NO:2
IMPLEMENTATION IN C/C++-:
#include<stdio.h>
#include<string.h>
#include<conio.h>
#include<process.h>
void main()
{
int ch=1,n=0,key;
char str[20];
clrscr();
printf("enter key to be used\n");
scanf("%d",&key);
key=key%26;
do
{
printf("enter 1 for encryption\n");
printf("enter 2 for decryption\n");
printf("enter 0 for exit\n");
scanf("%d",&ch);
if(ch==1)
{
n=0;
printf("enter text to be encrypted");
fflush(stdin);
scanf("%s",&str);
while(str[n]!='\0')
{
if(str[n]>=65&&str[n]<=90)
{
if((str[n]+32+key)>122)
{
str[n]=str[n]+32+key-122+96;
}
else
{
str[n]=str[n]+32+key;
}
n++;
}
puts(str);
}
if(ch==2)
{
n=0;
printf("enter text to be decrypted"); }
fflush(stdin);
scanf("%s",&str);
while(str[n]!='\0')
{
if(str[n]>=97&&str[n]<=122)
{
if((str[n]-(32+key))<65)
{
str[n]=str[n]-(32+key)+26;
}
else
{
str[n]=str[n]-(32+key);
}
}
n++;
}
puts(str);
}
}
while(ch!=0);
getch();
}
EXPERIMENT NO:3
O Z Y M A N D I A S
7 10 9 5 1 6 3 4 2 8
That is, the first occurance of the letter A is numbered 1, the second 2.
There are no B:s or C:s so the next letter to be numbered are the D
followed by I, and so on.
Next the plaintext is written in rows under the numbered keyword,
one letter under each letter of the keyword. Let's say that the plaintext
to be encrypted is Company has reached primary goal. It will look like
this:
O Z Y M A N D I A S
7 10 9 5 1 6 3 4 2 8
c o m p a n y h a s
r e a c h e d p r i
m a r y g o a l
Now the letters of the plaintext are copied down by reading them off
columnwise in the order stated by the enumeration of the keyword.
The result is the finished cryptogram, which - of course - are put into
groups of five letters, like this:
O Z Y M A N D I A S
7 10 9 5 1 6 3 4 2 8
. . . . a . . . . .
. . . . h . . . . .
. . . . g . . . * *
Next comes column number two. Since the last position in column two
is marked by a star and shouldn't be used, one only writes the next
two letters, instead of three. Continue in the same way by writing the
next three letters under keyword letter number three, and so on up to
keyword letter eight, it will look like this:
O Z Y M A N D I A S
7 10 9 5 1 6 3 4 2 8
c . . p a n y h a .
r . . c h e d p r .
m . . y g o a l * *
Now column eight follows, and there only two letters should be written
as stated above (the position marked by a star being left empty). This
leaves six letters of the cryptogram, and these - of course - are written
in column nine and ten, and then the cleartext can be read in the
normal way, row by row.
Usually when employing a transposition cipher like the above, one
adds dummy letters to make the final group five letters long if it isn't
already full. It is important to do this before transposing the letters,
otherwise the receiver can't calculate the columns that haven't a full
number of letters if the last row isn't complete. In some cases the last
row is always made complete by adding dummy letters, but that
reduces the security of the cipher and isn't recommended (now, this
cipher is quite easy to break anyway...).
IMPLEMENTATION IN C/C++-:
#include <iostream>
void main(void)
{
char strOriginalIput[100], strPass[35], strENCR[100], outpass[100];
int istrLen = 0;
int iArray[20] =
{
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20
};
cout << "\n Enter String(without space) : ";
cin >> strOriginalIput;
istrLen = strlen(strOriginalIput);
strOriginalIput[istrLen + 1] = '\0';
cout << "\n Enter Crypt Pass: ";
cin >> strPass;
int iLen = strlen(strPass);
strPass[iLen + 1] = '\0';
strcpy(outpass, strPass);
outpass[iLen + 1] = '\0';
int cnt = 0;
//cout << strOriginalIput << "\n";
for (int i = 0; i < iLen - 1; i++)
{
for (int j = 0; j < iLen - 1 - i; j++)
{
if (strPass[j + 1] < strPass[j])
{
/* compare the two neighbors */
char tmp = strPass[j]; /* swap a[j] and a[j+1] */
strPass[j] = strPass[j + 1];
strPass[j + 1] = tmp;
int t = iArray[j];
iArray[j] = iArray[j + 1];
iArray[j + 1] = t;
}
}
}
cnt = 0;
for (int z = 0; z < iLen; z++)
{
for (int x = 0; x <= iLen; x++)
{
if ((iArray[z] + iLen * x) <= istrLen)
{
strENCR[cnt++] = strOriginalIput[(iArray[z] + iLen * x) - 1];
}
}
}
strENCR[istrLen] = '\0'; //cout << strENCR << "\n\n" ;
// Output
int nl = 1;
for (i = 0; i < iLen; i++)
{
cout << outpass[i] << " ";
cout << "\n-------------------------------";
cout << "\n";
for (i = 0; i < istrLen; i++)
{
if (i == iLen * nl)
{
cout << "\n" << strOriginalIput[i] << " ";
nl++;
}
else
cout << strOriginalIput[i] << " ";
}
cout << "\n\n" << "Encrypted String : " << strENCR;
// Encryption is over, now going for decryption
cout << "\n";
char strtmp[100];
cnt = 0;
for (z = 0; z < iLen; z++)
{
for (int x = 0; x <= iLen; x++)
{
if ((iArray[z] + iLen * x) <= (istrLen))
strtmp[iArray[z] + (iLen * x) - 1] = strENCR[cnt+
+];
}
}
strtmp[istrLen] = '\0';
cout << "Decrypted String :" << strtmp << "\n\n";
}
}
EXPERIMENT NO:4
• The problem occurs when these character patterns occur within the
“transparent” data.
Solution: sender stuffs an extra DLE into the data stream just before each occurrence of
an “accidental” DLE in the data stream.
The data link layer on the receiving end unstuffs the DLE before giving the data to the
network
IMPLEMENTATION IN C/C++-:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char flag='F',p='*',esc='E';
char string[100],ch;
int l,i,j=1;
FILE *fp;
clrscr();
fp=fopen("flames","w");
printf("please enter the string");
gets(string);
l=strlen(string);
printf("%c %c",flag,string[0]);
putc(flag,fp);
putc(string[0],fp);
for(i=1;i<l;i++)
{
if(j==6)
{
printf("%c\n%c",flag,flag);
putc(flag,fp);
putc(flag,fp);
j=0;
}
if(string[i]=='*'||string[i]=='F'||string[i]=='E')
{
printf("%c",esc);
putc(esc,fp);
j++;
}
if(j!=6)
{
printf("%c",string[i]);
putc(string[i],fp);
j++;
}
}
for(i=j;i<6;i++)
{
printf("%c",p);
putc(p,fp);
}
printf("F");
fclose(fp);
printf("do u want to read file (Y/N):");
ch=getche();
if(ch=='y'||ch=='Y')
{
return;
}
j=0;
printf("\n");
fp=fopen("frames","r");
ch=getc(fp);
while(ch!=EOF)
{
if(ch==esc&&j==0)
{
ch=getc(fp);
j=1;
continue;
}
if(ch==flag&&j==0)
{
ch=getc(fp);
continue;
}
if(ch==p&&j==0)
{
ch=getc(fp);
continue;
}
if(ch==flag||ch==p||ch==esc&&j==1)
{
printf("%c",ch);
j=0;
ch=getc(fp);
continue;
}
else
{
printf("%c",ch);
ch=getc(fp);
continue;
}
}
getch();
}
EXPERIMENT NO:5
IMPLEMENTATION IN C/ C++-:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char text[100],text1[100];
int k,n,count=0,i,j,p;
clrscr();
printf("Enter text at sender end:");
gets(text);
n=strlen(text);
for(k=0;k<=n;k++)
{
text1[k]=text[k];
}
for(i=0;i<n;i++)
{
if(text[i]=='1')
count++;
if(text[i]=='0')
count=0;
if(count==5)
{
p=1+1;
for(j=n+1;j>p;j++)
{text[j]='0';}
n++;
count=0;
}
}
printf("Bit stuffed code is:");
printf("01111110");
for(k=0;k<strlen(text);k++)
printf("%c",text[k]);
printf("01111110");
printf("\n Text at reciever end:");
for(k=0;k<=n-1;k++)
{
printf("%c",text1[k]);
}
getch();
}
EXPERIMENT NO:6
Although CRCs can be constructed using any finite field, all commonly
used CRCs employ the finite field GF(2), the field of two elements,
usually called 0 and 1, comfortably matching computer architecture.
The rest of this article will discuss only these binary CRCs, but the
principles are more general.
METHOD-:
The mechanics of computing an n-bit binary CRC are simple. The bits
representing the input are lined up in a row, and the (n+1)-bit pattern
representing the CRC's divisor (called a "polynomial") is positioned
underneath the left-hand end of the row. Here is the first calculation
for computing a 3-bit CRC:
IMPLEMENTATION IN C/C++-:
#include<conio.h>
#include<stdio.h>
#include<process.h>
void main()
{
int msg[20],crc[20],n,i,j,p,a,rem[10],quo[10];
clrscr();
printf("Enter Mesage size");
scanf("%d",&n);
printf("Enter message in Binary");
for(i=0;i<n;i++)
{
scanf("%d",&msg[i]);
}
printf("Enter CRC Gen. size");
scanf("%d",&p);
printf("Enter crc bits in Binary");
for(i=0;i<p;i++)
{
scanf("%d",&crc[i]);
}
a=p-1;
for(i=0;i<a;i++)
{
msg[n+i]=0;
}
printf("The Code To be Sent is - ");
for(i=0;i<(n+a);i++)
{
printf("%d",msg[i]);
}
for(i=0;i<p;i++)
{
if (msg[i]==1 && crc[i]==1)
{
rem[i]=0;
}
if( msg[i]==1 && crc[i]==0)
{ rem[i]=1;
}
if(msg[i]==0 && crc[i]==1)
{
rem[i]=1;
}
if( msg[i]==1 && crc[i]==0)
{
rem[i]=0;
}
i=0;
if(rem[i]==0 && rem[i+1]== 0 && rem[i+2]==1)
{
rem[i+4]=msg[i+4];
rem[i+5]=msg[i+5];
if(crc[i]==0)
{
rem[i]=0;
}
if(crc[i]==1)
{
rem[i]=1;
}
if((rem[i+3]==1) && (crc[i+1]==0))
{
rem[i+1]=1;
}
if(rem[i+3]==1 && crc[i+1]==1)
{
rem[i+1]=0;
}
if(rem[i+3]==0 && crc[i+1]==1)
{
rem[i+1]=1;
}
if(rem[i+3]==0 && crc[i+1]==0)
{
rem[i+1]=0;
}
if(rem[i+4]==1 && crc[i+2]==0)
{
rem[i+2]=0;
}
if(rem[i+4]==1 && crc[i+2]==1)
{
rem[i+2]=0;
}
if(rem[i+4]==0 && crc[i+2]==0)
{
rem[i+2]=0;
}
if(rem[i+5]==0 && crc[i+3]==0)
{
rem[i+3]=1;
}
if(rem[i+5]==1 && crc[i+3]==1)
{
rem[i+3]=0;
}
if(rem[i+5]==0 && crc[i+3]==1)
{
rem[i+2]=1;
}
if(rem[i+5]==1 && crc[i+3]==0)
{
rem[i+3]=0;
}
if(rem[i]==0 && rem[i+1]==0 && rem[i+2]==0 && rem[i+3]==0)
{
if(crc[i]==0)
{
rem[i]=0;
}
if(crc[i]==1)
{
rem[i]=1;
}
if(crc[i+1]==0)
{
rem[i+1]=0;
}
if(crc[i+1]==1)
{
rem[i+1]=1;
}
if(crc[i+2]==0)
{
rem[i+2]=0;
}
if(crc[i+2]==1)
{
rem[i+2]=1;}
if(crc[i+3]==1)
{
rem[i+3]=0;
}
}
printf("The code to be sent is " );
for(j=0;j<n;j++)
{
printf("%d", msg[j]);
for(i=0;i<n;i++)
{
printf("%d",rem[i]);
}
}}}
getch();
}
EXPERIMENT NO:7
Something else that should be obvious from the graph is that any path
worth considering is simple. That is, you only go through each node
once.
Unfortunately, this is not always the case. The problem appears when
you allow negative weight edges. This isn’t by itself bad. But if a loop
of negative weight appears, then there is no shortest path. Look
at this example:
Look at the path B -> E -> D -> B. This is a loop, because the starting
node is the also the end. What’s the cost? It’s 10 - 20 + 5 = -5. This
means that adding this loop to a path once lowers the cost of the path
by 5. Adding it twice would lower the cost by 2 * 5 = 10. So, whatever
shortest path you may have come up with, you can make it smaller by
going through the loop one more time. BTW there’s no problem with a
negative cost path.
This algorithm calculates the length of the shortest path between all
nodes of a graph in O(V3) time. Note that it doesn’t actually find the
paths, only their lengths.
Let’s say you have the adjacency matrix of a graph. Assuming no loop
of negative values, at this point you have the minimum distance
between any two nodes which are connected by an edge.
ABCDE
A 0 10 0 5 0
B 10 0 5 5 10
C05000
D 5 5 0 0 20
E 0 10 0 20 0
The graph is the one shown above (the first one).
The idea is to try to interspace A between any two nodes in hopes of
finding a shorter path.
ABCDE
A 0 10 0 5 0
B 10 0 5 5 10
C05000
D 5 5 0 0 20
E 0 10 0 20 0
Then try to interspace B between any two nodes:
ABCDE
A 0 10 15 5 20
B 10 0 5 5 10
C 15 5 0 10 15
D 5 5 10 0 15
E 20 10 15 15 0
Do the same for C:
ABCDE
A 0 10 15 5 20
B 10 0 5 5 10
C 15 5 0 10 15
D 5 5 10 0 15
E 20 10 15 15 0
Do the same for D:
ABCDE
A 0 10 15 5 20
B 10 0 5 5 10
C 15 5 0 10 15
D 5 5 10 0 15
E 20 10 15 15 0
And for E:
ABCDE
A 0 10 15 5 20
B 10 0 5 5 10
C 15 5 0 10 15
D 5 5 10 0 15
E 20 10 15 15 0
This is the actual algorithm:
# dist(i,j) is "best" distance so far from vertex i to vertex j
include <stdio.h>
void printDist() {
int i, j;
printf(" ");
for (i = 0; i < n; ++i)
printf("%4c", 'A' + i);
printf("\n");
for (i = 0; i < n; ++i) {
printf("%4c", 'A' + i);
for (j = 0; j < n; ++j)
printf("%4d", dist[i][j]);
printf("\n");
}
printf("\n");
}
/*
floyd_warshall()
after calling this function dist[i][j] will the the minimum distance
between i and j if it exists (i.e. if there's a path between i and j)
or 0, otherwise
*/
void floyd_warshall() {
int i, j, k;
for (k = 0; k < n; ++k) {
printDist();
for (i = 0; i < n; ++i)
for (j = 0; j < n; ++j)
/* If i and j are different nodes and if
the paths between i and k and between
k and j exist, do */
if ((dist[i][k] * dist[k][j] != 0) && (i != j))
/* See if you can't get a shorter path
between i and j by interspacing
k somewhere along the current
path */
if ((dist[i][k] + dist[k][j] < dist[i][j]) ||
(dist[i][j] == 0))
dist[i][j] = dist[i][k] + dist[k][j];
}
printDist();
}
floyd_warshall();
return 0;
}