Learn and Use the Caesar Cipher


The Caesar cipher is one of the simplest and oldest methods for encrypting messages. Named after Julius Caesar, who used it to protect his military communications, this cipher works by shifting each letter in a message by a fixed number of positions in the alphabet. This number is called the “key” or “shift” value.
How does it work?
First, you pick a shift value (for example, 3).
To encrypt a message, you replace every letter with the letter that comes a certain number of places later in the alphabet. For example, with a shift of 3:
A becomes D
B becomes E
C becomes F …and so on.
If you reach the end of the alphabet, you wrap around to the beginning (so X with a shift of 3 becomes A).
Spaces, punctuation, and numbers are not changed — only letters are shifted.
To decrypt the message, the receiver shifts the letters back by the same number.
Example:
If your message is “HELLO” and your shift is 3, the encrypted message becomes “KHOOR”.
Code (C Language):
queue.h
#ifndef QUEUE_H
#define QUEUE_H
#define MAXQUEUE 26
typedef char QueueElement;
typedef struct {
int front;
int rear;
int count;
QueueElement items[MAXQUEUE];
} CircularQueue;
void CreateQueue(CircularQueue q);
void EncryptMessage(CircularQueue q, char *message, int key);
#endif
main.c
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include “queue.h”
//Queue setup
void CreateQueue(CircularQueue *q) {
q->front = 0;
q->rear = MAXQUEUE — 1;
q->count = 0;
for (int i = 0; i < MAXQUEUE; i++) {
q->items[i] = ‘A’ + i;
q->count++;
}
}
//encrypt
char Rotate(CircularQueue *q, char ch, int key) {
if (!isalpha(ch)) return ch;
int is_lower = islower(ch);
ch = toupper(ch);
int idx = ch — ‘A’;
int new_idx = (idx + key + MAXQUEUE) % MAXQUEUE;
char shifted = q->items[new_idx];
return is_lower ? tolower(shifted) : shifted;
}
void EncryptMessage(CircularQueue q, char message, int key) {
for (int i = 0; message[i] != ‘\0’; i++) {
printf(“%c”, Rotate(q, message[i], key));
}
printf(“\n”);
}
int main() {
CircularQueue q;
CreateQueue(&q);
char choice;
char message[100];
int key;
printf(“Encryption (E) or Decryption (D)?: “);
scanf(“ %c”, &choice);
getchar();
printf(“Enter your message: “);
fgets(message, sizeof(message), stdin);
message[strcspn(message, “\n”)] = 0;
printf(“Enter shift key: “);
scanf(“%d”, &key);
printf(“*****************************\n”);
if (choice == ‘E’) {
printf(“Encrypted Message: “);
EncryptMessage(&q, message, key);
} else if (choice == ‘D’) {
printf(“Original Message: “);
EncryptMessage(&q, message, -key); // Decryption is reverse shift
} else {
printf(“Invalid choice.\n”);
}
return 0;
}
Why is it important?
The Caesar cipher is easy to understand and use, making it a great introduction to cryptography. However, because there are only 26 possible shifts in English, it can be easily broken by trying all possible keys or by analyzing letter patterns.
In summary:
The Caesar cipher hides messages by shifting letters up or down the alphabet by a set amount. It’s simple, fun, and a great way to learn about the basics of secret codes and encryption.
Subscribe to my newsletter
Read articles from Sathsarani Geethamali directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
