Caesar Cipher Program in C

What is Caesar Cipher?

Caesar Cipher is a Symmetric Cryptography model, with the term Symmetric, I mean that that key which is used for both encryption and decryption are same. This is the earliest model which is used by Julius Caesar. It involves replacement of the current character with the third places further down.It uses Substitution Techniques. A substitution technique is one in which the letters of plaintext are replaced by other letters or by numbers or symbols.

Example: If I have to encrypt ‘a’ in Caesar Cipher I would replace it with 3rd character starting from a which is ‘d’. It goes in circular manner such that if we take a look at characters ‘x’, ‘y’ and ‘z’, they will be replaced with next shifting ‘a’,’b’ and ‘c’ respectively.

The algorithm for this Cipher can be expressed in

C = E(3, p) = (p + 3) mod 26 

where C denotes the Cipher text and p denotes the plain text.

Here is C Program for the following Caesar cipher. It is an Encrypting Program.

#include<stdio.h>
#include<string.h>
main(){
int y,i,max,min;
char pt[20],ct[20],c;
printf("Enter the Message you want to encrypt:");
gets(pt);
y=strlen(pt);
for(i=0;i<=y;i++){
c=pt[i];
ct[i]=c+3;
if(c==122)
{
ct[i]=97+2;
}
if(c==120||c==121)
{
min=c;
c=97;
max=122-min;
c=c+(max%2);
ct[i]=c;
}
}
printf("Caesar Ciper is : %s",ct);
return 0;
}

 

OUTPUT:

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.