Do you think IIT Guwahati certified course can help you in your career?
No
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.
What is the 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.
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.
The formula of encryption is:
En (x) =(x + n) mod 26
The formula of decryption is:
Dn(x) = (x - n) mod 26
Where,
En depicts encryption
Dn depicts decryption
x denotes the value of the letter
n denotes the shift or key-value
If any case (Dn) value becomes negative (-ve), in this case, we will add 26 in the negative value.
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.
Plaintext: N → 13
En: (13 + 3) mod 26
Ciphertext: 16 → Q
Plaintext: I → 8
En: (8 + 3) mod 26
Ciphertext: 11 → L
Plaintext: J → 9
En: (9 + 3) mod 26
Ciphertext: 12 → M
Plaintext: A → 0
En: (0 + 3) mod 26
Ciphertext: 3 → D
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.
Ciphertext: 16 → Q
Dn: (16 - 3) mod 26
Plaintext: N → 13
Ciphertext: 11 → L
Dn: (11 - 3) mod 26
Plaintext: I → 8
Ciphertext: 12 → M
Dn: (12 - 3) mod 26
Plaintext: J → 9
Ciphertext: 3 → D
Dn: (3 - 3) mod 26
Plaintext: A → 0
Caesar Cipher Cryptography Algorithm
The Caesar Cipher is a simple and widely known encryption technique that shifts each letter in a text by a fixed number of positions in the alphabet.
Choose a Shift Key: Decide on a fixed shift value (e.g., 3). This key determines how much each letter will be shifted.
Convert Each Letter: For encryption, shift each letter forward by the key value. For decryption, shift each letter backward by the key value.
Handle Wrap-around: If shifting a letter goes past 'Z' (uppercase) or 'z' (lowercase), wrap around to the beginning of the alphabet.
Ignore Non-Alphabet Characters: Numbers, spaces, and special characters remain unchanged.
Construct the Encrypted/Decrypted Message: Combine the shifted characters to get the final result.
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.
Input to be taken from the user:
First-line contains the string called text
Second-line contains the Key, it is an integer ranging from 0-25
Rules for the Caesar Cipher
Fixed Shift Value: Each letter in the plaintext is shifted by a fixed number of positions in the alphabet.
Alphabet Wrap-around: If shifting goes past 'Z' or 'z', it wraps around to the start of the alphabet.
Case Sensitivity: Uppercase and lowercase letters are treated separately and shifted accordingly.
Non-Alphabet Characters: Numbers, spaces, and special characters remain unchanged.
Reversibility: Decryption is done by shifting in the opposite direction using the same key.
Key Dependency: The same shift key must be used for both encryption and decryption.
Security Weakness: Since it has only 25 possible shifts, it is vulnerable to brute-force attacks.
Algorithm for Caesar Cipher
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.
Code for Encryption
C
C++
Java
Python
JS
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);
string caesarCipher(string text, int key) { for (int i = 0; i < text.length(); i++) { char 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; } } return text; }
int main() { string text; int key;
cout << "Enter a plaintext to encrypt:\n"; getline(cin, text);
public class CaesarCipher { public static String encrypt(String text, int key) { StringBuilder result = new StringBuilder(); for (char c : text.toCharArray()) { if (Character.isLowerCase(c)) { c = (char) (c + key); if (c > 'z') c = (char) (c - 'z' + 'a' - 1); } else if (Character.isUpperCase(c)) { c = (char) (c + key); if (c > 'Z') c = (char) (c - 'Z' + 'A' - 1); } result.append(c); } return result.toString(); }
public static void main(String[] args) { Scanner scanner = new Scanner(System.in);
System.out.println("Enter a plaintext to encrypt:"); String text = scanner.nextLine();
System.out.println("Enter key:"); int key = scanner.nextInt();
def caesar_cipher(text, key): result = "" for c in text: if 'a' <= c <= 'z': c = chr(((ord(c) - ord('a') + key) % 26) + ord('a')) elif 'A' <= c <= 'Z': c = chr(((ord(c) - ord('A') + key) % 26) + ord('A')) result += c return result
# Taking input text = input("Enter a plaintext to encrypt:\n") key = int(input("Enter key:\n"))
function caesarCipher(text, key) { let result = ""; for (let i = 0; i < text.length; i++) { let c = text[i]; if (c >= 'a' && c <= 'z') { c = String.fromCharCode(((c.charCodeAt(0) - 97 + key) % 26) + 97); } else if (c >= 'A' && c <= 'Z') { c = String.fromCharCode(((c.charCodeAt(0) - 65 + key) % 26) + 65); } result += c; } return result; }
// Taking input const text = prompt("Enter a plaintext to encrypt:"); const key = parseInt(prompt("Enter key:"), 10);
// 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);
string caesarDecrypt(string text, int key) { for (int i = 0; i < text.length(); i++) { char 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; } } return text; }
public class CaesarDecryption { public static String decrypt(String text, int key) { StringBuilder result = new StringBuilder(); for (char c : text.toCharArray()) { if (Character.isLowerCase(c)) { c = (char) (c - key); if (c < 'a') c = (char) (c + 'z' - 'a' + 1); } else if (Character.isUpperCase(c)) { c = (char) (c - key); if (c < 'A') c = (char) (c + 'Z' - 'A' + 1); } result.append(c); } return result.toString(); }
public static void main(String[] args) { Scanner scanner = new Scanner(System.in);
System.out.println("Enter encrypted text:"); String text = scanner.nextLine();
System.out.println("Enter key:"); int key = scanner.nextInt();
def caesar_decrypt(text, key): result = "" for c in text: if 'a' <= c <= 'z': c = chr(((ord(c) - ord('a') - key) % 26) + ord('a')) elif 'A' <= c <= 'Z': c = chr(((ord(c) - ord('A') - key) % 26) + ord('A')) result += c return result
Simple Substitution Cipher – Each letter in the plaintext is shifted by a fixed number of positions in the alphabet.
Fixed Key Shift – A numerical key determines how many positions each letter shifts. Common shifts include 3 (e.g., A → D, B → E).
Preserves Letter Frequency – Since it only shifts letters without altering their frequency, it is vulnerable to frequency analysis attacks.
Only Affects Alphabetic Characters – Numbers, symbols, and spaces remain unchanged unless explicitly modified.
Easily Breakable – Since there are only 25 possible shifts (excluding the identity shift), it can be decrypted using brute force or pattern recognition.
Used as a Basis for Modern Cryptography – Though insecure today, it helps in understanding cryptographic concepts like shift ciphers and modular arithmetic.
Advantages Of Caeser Cipher
Caesar cipher has the following advantages.
It is simple and very easy to implement.
Only one short key is used in the entire process(the same key is used for encryption and decryption).
If the system does not use complex coding techniques, it is the method and easy too.
Only a few computing resources are required.
Disadvantages of Caesar Cipher
The entire message can be decrypted by looking at the pattern of letters or trying brute force.
It is less secure.
It can easily be hacked since messages encrypted by this method can easily be decrypted.
Frequently Asked Questions
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.
How do I decode a Caesar cipher?
To decode a Caesar cipher, shift each letter backward by the key value. For example, with a shift of 3, 'D' becomes 'A'.
What is the shift of 3 in the Caesar cipher?
A shift of 3 means each letter moves three places forward in the alphabet. For example, 'A' → 'D', 'B' → 'E', 'C' → 'F'.
What is the 13-letter Caesar cipher?
A 13-letter Caesar cipher (ROT13) shifts letters by 13 places. Since 13 is half of 26, applying ROT13 twice restores the original text.
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++.