Aim :- Implement Caesar Cipher Encryption-Decryption..
It is an encryption & Decryption technique which is used for ecrypting and decrypting any message by replacing each character by another character that will be some fixed number of positions down to it.
Example, if key is 5 then we have to replace character by another character that is 5 position down to it. Like A will be replaced by F, B will be replaced by G and so on.
C Program Code :
Encryption :
#include<stdio.h>
#include<iostream.h>
void main()
{
int i, key;
char text[100],c;
clrscr();
printf("\nC Program to Perform Caesar Cipher (Encryption)");
printf("\nEnter Message : ");
gets(text);
printf("\nEnter Key : ");
scanf("%d", &key);
for(i=0;text[i]!='\0';++i)
{
c=text[i];
if(c>='a'&&c<= 'z')
{
c=c+key;
if(c>'z')
{
c=c-'z'+'a'-1;
}
text[i]=c;
}
else if(c>='A'&&c<='Z')
{
c=c+key;
if(c>'Z')
{
c=c-'Z'+'A'-1;
}
text[i]=c;
}
}
printf("\nEncrypted Text : %s", text);
getch();
}
Output :
Decryption :
#include<stdio.h>
#include<iostream.h>
void main()
{
int i, key;
char text[100],c;
clrscr();
printf("\nC Program to Perform Caesar Cipher (Decryption)");
printf("\nEnter Message : ");
gets(text);
printf("\nEnter Key : ");
scanf("%d", &key);
for(i = 0; text[i] != '\0'; ++i)
{
c = text[i];
if(c >= 'a' && c <= 'z')
{
c = c - key;
if(c < 'a')
{
c = c + 'z' - 'a' + 1;
}
text[i] = c;
}
else if(c >= 'A' && c <= 'Z')
{
c = c - key;
if(c < 'A')
{
c = c + 'Z' - 'A' + 1;
}
text[i] = c;
}
}
printf("Decrypted text: %s", text);
getch();
}
Output :
0 Comments