Poly alphabetic cipher implementation in C

Practical - 4
Implement Poly alphabetic cipher encryption-decryption.



Code:
//this code accepts only characters without spaces
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<ctype.h>
void take_input(char *text)
{
int i=0;
char c;
while(1)
{
c=getch();
if(c==13)
{
text[i]='\0';
break;
}
else if(c==8)
{
putch('\b');
putch(' ');
putch('\b');
i--;
}
else if(c!=32)
{
if((c>=65 && c<=90) || (c>=97 && c<=122))
{
printf("%c",c);
text[i]=c;
i++;
}
}
}
}
void encrypt()
{
int i=0,j=0,pt_len,key_len,temp;
char c,pt[200],ct[200],key[200];
printf("Enter text to encrypt:\n");
take_input(&pt);
printf("\nEnter key to encrypt:\n");
take_input(&key);
pt_len=strlen(pt);
key_len=strlen(key);
for(i=0;i<pt_len;i++)
{
if(isupper(pt[i]))
temp=pt[i]-65;
else
temp=pt[i]-97;
if(isupper(key[j]))
temp=temp+(key[j]-65);
else
temp=temp+(key[j]-97);
ct[i]=(temp%26)+97;
j=(j+1)%key_len;
}
ct[i]='\0';
printf("\nEncrypted message:\t%s\n",ct);
}
void decrypt()
{
int i=0,j=0,ct_len,key_len,temp;
char c,pt[200],ct[200],key[200];
printf("Enter encrypted text:\n");
take_input(&ct);
printf("\nEnter key to decrypt:\n");
take_input(&key);
ct_len=strlen(ct);
key_len=strlen(key);
for(i=0;i<ct_len;i++)
{
if(isupper(ct[i]))
temp=ct[i]-65;
else
temp=ct[i]-97;
if(isupper(key[j]))
temp=temp-(key[j]-65);
else
temp=temp-(key[j]-97);
pt[i]=((temp+26)%26)+97;
j=(j+1)%key_len;
}
pt[i]='\0';
printf("\nDecrypted message:\t%s\n",pt);
}
void main()
{
int choice;
clrscr();
while(1)
{
printf("Enter your choice:\n1.Encrypt\t2.Decrypt\t3.Exit\n");
scanf("%d",&choice);
switch(choice)
{
case 1:
{
encrypt();
break;
}
case 2:
{
decrypt();
break;
}
case 3:
break;
default:
printf("Enter valid choice...\n");
}
if(choice==3)
{
break;
}
}
}

Output:

Comments

Popular posts from this blog

Study of DB Miner Tool

Study of WEKA tool

Create calculated member using arithmetic operators and member property of dimension member