0% found this document useful (0 votes)
81 views37 pages

Information Security

The document describes an algorithm for implementing the Playfair cipher for encryption and decryption of plaintext. It involves generating a 5x5 key matrix from the encryption key, and then encrypting pairs of letters from the plaintext by applying rules based on their positions in the matrix. The code provided implements functions for the Playfair cipher encryption and decryption by generating the key matrix, applying the rules to letters from the plaintext to encrypt or decrypt pairs of letters, and outputting the cipher or decrypted text.

Uploaded by

VamseeKrishna Ch
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
81 views37 pages

Information Security

The document describes an algorithm for implementing the Playfair cipher for encryption and decryption of plaintext. It involves generating a 5x5 key matrix from the encryption key, and then encrypting pairs of letters from the plaintext by applying rules based on their positions in the matrix. The code provided implements functions for the Playfair cipher encryption and decryption by generating the key matrix, applying the rules to letters from the plaintext to encrypt or decrypt pairs of letters, and outputting the cipher or decrypted text.

Uploaded by

VamseeKrishna Ch
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 37

1.Implement ceaser, Playfair and rail fence ciphers.

Aim:Write a program for Ceaser cipher ,Playfair & Railfence encryption and decryption
using files

Tools / Apparatus: O.S.: Microsoft Windows (any) / Linux / DOS

Packages: Turbo/Borland/GNU - C/C++/Java

Procedure:

Algorithm Encryption:

1. Open a file which contains the plain text in read mode

2. Create a new file to which the cipher to be written.

3. Read one by one character of file-1 and call encrypt function write the cipher character in
file2.

4. Close the files.

Algorithm Decryption:

1. Open a file which contains the cipher text in read mode

2. Read one by one character of file and call decrypt function

3. Close the file.

Note: Use an integer digit from 1-26 key. The same key is used for Encryption and
Decryption.

Encrypt function:

Read the key

if character is between A to Z .
code = character + key;

cipher_character = to_char(code);

Decryption function:

if character is between A to Z .

code= character – key;

code=code+26;

original_character = to_char(code);

Code:
#include<stdio.h>
void decrypt(){
char message[100], ch;
int i, key;

printf("Enter a message to decrypt: ");


// gets(message);
scanf("%s",message);
printf("Enter key: ");
scanf("%d", &key);

for(i = 0; message[i] != '\0'; ++i){


ch = message[i];

if(ch >= 'a' && ch <= 'z'){


ch = ch - key;

if(ch < 'a'){


ch = ch + 'z' - 'a' + 1;
}

message[i] = ch;
}
else if(ch >= 'A' && ch <= 'Z'){
ch = ch - key;

if(ch < 'A'){


ch = ch + 'Z' - 'A' + 1;
}

message[i] = ch;
}
}

printf("Decrypted message: %s", message);

int main()
{
char message[100], ch;
int i, key;
printf("Enter a message to encrypt:");
gets(message);
printf("Enter key: ");
scanf("%d", &key);
for(i = 0; message[i] != '\0'; ++i){
ch = message[i];
if(ch >= 'a' && ch <= 'z'){
ch = ch + key;
if(ch > 'z'){
ch = ch - 'z' + 'a' - 1;
}
message[i] = ch;
}
else if(ch >= 'A' && ch <= 'Z'){
ch = ch + key;
if(ch > 'Z'){
ch = ch - 'Z' + 'A' - 1;
}
message[i] = ch;
}
}
printf("Encrypted message: %s\n", message);
decrypt();
return 0;
}

Output:-
1.b

AIM: Implementation of Play Fair Cipher Encryption

Tools / Apparatus: O.S.: Microsoft Windows (any) / Linux / DOS

Packages: Turbo/Borland/GNU - C/C++

Procedure:

Step1: Generating Key matrix

To Generate the key matrix take any random key of any length and form a 5X5matrix. Go on
filling the rows of the matrix with the key characters (if repeating character occurs then
ignore it). Fill the remaining matrix with alphabets from A to Z(except those already occurred
in the key). For example for the key “monarchy” we have the matrix as follow

Step 2: Encrypt the data using encryption rule and key matrix

To Encrypt the data take two characters at time from plain text file and encrypt itusing one of
the following rules.

Encryption rules

1) Repeating plain text letters that would fall in the same pair are separated withfiller
letter,such as x.( i.e. Balloon becomes Ba, lx, lo, on).
2) If both the characters are in the same raw then replace each with the character toits right,
with

the last character followed by the first, in the matrix.

3) If both the characters are in the same column then replace each with the characterbelow it,
withthe bottom character followed by the top, in the matrix.

4) Otherwise each plain text letter is replaced by the letter that lies in its own rowand the
column occupied by the other plain text letter

Example:Using key as “monarchy” we have

- Encryption of AR as RM

- Encryption of MU as CM

- Encryption of BP as IM

Designing the Solution:

For this solution we have to implement the following functions given below.

1) Input function for key & Plain Text.

2) Matrix generation.

3) Encryption function for generating Cipher Text.

4) Print function for printing Cipher Text Output.

CODE:-

#include <stdio.h>

#include <string.h>

#include <ctype.h>

#define MX 5

char keystr[10];
int ind = 0;

int *removeDuplicate(int n)

int i;

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

int j;

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

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

break;

if (j == i)

keystr[ind++] = keystr[i];

return 0;

void playfair(char ch1, char ch2, char key[MX][MX])

int i, j, w, x, y, z;

FILE * out;

if ((out = fopen("cipher.txt", "a+")) == NULL)

printf("File Currupted.");
}

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

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

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

w = i;

x = j;

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

y = i;

z = j;

if (w == y)

x = (x + 1) % 5;

z = (z + 1) % 5;
printf("%c%c", key[w][x], key[y][z]);

fprintf(out, "%c%c", key[w][x], key[y][z]);

else if (x == z)

w = (w + 1) % 5;

y = (y + 1) % 5;

printf("%c%c", key[w][x], key[y][z]);

fprintf(out, "%c%c", key[w][x], key[y][z]);

else

printf("%c%c", key[w][z], key[y][x]);

fprintf(out, "%c%c", key[w][z], key[y][x]);

fclose(out);

void playfaird(char ch1, char ch2, char key[MX][MX])

int i, j, w, x, y, z;
FILE * out;

if ((out = fopen("cipher.txt", "a+")) == NULL)

printf("File Currupted.");

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

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

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

w = i;

x = j;

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

y = i;

z = j;

}
if (w == y)

x = (x - 1) % 5;

z = (z - 1) % 5;

printf("%c%c", key[w][x], key[y][z]);

fprintf(out, "%c%c", key[w][x], key[y][z]);

else if (x == z)

w = (w - 1) % 5;

y = (y - 1) % 5;

printf("%c%c", key[w][x], key[y][z]);

fprintf(out, "%c%c", key[w][x], key[y][z]);

else

printf("%c%c", key[w][z], key[y][x]);

fprintf(out, "%c%c", key[w][z], key[y][x]);

fclose(out);

}
int main()

int i, j, k = 0, m = 0, n;

char key[MX][MX], keyminus[25], str[25] = {0};

char alpa[26] = {
'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'};

printf("\nEnter key:");

gets(keystr);

n = strlen(keystr);

removeDuplicate( n);

n = ind;

//convert the characters to uppertext

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

if (keystr[i] == 'j') keystr[i] = 'i';

else if (keystr[i] == 'J') keystr[i] = 'I';

keystr[i] = toupper(keystr[i]);

printf("\nEnter the plain text/ chiper text:");

gets(str);
//convert all the characters of plaintext to uppertext

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

if (str[i] == 'j') str[i] = 'i';

else if (str[i] == 'J') str[i] = 'I';

str[i] = toupper(str[i]);

// store all characters except key

j = 0;

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

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

if (keystr[k] == alpa[i]) break;

else if (alpa[i] == 'J') break;

if (k == n)

keyminus[j] = alpa[i];

j++;

}
}

//construct key keymatrix

k = 0;

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

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

if (k < n)

key[i][j] = keystr[k];

k++;

else

key[i][j] = keyminus[m];

m++;

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

printf("\n");

}
printf("1 encription\n2 decription");

int z;

scanf("%d",&z);

if(z==1)

// construct diagram and convert to cipher text

printf("\n\nEntered text :%s\nCipher Text :", str);

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

if (str[i] == 'J') str[i] = 'I';

if (str[i + 1] == '\0') playfair(str[i], 'X', key);

else

if (str[i + 1] == 'J') str[i + 1] = 'I';

if (str[i] == str[i + 1]) playfair(str[i], 'X', key);

else

playfair(str[i], str[i + 1], key);

i++;

}
}

else if(z==2)

printf("\n\nEntered text :%s\nPlain Text :", str);

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

if (str[i] == 'J') str[i] = 'I';

if (str[i + 1] == '\0') playfaird(str[i], 'X', key);

else

if (str[i + 1] == 'J') str[i + 1] = 'I';

if (str[i] == str[i + 1]) playfaird(str[i], 'X', key);

else

playfaird(str[i], str[i + 1], key);

i++;

}
}

return 0;

Output:
1.3
AIM:- Implementation of encryption and decryption using railfence cipher

Tools / Apparatus: O.S.: Microsoft Windows (any) / Linux / DOS

Packages: Turbo/Borland/GNU - C/C++

Procedure:

Rail Fence cipher is a Transposition cipher. Encryption is the result by changing the position
of the message. In this particular scheme the message is written in two rows. That is the first
character is written in the first row, second character is written in the second row and so on.
To get the cipher read the message off, row by row, first row followed by second row.

Algorithm Encryption:

1. Read plain text

2. Consider CT as a temporary string to which cipher is copied

3. Copy all the even indexed letters of the plain text to CT

4. Copy all the odd indexed letters of the plain text to CT

5. CT contains the cipher

Algorithm Decryption:

1. Read cipher text, CT

2. Consider PT as a temporary string to which plain text is copied

3. k=strlen(CT)/2

4. i=0,j=0;

5. PT[i]=CT[i]
6. PT[i+1]=CT[k]

7. i++,j++,k++

8. Repeat steps 5,6,7 till the end of the char is reached in CT

9. PT contains the plain text derived based on cipher

Code:-

#include<stdio.h>

#include<string.h>

void encryptMsg(char msg[], int key){

int msgLen = strlen(msg), i, j, k = -1, row = 0, col = 0;

char railMatrix[key][msgLen];

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

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

railMatrix[i][j] = '\n';

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

railMatrix[row][col++] = msg[i];

if(row == 0 || row == key-1)


k= k * (-1);

row = row + k;

printf("\nEncrypted Message: ");

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

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

if(railMatrix[i][j] != '\n')

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

void decryptMsg(char enMsg[], int key){

int msgLen = strlen(enMsg), i, j, k = -1, row = 0, col = 0, m = 0;

char railMatrix[key][msgLen];

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

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

railMatrix[i][j] = '\n';
for(i = 0; i < msgLen; ++i){

railMatrix[row][col++] = '*';

if(row == 0 || row == key-1)

k= k * (-1);

row = row + k;

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

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

if(railMatrix[i][j] == '*')

railMatrix[i][j] = enMsg[m++];

row = col = 0;

k = -1;

printf("\nDecrypted Message: ");

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

printf("%c", railMatrix[row][col++]);
if(row == 0 || row == key-1)

k= k * (-1);

row = row + k;

int main(){

char msg[100];

int key,k;

printf("enter the message: ");

gets(msg);

printf("press 1 for ENCRYPTION\npress 2 for DECRYPTION \n");

scanf("%d",&k);

if(k==1)

printf("enter key: ");

scanf("%d",&key);

printf("Original Message: %s", msg);

encryptMsg(msg, key);
}

else if(k==2)

printf("enter key: ");

scanf("%d",&key);

printf("Original Message: %s", msg);

decryptMsg(msg, key);

else

printf(" enter a valid number");

return 0;

Output:-
2. AIM: Implementation of encryption and decryption using S-DES algorithm.

Procedure:

S-DES is a simplified version of DES. S-DES algorithm is used for the academic purpose.
S-DES uses bit wise operation on message letters to encrypt the data so it is more powerful
against the cryptanalysis attacks. This algorithm takes 8-bitof the message as input, also takes
10 bit key and produces 8 bit cipher text. This algorithm has two rounds. It generates 2, 8-bit
keys that are to be used in each round. Following figure shows the functional details of
S-DES.
Code:

#include <stdio.h>
int l[4],r[4],keys[2][8],ct[8];
void sbox(int sip[],int p[],int sbno,int i)
{
int sbox[2][4][4]={1,0,3,2,3,2,1,0,0,2,1,3,3,1,3,2,0,1,2,3,2,0,1,3,3,0,1,0,2,1,0,3};
int rw,c,sop;
rw = sip[3]+sip[0]*2;
c = sip[2]+sip[1]*2;
sop = sbox[sbno][rw][c];
for(;sop!=0;sop/=2)
p[i--]=sop%2;
}
void cmp_fun(int round)
{
int EP[]={4,1,2,3,2,3,4,1},i,epd[8];
int slip[4],srip[4];
int p[4]={0},p4[]={2,4,3,1},np[4];
for(i=0;i<8;i++)
epd[i]=r[EP[i]-1];
for(i=0;i<8;i++)
if(i<4)
slip[i] = epd[i]^keys[round][i];
else
srip[i-4] = epd[i]^keys[round][i];
sbox(slip,p,0,1);
sbox(srip,p,1,3);
for(i=0;i<4;i++)
np[i]=p[p4[i]-1];
for(i=0;i<4;i++)
l[i] = l[i]^np[i];
}
void left_shift(int keyip[],int nob)
{
int t1,t2,i;
while(nob>0)
{
t1=keyip[0],t2=keyip[5];
for(i=0;i<9;i++)
if(i<4)
keyip[i] =keyip[i+1];
else if(i>4)
keyip[i] = keyip[i+1];
keyip[4]=t1,keyip[9]=t2;
nob--;
}
}
void gen_keys()
{
int key[10],i,keyip[10];
int p10[]={3,5,2,7,4,10,1,9,8,6},p8[]={6,3,7,4,8,5,10,9};
printf("Enter Key :");
for(i=0;i<10;i++)
scanf("%d", &key[i]);
for(i=0;i<10;i++)
keyip[i] = key[p10[i]-1];
left_shift(keyip,1);
printf("\nKey1 :");
for(i=0;i<8;i++){
keys[0][i] = keyip[p8[i]-1];
printf("%d",keys[0][i]);
}
left_shift(keyip,2);
printf("\nKey2 :");
for(i=0;i<8;i++){
keys[1][i] = keyip[p8[i]-1];
printf("%d",keys[1][i]);
}
}
void En_De(int pt[],int c)
{
int ip[]={2,6,3,1,4,8,5,7},ipi[]={4,1,3,5,7,2,8,6},t[8],i;
for(i=0;i<8;i++)
if(i<4)
l[i]=pt[ip[i]-1];
else
r[i-4] = pt[ip[i]-1];
cmp_fun(c);
for(i=0;i<4;i++)
r[i]=l[i]+r[i],l[i]=r[i]-l[i],r[i]=r[i]-l[i];
printf("\n\n");
cmp_fun(!c);
for(i=0;i<8;i++)
if(i<4) t[i]=l[i];
else t[i]=r[i-4];
for(i=0;i<8;i++)
ct[i] = t[ipi[i]-1];
}
void main()
{
int pt[8]={0},i;
printf("Enter plain text binary bits:");
for(i=0;i<8;i++)
scanf("%d",&pt[i]);
gen_keys();
En_De(pt,0);
printf("\nCipher Text :");
for(i=0;i<8;i++)
printf("%d",ct[i]);
En_De(ct,1);
printf("\nPlain Text (After Decrypting):");
for(i=0;i<8;i++)
printf("%d",ct[i]);
}

Output:
3.
AIM: Implement RSA asymmetric (public key and private key) EncryptionImplement
Euclidean and Extended Euclidean algorithm for calculating the GCD.

Algorithm:

Key generation:

Step1: Selecttwoprime numbers,p,q.

Step2: Calculaten= p*q

Step3: Calculate Ф(n) =(p-1)(q-1)

Step4: SelectesuchthateisrelativelyprimetoФ(n), gcd(e, Ф(n))=1 andlessthanФ(n);

-1
Step5: Determinedsuchthatd=e modФ(n)

Encryption:

Step1: Read Plain Text, M

Step 2: Find C= M*M mod n

Step 3: Repeat Step 2 for e times

Step 4: C contains cipher


Decryption:

Step1: Read Cipher Text, C

Step 2: Find M= C*C mod n

Step 3: Repeat Step 2 for d times

Step 4: M contains plain text

Code:
#include<stdio.h>
int phi,M,n,e,d,C,FLAG;
gcd(int a,int b)
{
int temp = 0;
while(b != 0)
{
temp = a;
a = b;
b = temp % b;
}
return a;
}
int isprime(int n)
{
int i,count=1;
for(i=2;i<n;i++)
{
if(n%i==0)
count=0;
break;
}
return count;
}

int check()
{
int i;
if((e<=1)||(e>=phi))
{
FLAG = 1;
return;
}
if(gcd(e,phi)!=1)
{
FLAG = 1;
return;
}
if(isprime(e)==0)
{
FLAG = 1;
return;
}
FLAG = 0;
return;
}
void encrypt()
{
int i;
C = 1;
for(i=0;i< e;i++)
C=C*M%n;
C = C%n;
printf("\n\tEncrypted keyword : %d",C);
}
void decrypt()
{
int i;
M = 1;
for(i=0;i< d;i++)
M=M*C%n;
M = M%n;
printf("\n\tDecrypted keyword : %d",M);
}
void main()
{
int i,p,q,s;

printf("Enter Two Relatively Prime Numbers\t: ");


scanf("%d%d",&p,&q);
n = p*q;
phi=(p-1)*(q-1);
printf("\n\tF(n)\t= %d",phi);
do
{
printf("\n\nEnter e value\t: ");
scanf("%d",&e);
check();
}while(FLAG==1);
printf("GCD",gcd(e,phi));
d = 1;

for(i=1;i<phi;i++)
{
if((e*i)%phi==1)
{
d=i;
break;
}
}
printf("\n\tPublic Key\t: {%d,%d}",e,n);
printf("\n\tPrivate Key\t: {%d,%d}",d,n);
printf("\n\nEnter The Plain Text\t: ");
scanf("%d",&M);
encrypt();
decrypt();
}

Output:
Experiment-4:-
Aim:-Demonstrate the usage of Wireshark to identify abnormal activity in network Communication

Wireshark is a free application you use to capture and view the data traveling back and forth on your network. It
provides the ability to drill down and read the contents of each packet and is filtered to meet your specific needs.
It is commonly used to troubleshoot network problems and to develop and test software. This open source
protocol analyzer is widely accepted as the industry standard, winning its fair share of awards over the years.

Originally known as Ethereal, Wireshark has a user-friendly interface that can display data from hundreds of
different protocols on all major network types. Data packets can be viewed in real time or analyzed offline.
Wireshark supports dozens of capture/trace file formats supported including CAP and ERF. Integrated decryption
tools allow you to view encrypted packets for several popular protocols including WEP and WPA/WPA2..

Downloading and installing Wireshark:- Wireshark can be downloaded at no cost from the Wire
Shark Foundation Website for both macOS and Windows operating systems. Unless you are an advanced user,
it is recommended that you only download the latest stable release. During the Windows setup process, you
should choose to install WinPcap if prompted, as it includes a library required for live data capture.The
application is also available for Linux and most other UNIX-like platforms Including Red Hat, Solaris, and
FreeBSD. The binaries required for these operating systems can be found toward the bottom of the download
page in the Third-Party Pacakages section. You can also download Wireshark's source code from this page.

Wireshark_Colourised:-
Wireshark_capturing Packets:

Wireshark_tcp:
Wireshark_protocol:
Experiment-5:-

Aim:-Demonstrate usage of NMAP (Zenmap) Tool in Network Scanning.

Download the Nmap installer:-This can be found for free from the developer’s website. It is
highly recommended that you download directly from the developer to avoid any potential
viruses or fake files. Downloading the Nmap installer includes Zenmap, the graphical
interface for Nmap which makes it easy for newcomers to perform scans without having to
learn command lines.The Zenmap program is available for Windows, Linux, and Mac OS X.
You can find the installation files for all operating systems on the Nmap website.

Nmap_setup:-If you left your settings at default during installation, you should be able to
see an icon for it on your desktop. If not, look in your Start menu. Opening Zenmap will start
the program.

Nmap_target:-The active results of the scan will be displayed in the Nmap Output tab. The
time the scan takes will depend on the scan profile you chose, the physical distance to the
target, and the targets network configuration.

Nmap_read results:-. Once the scan is finished, you’ll see the message “Nmap done” at the
bottom of the Nmap Output tab. You can now check your results, depending on the type of
scan you performed. All of the results will be listed in the main Nmap Output tab, but you can
use the other tabs to get a better look at specific data.

Nmap_ports/Hosts:Ports/Hosts:- This tab will show the results of your port scan, including
the services for those ports.

Nmap_topology:-This shows the traceroute for the scan you performed. You can see how
many hops your data goes through to reach the target.
Nmap_Host Details:-This shows the traceroute for the scan you performed. You can see
how many hops your data goes through to reach the target.

Nmap_scans:- This tab stores the commands of your previously-run scans. This allows you
to quickly re-scan with a specific set of parameters

You might also like