This is a program for Playfair Cipher, but after the encrypt function, puts() function is not returning the encrypted text.
i would be glad if someone helped me out.
The code is as follows:
Code:
#include<stdio.h>
#include<conio.h>
#include<string.h>
#define MAX 100
void encrypt(char square[5][5],char ptext[MAX],char ctext[MAX]);
void fillsquare(char key[MAX/4],char square[5][5]);
int clear_input_buffer(void);
void main()
{
char key[MAX/4],square[5][5],ptext[MAX],ctext[MAX];
int ch,i,j;
clrscr();
do
{
printf("\nPlayfair Cipher\n");
printf("---------------\n");
printf("1. Encryption\n");
printf("2. Decryption\n");
printf("\nEnter your choice: ");
scanf("%d",&ch);
printf("Enter the key: ");
clear_input_buffer();
gets(key);
fillsquare(key,square);
//for(i=0;i<5;i++) for(j=0;j<5;j++) printf("%c",square[i][j]);
switch(ch)
{
case 1:
printf("\nEnter the plaintext to be encrypted: \n");
clear_input_buffer();
gets(ptext);
printf("\nstart"); puts(ptext); printf("end");
encrypt(square,ptext,ctext);
printf("\nCiphertext: \n");
break;
case 2:
break;
default:
break;
}
}
while(ch=='y' || ch=='Y');
getch();
}
int clear_input_buffer(void)
{
int ch;
while(((ch=getchar())!=EOF) && (ch != '\n'));
return ch;
}
void fillsquare(char key[MAX/4],char square[5][5])
{
int i,j,l=strlen(key),y=0,x,count;
char alpha=97;
for(i=0;i<5;i++)
{
for(j=0;j<5;j++)
{
if(y<l)
{
square[i][j]=key[y++];
//printf("%c",square[i][j]);
}
else
{
count=1;
while(count==1)
{
x=0,count=0;
while(key[x]!='\0')
{
if(key[x++]==alpha)
{
count++;
alpha++;
}
}
}
square[i][j]=alpha++;
}
}
}
}
void encrypt(char square[5][5],char ptext[MAX],char ctext[MAX])
{
int x=0,y=0,i,j,al1row,al1col,al2row,al2col;
printf("\nout of while");
while(ptext[x]!='\0')
{
printf("\nwhile");
if(ptext[x]==ptext[x+1])
{
}
else
{
for(i=0;i<5;i++)
{
for(j=0;j<5;j++)
{
if(ptext[x]==square[i][j])
{
al1row=i;
al1col=j;
}
if(ptext[x+1]==square[i][j])
{
al2row=i;
al2col=j;
}
}
}
printf("\nal1row: %d al1col: %d al2row: %d al2col: %d",al1row,al1col,al2row,al2col);
if(al1row==al2row)
{
ctext[y++]=square[al1row][al1col+1];
ctext[y++]=square[al2row][al2col+1];
}
if(al1col==al2col)
{
ctext[y++]=square[al1row+1][al1col];
ctext[y++]=square[al2row+1][al2col];
}
if(al1row != al2row && al1col!=al2col)
{
ctext[y++]=square[al2row][al1col];
ctext[y++]=square[al1row][al2col];
}
x+=2;
}
ctext[y]='\0';
}
}