0% found this document useful (0 votes)
60 views4 pages

Practicle:-3: AIM:-Implement Playfair Cipher Encryption - Decryption

This document describes implementing the Playfair cipher for encryption and decryption. It includes: 1) Defining a playfair() function that takes two characters and a key matrix to encrypt them. 2) A main() function that gets a key and plaintext from the user, constructs a 5x5 key matrix, and calls playfair() to encrypt pairs of characters and output the cipher text. 3) It handles 'j'/J by replacing with 'i'/I, encrypts single characters with 'X', and maintains the pairs by adding 'X' if odd length plaintext.

Uploaded by

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

Practicle:-3: AIM:-Implement Playfair Cipher Encryption - Decryption

This document describes implementing the Playfair cipher for encryption and decryption. It includes: 1) Defining a playfair() function that takes two characters and a key matrix to encrypt them. 2) A main() function that gets a key and plaintext from the user, constructs a 5x5 key matrix, and calls playfair() to encrypt pairs of characters and output the cipher text. 3) It handles 'j'/J by replacing with 'i'/I, encrypts single characters with 'X', and maintains the pairs by adding 'X' if odd length plaintext.

Uploaded by

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

Practicle:-3

 AIM:- Implement Playfair cipher encryption-


decryption.
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<ctype.h>
#define MX 5
int 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;
}
}
}
//printf("%d%d %d%d",w,x,y,z);
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
{

Name:-Ridham Vyas Enrollment No:-170630116035


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,l,m=0,n;
char key[MX][MX],keyminus[25],keystr[10],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);
printf("\nEnter the plain text:");
gets(str);
n=strlen(keystr);
//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]);

//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;
Practicle:-3
}
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");
}

// 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++;
}
}
}
getch();
}

Name:-Ridham Vyas Enrollment No:-170630116035


 OUTPUT

You might also like