Implement Mono alphabetic cipher encryption-decryption

Practical - 2
Implement Mono alphabetic cipher encryption-decryption.



Code:
//It Accepts alphabets only
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<ctype.h>
char keyl[26] = {'k', 'd', 'j', 'i', 'l', 'w', 'y', 'o', 'r', 't', 'a', 'c', 'e', 'h', 'b', 'u', 'x', 'v', 'p', 'z', 'n', 'q', 'f', 's', 'm', 'g' };
char keyu[26] = {'K', 'D', 'J', 'I', 'L', 'W', 'Y', 'O', 'R', 'T', 'A', 'C', 'E', 'H', 'B', 'U', 'X', 'V', 'P', 'Z', 'N', 'Q', 'F', 'S', 'M', 'G' };
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++;
}
}
}
}
int position(char c)
{
int i;
for(i=0;i<26;i++)
{
if(c==keyl[i] || c==keyu[i])
{
return i;
}
}
}
void encrypt()
{
int i=0,len;
char ct[200],pt[200],temp;//ct=cipher text,pt=plain text
printf("Enter message to encrypt:\n");
take_input(&pt);
len=strlen(pt);
while(i<len)
{
temp=pt[i];
if(isupper(temp))
ct[i]=keyu[pt[i]-65];
else
ct[i]=keyl[pt[i]-97];
i++;
}
ct[i]='\0';
printf("\nEncrypted Message:\t%s\n",ct);
}
void decrypt()
{
int i=0,len;
char temp,ct[200],pt[200];
printf("Enter message to decrypt:\n");
take_input(&ct);
len=strlen(ct);
while(i<len)
{
temp=ct[i];
if(isupper(temp))
pt[i]=position(temp)+65;
else
pt[i]=position(temp)+97;
i++;
}
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 a valid choice\n");
}
}
if(choice==3)
break;
}
}
Output:



Comments

Post a Comment

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