You are on page 1of 51

SIR BHAVSINHJI POLYTECHNIC

INSTITUTE BHAVNAGAR
DEPATMENT OF INFORMATION TECHNOLOGY
(AFFILIATED WITH GUJARAT TECHNOLOGICAL UNIVERSITY)

Ens LAB MANUAL


ESSENTIALS OF NETWORK SECURITY
Subject Code: 3351602
Semester: 5th

Guided by
G. M. PANDEY

Name:_________________________________________________________

Enrollment No:________________________________________________

Div: _____________________________ Batch: ____________________


INDEX

Name:_________________________________________________________

Enrollment No:________________________________________________

Div: _____________________________ Batch: ____________________

Page
No Name of Practical Date Sign Remarks
No
1 Write a c program to Encryption the Plaintext and 1
display the Ciphertext using Caesar Cipher.
2 Write a c program to Decryption the Ciphertext 3
and display the Plaintext using Caesar Cipher. 22/06
3 Write a c program to Encryption the Plaintext and 5
display the Ciphertext using Monoalphabetic
Technique.
4 Write a c program to Decryption the Ciphertext 8
and display the Plaintext using Monoalphabetic
Technique.
5 Write a c program to Encryption the Plaintext and 11 13/07
display the Ciphertext using Playfair.
6 Write a c program to Decryption the Ciphertext 19
and display the Plaintext using Playfair.
7 Write a c program to Encryption the Plaintext and 25
display the Ciphertext using Vegenere Cipher.
8 Write a c program to Decryption the Ciphertext 27
and display the Plaintext using Vegenere Cipher. 03/08
9 Write a c program to Encryption the Plaintext and 29
display the Ciphertext using Columnar
Transposition.
10 Write a c program to Decryption the Ciphertext 33
and display the Plaintext using Columnar 24/08
Transposition.
11 Write a c program to Encryption the Plaintext and 36
display the Ciphertext using One Time Pad.
12 Write a c program to Decryption the Ciphertext 38
and display the Plaintext using One Time Pad.
13 Write a c program to Encryption the Plaintext and 40
display the Ciphertext using Rail Fence
Technique.
14 Write a c program to Decryption the Ciphertext 42 21/09
and display the Plaintext using Rail Fence.
15 Explain simple Steganography example using 45
DOS command.
3351602_Essential of Network Security 196490316039

Practical – 1
Aim: Write a C program to encrypt the plaintext and display the cipher using
Caeser Cipher.
Ans:

#include<stdio.h>

#include<conio.h>

#include<string.h>

void main()

{
char plain[20],cipher[20]="";intlen,i,key;

clrscr();

printf("Enter Plaintext:");

gets(plain);

printf("Enter Key:");

scanf("%d",&key);

len=strlen(plain);

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

cipher[i]=plain[i]+key;

if((cipher[i]>'z') || (plain[i]<='Z' && cipher[i]>'Z'))

cipher[i]=cipher[i]-26;

Page | 1
3351602_Essential of Network Security 196490316039

puts(cipher);

getch();

Output:

Page | 2
3351602_Essential of Network Security 196490316039

Practical – 2
Aim: Write a C program to decrypt the cipher text and display the plaintext
using Ceaser Cipher.
Ans:

#include<stdio.h>

#include<conio.h>

#include<string.h>

void main()

int key,i,j;

char cipher[20],plain[20]="";

clrscr();

printf("Enter text to decrypt : ");

gets(cipher);

printf("Enter key : ");

scanf("%d",&key);

for(i=0;i<strlen(cipher);i++)

plain[i]=cipher[i]-key;

if(plain[i]<'A'|| plain[i]<'a' && cipher[i]>='a')

plain[i]=plain[i]+26;

puts(plain);

getch();

Page | 3
3351602_Essential of Network Security 196490316039

Output:

Page | 4
3351602_Essential of Network Security 196490316039

Practical – 3
Aim: Write a C program to Encryption the plaintext and display the cipher
using Monoalphabetic Substitution Cipher.
Ans.

#include<conio.h>

#include<stdio.h>

#include<string.h>

void encry();

char a[53]={'
','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','A','B','C','D','E','F','G','H','
I','J', 'K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};

char r[53]={'
','q','w','e','r','t','y','u','i','o','p','l','k','j','h','g','f','d','s','a','z','x','c','v','b','n','m','M','N','B','V','C','X','Z','A',
'S','D', 'F','G','H','J','K','L','P','O','I','U','Y','T','R','E','W','Q'};

char s,pt[300],ct[300];

int i,j;

void main()

int cont=1,ch; clrscr();

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

printf("%c",a[i]);

printf("\n");

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

printf("%c",r[i]);
Page | 5
3351602_Essential of Network Security 196490316039

printf("\n");

encry();

getch();

void encry()

printf("enter plain text\n");

scanf("%s",&pt);

for(i=0;i<strlen(pt);i++)

s=pt[i];

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

if(a[j]==s)

ct[i]=r[j]; break;

ct[i]='\0';

puts(ct);

Page | 6
3351602_Essential of Network Security 196490316039

Output:

Page | 7
3351602_Essential of Network Security 196490316039

Practical – 4
Aim: Write a C program to decryption the plaintext and display the cipher
using Monoalphabetic Substitution Cipher.
Ans:

#include<conio.h>

#include<stdio.h>

#include<string.h>

void decry();

char a[53]={'
','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','A','B','C','D','E','F','G','H','
I','J', 'K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};

char r[53]={'
','q','w','e','r','t','y','u','i','o','p','l','k','j','h','g','f','d','s','a','z','x','c','v','b','n','m','M','N','B','V','C','X','Z','A',
'S','D', 'F','G','H','J','K','L','P','O','I','U','Y','T','R','E','W','Q'};

char s,pt[300],ct[300]; int i,j;

void main()

int cont=1,ch; clrscr();

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

printf("%c",a[i]);

printf("\n");

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

printf("%c",r[i]);

}
Page | 8
3351602_Essential of Network Security 196490316039

printf("\n");

decry();

getch();

void decry()

printf("enter cipher text\n");

scanf("%s",&ct);

for(i=0;i<strlen(ct);i++)

s=ct[i];

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

if(r[j]==s)

pt[i]=a[j];

break;

pt[i]='\0';

puts(pt);

Page | 9
3351602_Essential of Network Security 196490316039

Output:

Page | 10
3351602_Essential of Network Security 196490316039

Practical – 5
Aim: Write a C program to Encrypt the plaintext and display the cipher using
Playfair Cipher.
Ans:

#include<stdio.h>

#include<conio.h>

#include<string.h>

void main()

char cipher[20]="",hide,c = 'a';

char key[20],plain[20]="";

char matrix[5][5] ={ NULL };

int i, j,k,temp,frowindex,srowindex,fcolindex,scolindex,count = 0;

clrscr();

printf("enter plain text : ");

gets(plain);

printf("Enter key : ");

gets(key);

for(i=0;i<strlen(key);i++)

for(j=i+1;j<strlen(key);j++)

if(key[i]==key[j])

for(k=j;k<strlen(key);k++)

Page | 11
3351602_Essential of Network Security 196490316039

key[k]=key[k+1];

next:

for(i=0;i<strlen(plain);i=i+2)

if(plain[i]==plain[i+1])

plain[i+1] = 'x';

for(j=strlen(plain);j>i+2;j--)

plain[j] = plain[j-1];

plain[i+2] = plain[i];

goto next;

if(strlen(plain) % 2 != 0)

plain[strlen(plain)]='x';

printf("\n");

Page | 12
3351602_Essential of Network Security 196490316039

printf("modified plain text : ");

puts(plain);

printf("\n");

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

for(j=0,c=c;j<5;j++,c++)

if(count>=strlen(key))

here:

for(k=0;k<strlen(key);k++)

if(c == key[k])

c++;

goto here;

else

temp = 1;

if(temp == 1)

matrix[i][j] = c;

Page | 13
3351602_Essential of Network Security 196490316039

if(i==2 && j==2)

c=c+1;

there:

for(k=0;k<strlen(key);k++)

if(c==key[k])

c=c+1;

goto there;

hide =c;

else

matrix[i][j]=key[count];

count++;

c--;

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

Page | 14
3351602_Essential of Network Security 196490316039

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

printf(" %c ",matrix[i][j]);

printf("\n");

for(i=0;i<strlen(plain);i=i+2)

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

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

if(plain[i] == matrix[j][k])

frowindex = j;

fcolindex = k;

else if (plain[i+1] == matrix[j][k])

srowindex = j;

scolindex = k;

Page | 15
3351602_Essential of Network Security 196490316039

if(plain[i] == hide)

frowindex = 2;

fcolindex = 2;

if(plain[i+1] == hide)

srowindex = 2;

scolindex = 2;

if(frowindex == srowindex )

if(fcolindex == 4)

cipher[i] = matrix[frowindex][0];

else

cipher[i] = matrix[frowindex][fcolindex+1];

if(scolindex == 4)

cipher[i+1] = matrix[srowindex][0];

else

Page | 16
3351602_Essential of Network Security 196490316039

cipher[i+1] = matrix[srowindex][scolindex+1];

else if(fcolindex == scolindex)

if(frowindex==4)

cipher[i] = matrix[0][fcolindex];

else

cipher[i] = matrix[frowindex+1][fcolindex];

if(srowindex == 4)

cipher[i+1] = matrix[0][scolindex];

else

cipher[i+1] = matrix[srowindex+1][fcolindex];

else

Page | 17
3351602_Essential of Network Security 196490316039

cipher[i] = matrix[frowindex][scolindex];

cipher[i+1]=matrix[srowindex][fcolindex];

printf("\n");

printf("cipher text : ");

puts(cipher);

getch();

Output:

Page | 18
3351602_Essential of Network Security 196490316039

Practical – 6
Aim: Write a C program to decrypt the plaintext and display the cipher using
Playfair Cipher.
Ans:

#include<stdio.h>

#include<Conio.h>

#include<string.h>

void main()

char plain[20]="",cipher[20],key[20],matrix[5][5],c='a',hide;

int count=0,i,j,k,m,frowindex,srowindex,fcolindex,scolindex;

clrscr();

printf("enter cipher text : ");

gets(cipher);

printf("enter key : ");

gets(key);

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

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

if(count<strlen(key))

matrix[i][j] = key[count];

count++;

Page | 19
3351602_Essential of Network Security 196490316039

else

next:

for(k=0;k<strlen(key);k++)

if(c==key[k])

c++;

goto next;

if(i==2 && j==2)

matrix[i][j] = c;

c=c+1;

here:

for(m=0;m<strlen(key);m++)

if(c==key[m])

c++;

goto here;

hide = c;

Page | 20
3351602_Essential of Network Security 196490316039

c=c+1;

else

matrix[i][j] = c;

c++;

printf("\n");

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

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

printf(" %c " ,matrix[i][j]);

printf("\n");

for(i=0;i<strlen(cipher);i=i+2)

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

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

Page | 21
3351602_Essential of Network Security 196490316039

if(cipher[i] == matrix[j][k])

frowindex = j;

fcolindex = k;

if(cipher[i+1] == matrix[j][k])

srowindex = j;

scolindex = k;

if(frowindex == srowindex)

if(fcolindex == 0)

fcolindex = 5;

if(scolindex == 0)

scolindex = 5;

plain[i] = matrix[frowindex][fcolindex-1];

plain[i+1] =matrix[srowindex][scolindex-1];

Page | 22
3351602_Essential of Network Security 196490316039

else if(fcolindex == scolindex)

if(frowindex == 0)

frowindex = 5;

if(srowindex == 0)

srowindex = 5;

plain[i]= matrix[frowindex-1][fcolindex];

plain[i+1] = matrix[srowindex-1][scolindex];

else

plain[i] = matrix[frowindex][scolindex]; plain[i+1] =


matrix[srowindex][fcolindex];

up:

for(i=0;i<strlen(plain);i++)

if(plain[i]=='x')

if(plain[i-1] == plain[i+1] && ((i) % 2) != 0)

Page | 23
3351602_Essential of Network Security 196490316039

for(j=i;j<strlen(plain);j++)

plain[j] = plain[j+1];

goto up;

if(plain[strlen(plain)-1]=='x')

plain[strlen(plain)-1] = 0 ;

printf("\n plain text : ");

puts(plain);

getch();

Output:

Page | 24
3351602_Essential of Network Security 196490316039

Practical – 7
Aim: Write a C program to encrypt the plaintext and display the cipher using
Vegenere Cipher.
Ans:

#include<stdio.h>

#include<conio.h>

void main()

char a[30],ch=97,plain[20],cipher[20]="",key[10];

int i,count = 0;

clrscr();

printf("\nplain alphabet\n");

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

a[i]=ch;

printf("%c=%i\t",a[i],i);

ch++;

printf("\nEnter plain text : ");

gets(plain);

printf("Enter key : ");

gets(key);

for(i=0;i<strlen(plain);i++)

cipher[i] = plain[i] - 97 + key[count] - 97;

Page | 25
3351602_Essential of Network Security 196490316039

if(cipher[i] >= 26 )

cipher[i] = cipher[i]-26;

cipher[i] = cipher[i] + 97;

count++;

if(count==strlen(key))

count=0;

printf("Cipher text : ");

puts(cipher);

getch();

Output:

Page | 26
3351602_Essential of Network Security 196490316039

Practical – 8
Aim: Write a C program to decrypt the plaintext and display the cipher using
Vegenere Cipher.
Ans:

#include<stdio.h>

#include<conio.h>

void main()

char a[30],ch=97,plain[20],cipher[20]="",key[10];

int i,count = 0;

clrscr();

printf("\nplain alphabet\n");

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

a[i]=ch;

printf("%c=%i\t",a[i],i);

ch++;

printf("\nEnter cipher text : ");

gets(cipher);

printf("Enter key : ");

gets(key);

for(i=0;i<strlen(plain);i++)

cipher[i] = plain[i] - 97 + key[count] - 97;

Page | 27
3351602_Essential of Network Security 196490316039

if(cipher[i] >= 26 )

cipher[i] = cipher[i]-26;

cipher[i] = cipher[i] + 97;

count++;

if(count==strlen(key))

count=0;

printf("plain text : ");

puts(plain);

getch();

Output:

Page | 28
3351602_Essential of Network Security 196490316039

Practical – 9
Aim: Write a C program to encrypt the plaintext and display the cipher using
Columnar Transposition technique.
Ans:

#include<stdio.h>

#include<conio.h>

#include<stdlib.h>

void main()

char cipher[50]="",plain[50],matrix[10][6]={""};

int n, i,key[6],j,count,temp,k,extra=1;

clrscr();

printf("Enter Plain text : ");

gets(plain);

printf("\n");

randomize();

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

up:

n=random(6)+1;

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

if(key[j] == n)

goto up;

Page | 29
3351602_Essential of Network Security 196490316039

key[i]=n;

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

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

count = 0; i=0;

do

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

if(strlen(plain)==count)

matrix[i][j] = 'x';

extra++;

else

matrix[i][j] = plain[count];

count++;

if(strlen(plain)==count && count % 6==0)

Page | 30
3351602_Essential of Network Security 196490316039

break;

i++;

}while(i<=strlen(plain)/6 );

printf("\n\n");

for(i=0;i<=strlen(plain)/6;i++)

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

printf("%c",matrix[i][j]);

printf("\n");

printf("\n");

temp=1;

count=0;

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

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

if(key[j] == temp)

break;

Page | 31
3351602_Essential of Network Security 196490316039

for(k=0;k<(strlen(plain)+extra)/6;k++)

//printf("%c",matrix[k][j]);

cipher[count] = matrix[k][j];

count++;

temp++;

printf("Cipher text : ");

puts(cipher);

getch();

Output:

Page | 32
3351602_Essential of Network Security 196490316039

Practical – 10
Aim: Write a C program to decrypt the ciphertext and display the plaintext
using Columnar Transposition technique.
Ans:

#include<stdio.h>

#include<conio.h>

void main()

char plain[50]="",cipher[50],matrix[10][6];

int i,j,k,key[6],temp;

clrscr();

printf("Enter Cipher text : ");

gets(cipher);

printf("Enter Key : ");

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

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

temp=0;

for(i=0;i<strlen(cipher)/6;i++)

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

matrix[i][j] = cipher[(key[j]-1)*(strlen(cipher)/6)+temp];

Page | 33
3351602_Essential of Network Security 196490316039

temp++;

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

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

printf("\n");

for(i=0;i<strlen(cipher)/6;i++)

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

printf("%c",matrix[i][j]);

printf("\n");

temp=0;

for(i=0;i<strlen(cipher)/6;i++)

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

plain[temp]=matrix[i][j]; temp++;

for(i=0;i<strlen(plain);i++)

Page | 34
3351602_Essential of Network Security 196490316039

if(plain[i] == 'x' && (plain[i+1]=='x' || plain[i+1] == '\0'))

plain[i] = '\0';

puts(plain);

printf("%s",strlen(plain));

getch();

Output:

Page | 35
3351602_Essential of Network Security 196490316039

Practical – 11
Aim: Write a C program to Encrypt the plaintext and display the cipher using
ONE time pad technique.
Ans:

#include<stdio.h>

#include<stdlib.h>

#include<string.h>

void main()

char plain[20],cipher[20]="",key[20]="";

int i,n;

clrscr();

printf("enter plain text : ");

gets(plain);

randomize();

for(i=0;i<strlen(plain);i++)

n=random(26);

key[i]=n+97;

printf("key : ");

puts(key);

for(i=0;i<strlen(plain);i++)

cipher[i] = plain[i]-97+key[i]-97;

Page | 36
3351602_Essential of Network Security 196490316039

if(cipher[i]>26)

cipher[i] = cipher[i]-26;

cipher[i] = cipher[i] + 97;

printf("cipher text : ");

puts(cipher);

getch();

Output:

Page | 37
3351602_Essential of Network Security 196490316039

Practical – 12
Aim: Write a C program to decrypt the cipher and display the plaintext using
ONE time pad technique.
Ans:

#include<stdio.h>

#include<conio.h>

#include<string.h>

void main()

char cipher[20],plain[20]="",key[20];

int i;

clrscr();

printf("enter cipher text : ");

gets(cipher);

printf("enter key : ");

gets(key);

for(i = 0;i<strlen(key);i++)

plain[i] = (cipher[i] - key[i]);

if(plain[i]<0)

plain[i] = plain[i]+26;

plain[i] = plain[i]+97;

Page | 38
3351602_Essential of Network Security 196490316039

printf("decrypted text : ");

puts(plain);

getch();

Output:

Page | 39
3351602_Essential of Network Security 196490316039

Practical – 13
Aim: Write a C program to encrypt the plaintext and display the Ciphertext
using Rail Fence technique.
Ans:

#include<stdio.h>

#include<conio.h>

#include<string.h>

void main()

char plain[20],cipher[20],temp1[10]="",temp2[10]="";

int count1=0,count2=0,i;

clrscr();

printf("enter plain text : ");

gets(plain);

for(i=0;i<strlen(plain);i++)

if(i%2==0)

temp1[count1] = plain[i];

count1++;

else

temp2[count2] = plain[i]; count2++;

Page | 40
3351602_Essential of Network Security 196490316039

strcat(temp1,temp2);

strcpy(cipher,temp1);

puts(cipher);

getch();

Output:

Page | 41
3351602_Essential of Network Security 196490316039

Practical – 14
Aim: Write a C program to decrypt the cipher and display the plaintext using
Rail Fence technique.
Ans:

#include<stdio.h>

#include<conio.h>

#include<string.h>

void main()

char cipher[20],plain[20] = "",temp1[20]="",temp2[20]="";

int count1=0,count2=0,i; clrscr();

printf("enter cipher text : ");

gets(cipher);

for(i=0;i<strlen(cipher);i++)

if((strlen(cipher)+1)/2>i)

temp1[i] = cipher[i];

else

temp2[count1] = cipher[i]; count1++;

count1=0;

Page | 42
3351602_Essential of Network Security 196490316039

count2=0;

for(i=0;i<strlen(cipher);i++)

if(i%2 == 0)

plain[i] = temp1[count1]; count1++;

else

plain[i] = temp2[count2]; count2++;

printf("decrypted text : ");

puts(plain);

getch();

Page | 43
3351602_Essential of Network Security 196490316039

Output:

Page | 44
3351602_Essential of Network Security 196490316039

Practical – 15
Aim: Example of Steganography
Ans:

Myimg.jpg

Page | 45
3351602_Essential of Network Security 196490316039

Mytext.txt

In Command prompt copy both Myimg.jpg and Mytext.txt file into new file that is
Myimg.jpg file.

Page | 46
3351602_Essential of Network Security 196490316039

After encryption Myimage.jpg open with notepad.

Sign:
Date:

Page | 47
3351602_Essential of Network Security 196490316039

Page | 48

You might also like