Program For Playfair Cipher In C

Information Security‎ > ‎

Write a C program for Play fair cipher.

posted Jan 26, 2012, 12:22 AM by Devharsh Trivedi
#include <stdio.h>
#define siz 5
void encrypt(int *i, int *j)
{
(*i)++,(*j)++;
if((*i)siz) *i=0;
else if((*j)siz) *j=0;
}
void playfair(char ch1,char ch2, char mat[siz][siz])
{
int j,m,n,p,q,c,k;
for(j=0,c=0;(c<2)||(j<siz);j++)
for(k=0;k<siz;k++)
if(mat[j][k] ch1)
m=j,n=k,c++;
else if(mat[j][k] ch2)
p=j,q=k,c++;
if(mp)
encrypt(&n,&q);
else if(nq)
encrypt(&m,&p);
else
n+=q,q=n-q,n-=q;
printf('%c%c',mat[m][n],mat[p][q]);
}
void main()
{
char mat[siz][siz],key[10],str[25]={0};
int m,n,i,j;
char temp;
printf('Enter Key:');
gets(key);
m=n=0;
for(i=0;key[i]!='0';i++)
{
for(j=0;j<i;j++)
if(key[j] key[i]) break;
if(key[i]'j') key[i]='i';
if(j>=i)
{
mat[m][n++] = key[i];
if(nsiz)
n=0,m++;
}
}
for(i=97;i<=122;i++)
{
for(j=0;key[j]!='0';j++)
if(key[j] i)
break;
else if(i'j')
break;
if(key[j]'0')
{
mat[m][n++] = i;
if(nsiz) n=0,m++;
}
}
printf('Enter input String:');
gets(str);
printf('nnMatrix :n');
for(i=0;i<siz;i++)
{
for(j=0;j<siz;j++)
printf('%ct',mat[i][j]);
printf('n');
}
printf('nnEntered text :%snCipher Text :',str);
for(i=0;str[i]!='0';i++)
{
temp = str[i++];
if(temp 'j') temp='i';
if(str[i]'0')
playfair(temp,'x',mat);
else
{
if(str[i]'j') str[i]='i';
if(temp str[i])
{
playfair(temp,'x',mat);
playfair('x',str[i],mat);
}
else
playfair(temp,str[i],mat);
}
}
}
Playfair

Playfair Cipher Program In Cpp

C program to implement Lexical Analyzer #include #include #include void removeduplic. C code to implement RSA Algorithm(Encryption and Decryption) C program to implement RSA algorithm. The given program will Encrypt and Decrypt a message using RSA Algorithm. For the implementation of the Playfair encryption I needed a custom struct called Cell. This is because I not only need an array of characters I also want to get Elements in a matrix based on their '. CipherCellIndexB = matrix.FindIndex(c => c.X == b.X% 5 && c.Y == a.Y); } cipher.Append(matrix[cipherCellIndexA].Character); cipher.Append. This key means that any ‘a’ in the plaintext will be replaced by a ‘z’ in the cipher-text, any ‘z’ in the plaintext will be replaced by a ‘i’ in the cipher-text, and so on. The following program shows the simple implementation of mono-alphabetic cipher technique in c language for encrypting and decrypting file. Playfair encryption is not cryptographically safe. You would be able to find tutorials for modern, complex algorithms which are safe. However, you're learning C++, so a simple algorithm is indeed a better exercise.

Playfair Cipher Program In C Geeksforgeeks

Program

Program For Playfair Cipher In Python

Playfair cipher software

Playfair Cipher Explanation

#include<stdio.h>
#include<conio.h>
int check(char table[5][5],char k)
{
int i,j;
for(i=0;i<5;++i)
for(j=0;j<5;++j)
{
if(table[i][j]k)
return 0;
}
return 1;
}

void main()
{
int i,j,key_len;
char table[5][5];
for(i=0;i<5;++i)
for(j=0;j<5;++j)
table[i][j]='0';

printf('**********Playfair Cipher************nn');

printf('Enter the length of the Key. ');
scanf('%d',&key_len);

char key[key_len];

printf('Enter the Key. ');
for(i=-1;i<key_len;++i)
{
scanf('%c',&key[i]);
if(key[i]'j')
key[i]='i';
}

int flag;
int count=0;
for(i=0;i<5;++i)
{
for(j=0;j<5;++j)
{
flag=0;
while(flag!=1)
{
if(count>key_len)
goto l1;

flag=check(table,key[count]);
++count;
}
table[i][j]=key[(count-1)];
}
}

l1:printf('n');

int val=97;
for(i=0;i<5;++i)
{
for(j=0;j<5;++j)
{
if(table[i][j]>=97 && table[i][j]<=123)
{}
else
{
flag=0;
while(flag!=1)
{
if('j'(char)val)
++val;
flag=check(table,(char)val);
++val;
}
table[i][j]=(char)(val-1);
}
}
}

printf('The table is as follows:n');
for(i=0;i<5;++i)
{
for(j=0;j<5;++j)
{
printf('%c ',table[i][j]);
}
printf('n');
}

int l=0;
printf('nEnter the length length of plain text.(without spaces) ');
scanf('%d',&l);

printf('nEnter the Plain text. ');
char p[l];
for(i=-1;i<l;++i)
{
scanf('%c',&p[i]);
}

for(i=-1;i<l;++i)
{
if(p[i]'j')
p[i]='i';
}

printf('nThe replaced text(j with i)');
for(i=-1;i<l;++i)
printf('%c ',p[i]);

count=0;
for(i=-1;i<l;++i)
{
if(p[i]p[i+1])
count=count+1;
}

printf('nThe cipher has to enter %d bogus char.It is either 'x' or 'z'n',count);

int length=0;
if((l+count)%2!=0)
length=(l+count+1);
else
length=(l+count);


printf('nValue of length is %d.n',length);
char p1[length];
char temp1;
int count1=0;
for(i=-1;i<l;++i)
{
p1[count1]=p[i];
if(p[i]p[i+1])
{
count1=count1+1;
if(p[i]'x')
p1[count1]='z';
else
p1[count1]='x';
}
count1=count1+1;
}

char bogus;
if((l+count)%2!=0)
{
if(p1[length-1]'x')
p1[length]='z';
else
p1[length]='x';
}

printf('The final text is:');
for(i=0;i<=length;++i)
printf('%c ',p1[i]);

char cipher_text[length];
int r1,r2,c1,c2;
int k1;

for(k1=1;k1<=length;++k1)
{
for(i=0;i<5;++i)
{
for(j=0;j<5;++j)
{
if(table[i][j]p1[k1])
{
r1=i;
c1=j;
}
else
if(table[i][j]p1[k1+1])
{
r2=i;
c2=j;
}
}
}

if(r1r2)
{
cipher_text[k1]=table[r1][(c1+1)%5];
cipher_text[k1+1]=table[r1][(c2+1)%5];
}

else
if(c1c2)
{
cipher_text[k1]=table[(r1+1)%5][c1];
cipher_text[k1+1]=table[(r2+1)%5][c1];
}
else
{
cipher_text[k1]=table[r1][c2];
cipher_text[k1+1]=table[r2][c1];
}

k1=k1+1;
}

printf('nnThe Cipher text is:n ');
for(i=1;i<=length;++i)
printf('%c ',cipher_text[i]);
getch();
}