This is a code for Caesar cipher encryption and decryption using C programming language.
Caesar cipher encryption:-
Source code:-
#include<conio.h>
#include<stdio.h>
int main()
{
int key,i;
char msg[100],ch;
printf("enter string to be encrypt");
scanf("%s",&msg);
printf("enter key");
scanf("%d",&key);
for(i=0;msg[i]!='\0';i++)
{
ch=msg[i];
if(ch>='a'&&ch<='z')
{
ch=ch+key;
if(ch>'z')
{
ch=ch-'z'+'a'-1;
}
msg[i]=ch;
}
else if (ch>='A'&&ch<'Z'){
ch=ch+key;
if(ch>'Z')
{
ch=ch-'Z'+'A'-1;
}
msg[i]=ch;
}
}
printf("encrypted msg:%s",msg);
return 0;
}
Output:-
Caesar cipher decryption:-
Source code :-
#include<conio.h>
#include<stdio.h>
int main()
{
int key,i;
char msg[100],ch;
printf("enter string to be encrypt");
scanf("%s",&msg);
printf("enter key");
scanf("%d",&key);
for(i=0;msg[i]!='\0';i++)
{
ch=msg[i];
if(ch>='a'&&ch<='z')
{
ch=ch-key;
if(ch>'z')
{
ch=ch-'z'+'a'-1;
}
msg[i]=ch;
}
else if (ch>='A'&&ch<'Z')
{
ch=ch-key;
if(ch>'Z')
{
ch=ch-'Z'+'A'-1;
}
msg[i]=ch;
}
}
printf("encrypted msg:%s",msg);
return 0;
}
Output:-