Table of contents
1.
Introduction
2.
What is the Caesar Cipher?
3.
Program for Caeser Cipher in C
3.1.
Caesar Cipher Encryption
3.2.
Caesar Cipher Decryption
4.
Caesar Cipher Cryptography Algorithm
5.
Implementation for Caesar Cipher
6.
Rules for the Caesar Cipher
7.
Algorithm for Caesar Cipher
7.1.
Code for Encryption
7.2.
C
7.3.
C++
7.4.
Java
7.5.
Python
7.6.
JS
7.7.
C#
7.8.
Code for Decryption
7.9.
C
7.10.
C++
7.11.
Java
7.12.
Python
7.13.
JS
7.14.
C#
8.
Characteristics of Caesar Cipher
9.
Advantages Of Caeser Cipher
10.
Disadvantages of Caesar Cipher 
11.
Frequently Asked Questions
11.1.
What is the logic of the Caesar cipher in C?
11.2.
How do I decode a Caesar cipher?
11.3.
What is the shift of 3 in the Caesar cipher?
11.4.
What is the 13-letter Caesar cipher?
12.
Conclusion
Last Updated: Feb 5, 2025
Medium

Introduction to Caesar Cipher

Author Apoorv Dixit
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

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?                       

caesar cipher program in c

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.

Caesar Cipher

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.

Program for Caeser Cipher in C

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.

  1. Choose a Shift Key: Decide on a fixed shift value (e.g., 3). This key determines how much each letter will be shifted.
  2. Convert Each Letter: 
    For encryption, shift each letter forward by the key value. 
    For decryption, shift each letter backward by the key value.
  3. Handle Wrap-around: If shifting a letter goes past 'Z' (uppercase) or 'z' (lowercase), wrap around to the beginning of the alphabet.
  4. Ignore Non-Alphabet Characters: Numbers, spaces, and special characters remain unchanged.
  5. 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);

return 0;
}
You can also try this code with Online C Compiler
Run Code

C++

#include <iostream>
#include <string>

using namespace std;

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);

cout << "Enter key:\n";
cin >> key;

cout << "Encrypted message: " << caesarCipher(text, key) << endl;

return 0;
}
You can also try this code with Online C++ Compiler
Run Code

Java

import java.util.Scanner;

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();

System.out.println("Encrypted message: " + encrypt(text, key));

scanner.close();
}
}
You can also try this code with Online Java Compiler
Run Code

Python

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"))

# Output
print("Encrypted message:", caesar_cipher(text, key))
You can also try this code with Online Python Compiler
Run Code

JS

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);

// Output
console.log("Encrypted message:", caesarCipher(text, key));
You can also try this code with Online Javascript Compiler
Run Code

C#

using System;

class CaesarCipher {
static string Encrypt(string text, int key) {
char[] chars = text.ToCharArray();
for (int i = 0; i < chars.Length; i++) {
char c = chars[i];
if (c >= 'a' && c <= 'z') {
c = (char)(c + key);
if (c > 'z') c = (char)(c - 'z' + 'a' - 1);
} else if (c >= 'A' && c <= 'Z') {
c = (char)(c + key);
if (c > 'Z') c = (char)(c - 'Z' + 'A' - 1);
}
chars[i] = c;
}
return new string(chars);
}

static void Main() {
Console.WriteLine("Enter a plaintext to encrypt:");
string text = Console.ReadLine();

Console.WriteLine("Enter key:");
int key = int.Parse(Console.ReadLine());

Console.WriteLine("Encrypted message: " + Encrypt(text, key));
}
}


OUTPUT:

Output for Encryption

Code for Decryption

  • 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 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;
}
You can also try this code with Online C Compiler
Run Code

C++

#include <iostream>
#include <string>

using namespace std;

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;
}

int main() {
string text;
int key;

cout << "Enter encrypted text:\n";
getline(cin, text);

cout << "Enter key:\n";
cin >> key;

cout << "Decrypted message: " << caesarDecrypt(text, key) << endl;

return 0;
}
You can also try this code with Online C++ Compiler
Run Code

Java

import java.util.Scanner;

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();

System.out.println("Decrypted message: " + decrypt(text, key));

scanner.close();
}
}
You can also try this code with Online Java Compiler
Run Code

Python

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

# Taking input
text = input("Enter encrypted text:\n")
key = int(input("Enter key:\n"))

# Output
print("Decrypted message:", caesar_decrypt(text, key))
You can also try this code with Online Python Compiler
Run Code

JS

function caesarDecrypt(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) % 26) + 97);
} else if (c >= 'A' && c <= 'Z') {
c = String.fromCharCode(((c.charCodeAt(0) - 65 - key + 26) % 26) + 65);
}
result += c;
}
return result;
}

// Taking input
const text = prompt("Enter encrypted text:");
const key = parseInt(prompt("Enter key:"), 10);

// Output
console.log("Decrypted message:", caesarDecrypt(text, key));
You can also try this code with Online Javascript Compiler
Run Code

C#

using System;

class CaesarDecryption {
static string Decrypt(string text, int key) {
char[] chars = text.ToCharArray();
for (int i = 0; i < chars.Length; i++) {
char c = chars[i];
if (c >= 'a' && c <= 'z') {
c = (char)(c - key);
if (c < 'a') c = (char)(c + 'z' - 'a' + 1);
} else if (c >= 'A' && c <= 'Z') {
c = (char)(c - key);
if (c < 'A') c = (char)(c + 'Z' - 'A' + 1);
}
chars[i] = c;
}
return new string(chars);
}

static void Main() {
Console.WriteLine("Enter encrypted text:");
string text = Console.ReadLine();

Console.WriteLine("Enter key:");
int key = int.Parse(Console.ReadLine());

Console.WriteLine("Decrypted message: " + Decrypt(text, key));
}
}


OUTPUT:

Output for Decryption

Characteristics of Caesar Cipher

  • 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.

  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.

Disadvantages of Caesar Cipher 

  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.

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++

Live masterclass