1.
Introduction
2.
What is Caesar Cipher?
3.
Program for Caeser Cipher in C
3.1.
Caesar Cipher Encryption
3.2.
Caesar Cipher Decryption
4.
Implementation for Caesar Cipher
4.1.
Algorithm
4.2.
Code for Encryption
4.3.
C
4.4.
Code for Decryption
4.5.
C
5.
6.
7.
7.1.
How do you program a Caesar cipher?
7.2.
Why is the Caesar cipher not a strong encryption method?
7.3.
What is the logic of the Caesar cipher in C?
8.
Conclusion
Last Updated: Mar 27, 2024
Medium

# Caesar Cipher Program in C

Apoorv Dixit
0 upvote
Master Python: Predicting weather forecasts
Speaker
Ashwin Goyal
Product Manager @

## Introduction

The 'Caesar Cipher' is one of the oldest cryptography methods, named after Julius Caesar. It was developed around 100 BC and used by Julius Caesar to send secret messages to his generals in the field. If his messages got intercepted during the event, his opponent could not read them. This obviously gave him a tremendous strategic advantage. So, what was the code?

A basic and proven approach for converting data into secret code is the Caesar Cipher program in C. It offers a way to both encrypt and decode the given data. It seasonally switches the current character to a few characters. Since it is basic, it is simple to use and, hence, simple to crack.

In this article, we will discuss what caesar cipher is and how to encode and decode a message.

## What is Caesar Cipher?

Caeser shifted each letter of his message three positions right( change to 3rd next alphabet) to produce what could be called ciphertext. If the enemy somehow got the message, he will see the ciphertext instead of the true message. For example, in Caeser's message letter 'A' would become 'D,' letter 'B' would become 'E,' and the letter 'Y' becomes 'B.' Caser used the key as 3. However, a key can be altered while generating a cipher, and the same key must be used to decrypt it.

An image showing decryption of letter choosing key as 3 is shown below.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

## Program for Caeser Cipher in C

In technical terms, the Caeser cipher, also termed a shift cipher, is an encryption technique based on the monoalphabetic cipher. The table for encryption and decryption by Caeser cipher is explained in the table below.

Time for an example:

Suppose you have to encrypt and decrypt the word NINJA.

The above table depicts the value x for each letter.

### Caesar Cipher Encryption

You will traverse word by word, and for each letter, you will find its corresponding cipher letter. The letter N will become Q  and similarly, all letters will transform into their respective letters and the NINJA will become QLQMD. Use formula En (x) =(x + n) mod 26, here n(key)=3.

Now let's see how to decrypt.

### Caesar Cipher Decryption

Similarly, QLQMD can be decrypted to NINJA again using the formula Dn(x) = (xi - n) mod 26.

## Implementation for Caesar Cipher

You can easily generate Caesar cipher for the given text; you can also choose a key and encrypt and decrypt your message accordingly. Now let's see the algorithm and C++ program for encryption and decryption of messages.

### Algorithm

• Traverse the given text one character at a time.
• For every character in the text, transform it as per the rule( given key). Consider whether the text is encrypted or decrypted.
• Return the newly transformed string.

• C

### C

``#include <stdio.h>#include <string.h>int main() {    // Declaring variables    int i, key;    char s[1000], c;    // Taking Input    printf("Enter a plaintext to encrypt:\n");    fgets(s, sizeof(s), stdin);    printf("Enter key:\n");    scanf("%d", &key);    int n = strlen(s);    // Encrypting each character according to the given key    for (i = 0; i < n; i++) {        c = s[i];        if (c >= 'a' && c <= 'z') {            c = c + key;            if (c > 'z') {                c = c - 'z' + 'a' - 1;            }            s[i] = c;        } else if (c >= 'A' && c <= 'Z') {            c = c + key;            if (c > 'Z') {                c = c - 'Z' + 'A' - 1;            }            s[i] = c;        }    }    // Output the cipher    printf("Encrypted message: %s\n", s);    return 0;}``

OUTPUT:

• C

### C

``#include <stdio.h>#include <string.h>int main() {    // Declaring variables    int i, key;    char s[1000], c;    // Taking ciphertext and key input    printf("Enter encrypted text:\n");    fgets(s, sizeof(s), stdin);    printf("Enter key:\n");    scanf("%d", &key);    int n = strlen(s);    // Decrypting each character according to the given key    for (i = 0; i < n; i++) {        c = s[i];        if (c >= 'a' && c <= 'z') {            c = c - key;            if (c < 'a') {                c = c + 'z' - 'a' + 1;            }            s[i] = c;        } else if (c >= 'A' && c <= 'Z') {            c = c - key;            if (c < 'A') {                c = c + 'Z' - 'A' + 1;            }            s[i] = c;        }    }    // Output the original message    printf("Decrypted message: %s\n", s);    return 0;}``

OUTPUT:

Caesar cipher has the following advantages.

1. It is simple and very easy to implement.
2. Only one short key is used in the entire process(the same key is used for encryption and decryption).
3. If the system does not use complex coding techniques, it is the method and easy too.
4. Only a few computing resources are required.

1. The entire message can be decrypted by looking at the pattern of letters or trying brute force.
2. It is less secure.
3. It can easily be hacked since messages encrypted by this method can easily be decrypted.

### How do you program a Caesar cipher?

The first step is to traverse the given text one character at a time. Then for every character in the text, transform it as per the rule( given key). Consider whether the text is encrypted or decrypted. Finally, Return the newly transformed string.

### Why is the Caesar cipher not a strong encryption method?

The Caesar cipher is not a strong encryption method due to its simplicity and vulnerability to brute-force attacks. It has only 26 possible keys, making it easily breakable by trying all possible shifts. It does not provide sufficient security for modern encryption requirements.

### What is the logic of the Caesar cipher in C?

The logic of the Caesar cipher in C involves shifting each character in the plaintext message by a fixed number of positions (key) to obtain the corresponding character in the ciphertext.

## Conclusion

In this article, we discussed what Caeser cipher is. We discussed how a message is encrypted and decrypted using Caesar cipher. We also saw its implementation using code in C++

We hope that this blog has helped you enhance your knowledge regarding Caesar Cipher and if you would like to learn more, check out our articles, What is CryptographyRSA algorithm, and Difference between Public Key and Private Key. Do upvote our blog to help other ninjas grow.

Happy Learning!

Live masterclass