Ceaser Cipher Method Implementation in C

Practical - 1
Write a C program to implement Ceaser Cipher method for encryption and decryption.



Code:
//It Accepts alphabets only
#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++;
}
}
}
}
int key;
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);
printf("\nEnter key for encryption:\t");
scanf("%d",&key);
len=strlen(pt);
while(i<len)
{
temp=pt[i];
if(isupper(temp))
ct[i]=(((temp-65)+key)%26)+97;
else
ct[i]=(((temp-97)+key)%26)+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);
printf("\nEnter key for decryption:\t");
scanf("%d",&key);
len=strlen(ct);
while(i<len)
{
temp=ct[i];
if(isupper(temp))
pt[i]=(((temp-65)-key)%26)+97;
else
pt[i]=(((temp-97)-key)%26)+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

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