Implement Polyalphabetic cipher encryption-decryption.

C Source file of program


#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<math.h>

char* doEncryption(char* pt,char* key)
{
int m=0,c=0,i;
int length=strlen(pt);
for(i=0;i<length;i++)
{
m=(int)pt[i]-97;
c=(int)key[i]-97;
m=((m+c)%26)+97;
pt[i]=(char)m;
}
pt[i]='\0';

return pt;
}

char* doDecryption(char* pt,char* key)
{
int m=0,c=0,i;

int length=strlen(pt);
printf("\n");
for(i=0;i<length;i++)
{
m=(int)pt[i]-97;
c=(int)key[i]-97;
   
m=m-c;
if(m<0)
m=((m+26)%26)+97;
else
m=(m%26)+97;

pt[i]=(char)m;
}
pt[i]='\0';

return pt;
}


void main()
{
char *pt;
char *key;
char ch;
char *ct;
int length_of_pt=0;
int length_of_key=0;
int i=0;

clrscr();

printf("Enter the plaintext\n");
gets(pt);
strlwr(pt);
length_of_pt=strlen(pt);

printf("Enter your key\n");
scanf("%s",key);
length_of_key=strlen(key);

for(i=length_of_key;i<length_of_pt;i++)
{
key[i]=key[i-length_of_key];
}
key[i]='\0';

       ct=doEncryption(pt,key);
       printf("Encrypted string=%s\n\n\n",ct);
     strlwr(ct);
length_of_pt=strlen(ct);
printf("%s",ct);

pt=doDecryption(ct,key);
printf("Decrypted string=%s\n",pt);
getch();
}

Output :