FreBlogg

C Program for decrypting Caesar's Cipher

06:17 Posted by Durga swaroop Perla
Caesar's cipher is one of the most popular encryption techniques in Cryptography. It is also referred to as Caesar's shift cipher or simply Shifting cipher.  It is said to be widely used in private communications of Julius Caesar. The encryption of the plain text is done by shifting the letters by a previously agreed number. So, after a shift of 2, 'a' becomes 'c', 'b' becomes 'd' and so on..

This article will show you a program that can be used to decrypt the cipher text encrypted using this method. To decrypt, you need to know the key which is the shift used to encrypt. Using the key we'll just do the reverse of encryption to get the Plain text. And, we'll program all that.

For example, the following is a decryption with a left shift 3.
Ciphertext: QEB NRFZH YOLTK CLU GRJMP LSBO QEB IXWV ALD
Plaintext:  THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG
The following is the function implemented in C Programming for the decrytpion.

void caeser(char *text,int key) // takes the cipher text and key as input
{
int len = strlen(text);     // calculates length
int i=0;
if(key>26)          // if key is more than 26, it is same key%26
key = key%26;

for(i=0;i<len;i++)
if(text[i]==' ')
continue;
else
{
if(text[i]<'a'+key)
text[i]=text[i]+26;
text[i]=text[i]-key;  // subtracting the shift
}
}
Sample Output:

So, That is all for this article. If you like this article you might like our other articles too about Programming and Cryptography. So, check them out. Also stay tuned for a lot of other interesting articles.
For more articles on  C Programming or general programming concepts, click here
As, always, Have a happy reading and Stay Awesome !

-------------------------------------------------------------------------------------------------
Follow our blog posts @ Follow. So that you won't miss any interesting post and also to be the first to know the answers to many interesting questions.