0% found this document useful (0 votes)
10 views

Caesar Cipher in Cryptography

The Caesar Cipher is a simple encryption technique that shifts letters of the alphabet by a fixed number of positions, historically used by Julius Caesar for secure communication. Despite its ease of implementation and historical significance, it is vulnerable to modern decryption methods and brute force attacks due to its limited keyspace. The document explains the encryption and decryption process, advantages, disadvantages, and provides examples of how to use the Caesar Cipher.

Uploaded by

Shikha Singh
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Caesar Cipher in Cryptography

The Caesar Cipher is a simple encryption technique that shifts letters of the alphabet by a fixed number of positions, historically used by Julius Caesar for secure communication. Despite its ease of implementation and historical significance, it is vulnerable to modern decryption methods and brute force attacks due to its limited keyspace. The document explains the encryption and decryption process, advantages, disadvantages, and provides examples of how to use the Caesar Cipher.

Uploaded by

Shikha Singh
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 14

Caesar Cipher in Cryptography

Last Updated : 27 Jun, 2024

The Caesar Cipher is one of the simplest and oldest methods of encrypting messages, named after Julius
Caesar, who reportedly used it to protect his military communications. This technique involves shifting
the letters of the alphabet by a fixed number of places. For example, with a shift of three, the letter ‘A’
becomes ‘D’, ‘B’ becomes ‘E’, and so on. Despite its simplicity, the Caesar Cipher formed the groundwork
for modern cryptographic techniques. In this article, we’ll explore how the Caesar Cipher works, its
significance, and its impact on the development of cryptography with its advantages and disadvantages.

What is Caesar Cipher Technique?

The Caesar cipher is a simple encryption technique that was used by Julius Caesar to send secret
messages to his allies. It works by shifting the letters in the plaintext message by a certain number of
positions, known as the “shift” or “key”. The Caesar Cipher technique is one of the earliest and simplest
methods of encryption techniques.

It’s simply a type of substitution cipher, i.e., each letter of a given text is replaced by a letter with a fixed
number of positions down the alphabet. For example with a shift of 1, A would be replaced by B, B
would become C, and so on. The method is apparently named after Julius Caesar, who apparently used it
to communicate with his officials.

Aiming for a top All India Rank in GATE CS & IT 2025 exam, but not sure where you stand?

We’ve got you covered! FREE GATE CS & IT Test Series - 2025 is designed to give you the edge you need.
With previous year questions, subject-wise and full-length mock tests, and All India Mock Test, you can
get a real feel of the exam. Plus, get our live mentorship classes with experts and attend live doubt-
solving sessions to clear all your queries.

Cryptography Algorithm For the Caesar Cipher

 Thus to cipher a given text we need an integer value, known as a shift which indicates the
number of positions each letter of the text has been moved down.
The encryption can be represented using modular arithmetic by first transforming the letters
into numbers, according to the scheme, A = 0, B = 1,…, Z = 25. Encryption of a letter by a
shift n can be described mathematically as.

 For example, if the shift is 3, then the letter A would be replaced by the letter D, B would
become E, C would become F, and so on. The alphabet is wrapped around so that after Z, it starts
back at A.

 Here is an example of how to use the Caesar cipher to encrypt the message “HELLO” with a shift
of 3:
1. Write down the plaintext message: HELLO

2. Choose a shift value. In this case, we will use a shift of 3.

3. Replace each letter in the plaintext message with the letter that is three positions to the right in
the alphabet.

H becomes K (shift 3 from H)


E becomes H (shift 3 from E)
L becomes O (shift 3 from L)
L becomes O (shift 3 from L)
O becomes R (shift 3 from O)

4.The encrypted message is now “KHOOR”.

 To decrypt the message, you simply need to shift each letter back by the same number of
positions. In this case, you would shift each letter in “KHOOR” back by 3 positions to get the
original message, “HELLO”.

En(x)=(x+n)mod 26 En(x)=(x+n)mod 26
(Encryption Phase with shift n)

Dn(x)=(x−n)mod 26 Dn(x)=(x−n)mod 26
(Decryption Phase with shift n)

Examples :

Text : ABCDEFGHIJKLMNOPQRSTUVWXYZ
Shift: 23
Cipher: XYZABCDEFGHIJKLMNOPQRSTUVW

Text : ATTACKATONCE
Shift: 4
Cipher: EXXEGOEXSRGI
Advantages

 Easy to implement and use thus, making suitable for beginners to learn about encryption.

 Can be physically implemented, such as with a set of rotating disks or a set of cards, known as a
scytale, which can be useful in certain situations.

 Requires only a small set of pre-shared information.

 Can be modified easily to create a more secure variant, such as by using a multiple shift values or
keywords.

Disadvantages

 It is not secure against modern decryption methods.

 Vulnerable to known-plaintext attacks, where an attacker has access to both the encrypted and
unencrypted versions of the same messages.

 The small number of possible keys means that an attacker can easily try all possible keys until the
correct one is found, making it vulnerable to a brute force attack.

 It is not suitable for long text encryption as it would be easy to crack.

 It is not suitable for secure communication as it is easily broken.

 Does not provide confidentiality, integrity, and authenticity in a message.

Features of Caesar Cipher

1. Substitution cipher: The Caesar cipher is a type of substitution cipher, where each letter in the
plaintext is replaced by a letter some fixed number of positions down the alphabet.

2. Fixed key: The Caesar cipher uses a fixed key, which is the number of positions by which the
letters are shifted. This key is known to both the sender and the receiver.

3. Symmetric encryption: The Caesar cipher is a symmetric encryption technique, meaning that
the same key is used for both encryption and decryption.

4. Limited keyspace: The Caesar cipher has a very limited keyspace of only 26 possible keys, as
there are only 26 letters in the English alphabet.

5. Vulnerable to brute force attacks: The Caesar cipher is vulnerable to brute force attacks, as
there are only 26 possible keys to try.

6. Easy to implement: The Caesar cipher is very easy to implement and requires only simple
arithmetic operations, making it a popular choice for simple encryption tasks.

Rules for the Caesar Cipher

1. Choose a number between 1 and 25. This will be your “shift” value.

2. Write down the letters of the alphabet in order, from A to Z.


3. Shift each letter of the alphabet by the “shift” value. For example, if the shift value is 3, A would
become D, B would become E, C would become F, and so on.

4. Encrypt your message by replacing each letter with the corresponding shifted letter. For
example, if the shift value is 3, the word “hello” would become “khoor”.

5. To decrypt the message, simply reverse the process by shifting each letter back by the same
amount. For example, if the shift value is 3, the encrypted message “khoor” would become
“hello”.

Algorithm for Caesar Cipher

Input:

1. Choose a shift value between 1 and 25.

2. Write down the alphabet in order from A to Z.

3. Create a new alphabet by shifting each letter of the original alphabet by the shift value. For
example, if the shift value is 3, the new alphabet would be:

4. A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
DEFGHIJKLMNOPQRSTUVWXYZABC

5. Replace each letter of the message with the corresponding letter from the new alphabet. For
example, if the shift value is 3, the word “hello” would become “khoor”.

6. To decrypt the message, shift each letter back by the same amount. For example, if the shift
value is 3, the encrypted message “khoor” would become “hello”.

Procedure:

 Traverse the given text one character at a time .

 For each character, transform the given character as per the rule, depending on whether
we’re encrypting or decrypting the text.

 Return the new string generated.

A program that receives a Text (string) and Shift value( integer) and returns the encrypted text.

C++JavaPythonC#JavaScriptPHP

#A python program to illustrate Caesar Cipher Technique

def encrypt(text,s):

result = ""
4

# traverse text

for i in range(len(text)):

char = text[i]

# Encrypt uppercase characters

10

if (char.isupper()):

11

result += chr((ord(char) + s-65) % 26 + 65)

12

13

# Encrypt lowercase characters

14

else:

15

result += chr((ord(char) + s - 97) % 26 + 97)

16

17

return result

18
19

#check the above function

20

text = "ATTACKATONCE"

21

s=4

22

print ("Text : " + text)

23

print ("Shift : " + str(s))

24

print ("Cipher: " + encrypt(text,s))

Output

Text : ATTACKATONCE

Shift: 4

Cipher: EXXEGOEXSRGI

Time complexity: O(N) where N is length of the given text


Auxiliary space: O(N)

How to decrypt?
We can either write another function decrypt similar to encrypt, that’ll apply the given shift in the
opposite direction to decrypt the original text. However we can use the cyclic property of the cipher
under modulo, hence we can simply observe

Cipher(n) = De-cipher(26-n)

Hence, we can use the same function to decrypt, instead, we’ll modify the shift value such that shift =
26-shift (Refer to this for a sample run in C++).

Conclusion

The Caesar Cipher, with its straightforward approach of shifting letters, serves as an excellent
introduction to the world of cryptography. While it is easy to understand and implement, its simplicity
also makes it vulnerable to basic attacks. Despite these limitations, the Caesar Cipher’s historical role is
significant, it represents the early efforts to secure communication and has made the way for the more
advanced encryption methods used today. Understanding the Caesar Cipher helps us appreciate the
evolution of cryptographic techniques and the ongoing quest to protect information in our digital age.

Frequently Asked Question on Caesar Cipher in Cryptography -FAQs

What is the Caesar cipher in cryptography?

The Caesar Cipher, used by Julius Caesar around 58 BC, is a method that scrambles a message by shifting
its letters. For example, shifting ‘A’ by three positions makes it ‘D’. To read the message, the receiver
reverses this shift. Later, an Arab mathematician cracked the Caesar Cipher by analyzing how often each
letter appears, which helped him figure out the pattern and decode the message.

What is an example of decryption in Caesar cipher?

An example of decryption in the Caesar cipher is decrypting the message KHOOR. If the original shift used
to encrypt the message was 3, you would reverse this by shifting each letter back by 3 places in the
alphabet. Thus, K shifts to H, H to E, O to L, and R to O, revealing the decrypted message: HELLO.

What are keys in Caesar cipher?

In the Caesar cipher, the key is a letter that shows how many places to shift each letter in the message.
For example, a key D means “shift 3 places,” and a key M means “shift 12 places.” A key A means “do not
shift,” and a key Z means either “shift 25 places” or “shift one place backwards.”

Caesar Cipher Technique

The Caesar cipher is the simplest and oldest method of cryptography. The Caesar cipher method is based
on a mono-alphabetic cipher and is also called a shift cipher or additive cipher. Julius Caesar used the
shift cipher (additive cipher) technique to communicate with his officers. For this reason, the shift cipher
technique is called the Caesar cipher. The Caesar cipher is a kind of replacement (substitution) cipher,
where all letter of plain text is replaced by another letter.

Let's take an example to understand the Caesar cipher, suppose we are shifting with 1, then A will be
replaced by B, B will be replaced by C, C will be replaced by D, D will be replaced by C, and this process
continues until the entire plain text is finished.

Caesar ciphers is a weak method of cryptography. It can be easily hacked. It means the message
encrypted by this method can be easily decrypted.

Plaintext: It is a simple message written by the user.

Ciphertext: It is an encrypted message after applying some technique.

The formula of encryption is:

En (x) = (x + n) mod 26
The formula of decryption is:

Dn (x) = (xi - n) mod 26

If any case (Dn) value becomes negative (-ve), in this case, we will add 26 in the negative value.

Where,

E denotes the encryption


D denotes the decryption
x denotes the letters value
n denotes the key value (shift value)

Note: "i" denotes the offset of the ith number of the letters, as shown in the table below.

Example: 1 Use the Caesar cipher to encrypt and decrypt the message "JAVATPOINT," and the key (shift)
value of this message is 3.

Encryption

We apply encryption formulas by character, based on alphabetical order.

The formula of encryption is:

En (x) = (x + n) mod 26

Plaintext: J → 09 En: (09 + 3) mod 26 Ciphertext: 12 → M

Plaintext: A → 00 En: (00 + 3) mod 26 Ciphertext: 3 → D

Plaintext: V → 21 En: (21 + 3) mod 26 Ciphertext: 24 → Y

Plaintext: A → 00 En: (00 + 3) mod 26 Ciphertext: 3 → D

Plaintext: T → 19 En: (19 + 3) mod 26 Ciphertext: 22 → W

Plaintext: P → 15 En: (15 + 3) mod 26 Ciphertext: 18 → S

Plaintext: O → 14 En: (14 + 3) mod 26 Ciphertext: 17 → R


Plaintext: I → 08 En: (08 + 3) mod 26 Ciphertext: 11 → L

Plaintext: N → 13 En: (13 + 3) mod 26 Ciphertext: 16 → Q

Plaintext: T → 19 En: (19 + 3) mod 26 Ciphertext: 22 → W

The encrypted message is "MDYDWSRLQW". Note that the Caesar cipher is monoalphabetic, so the
same plaintext letters are encrypted as the same letters. For example, "JAVATPOINT" has "A", encrypted
by "D".

Decryption

We apply decryption formulas by character, based on alphabetical order.

The formula of decryption is:

Dn (x) = (xi - n) mod 26

If any case (Dn) value becomes negative (-ve), in this case, we will add 26 in the negative value.

Ciphertext: M → 12 Dn: (12 - 3) mod 26 Plaintext: 09 → J

Ciphertext: D → 03 Dn: (03 - 3) mod 26 Plaintext: 0 → A

Ciphertext: Y → 24 Dn: (24 - 3) mod 26 Plaintext: 21 → V

Plaintext: A → 00 En: (00 + 3) mod 26 Ciphertext: 3 → D

Plaintext: T → 19 En: (19 + 3) mod 26 Ciphertext: 22 → W

Plaintext: P → 15 En: (15 + 3) mod 26 Ciphertext: 18 → S

Plaintext: O → 14 En: (14 + 3) mod 26 Ciphertext: 17 → R

Plaintext: I → 08 En: (08 + 3) mod 26 Ciphertext: 11 → L

Plaintext: N → 13 En: (13 + 3) mod 26 Ciphertext: 16 → Q

Plaintext: T → 19 En: (19 + 3) mod 26 Ciphertext: 22 → W

The decrypted message is "JAVATPOINT".


Example: 2 Use the Caesar cipher to encrypt and decrypt the message "HELLO," and the key (shift) value
of this message is 15.

Encryption

We apply encryption formulas by character, based on alphabetical order.

The formula of encryption is:

En (x) = (x + n) mod 26

Plaintext: H → 07 En: (07 + 15) mod 26 Ciphertext: 22 → W

Plaintext: E → 04 En: (04 + 15) mod 26 Ciphertext: 19 → T

Plaintext: L → 11 En: (11 + 15) mod 26 Ciphertext: 00 → A

Plaintext: L → 11 En: (11 + 15) mod 26 Ciphertext: 00 → A

Plaintext: O → 14 En: (14 + 15) mod 26 Ciphertext: 03 → D

Note that the Caesar cipher is monoalphabetic, so the same plaintext letters are encrypted as the same
letters. Like, "HELLO" has "L", encrypted by "A".

The encrypted message of this plain text is "WTAAD".

Decryption

We apply decryption formulas by character, based on alphabetical order.

The formula of decryption is:

Dn (x) = (xi - n) mod 26

Ciphertext: W → 22 Dn: (22 - 15) mod 26 Plaintext: 07 → H

Ciphertext: T → 19 Dn: (19 - 15) mod 26 Plaintext: 04 → E

Ciphertext: A → 00 Dn: (00 - 15) mod 26 Plaintext: 11 → L

Ciphertext: A → 00 Dn: (00 - 15) mod 26 Plaintext: 11 → L

Ciphertext: D → 03 Dn: (03 - 15) mod 26 Plaintext: 14 → O

The decrypted message is "HELLO".


Note: If any case (Dn) value becomes negative (-ve), in this case, we will add 26 in the negative value.
Like, the third letter of the ciphertext.

Dn = (00 - 15) mod 26


= -15

The value of dn is negative, so 26 will be added to it.

= -15 + 26
= 11

Advantages of Caesar cipher

Its benefits are as follows: -

1. It is very easy to implement.

2. This method is the simplest method of cryptography.

3. Only one short key is used in its entire process.

4. If a system does not use complex coding techniques, it is the best method for it.

5. It requires only a few computing resources.

Disadvantages of Caesar cipher

Its disadvantages are as follows: -

1. It can be easily hacked. It means the message encrypted by this method can be easily decrypted.

2. It provides very little security.

3. By looking at the pattern of letters in it, the entire message can be decrypted.

Program

C program for the encryption:

1. #include<stdio.h>

2. #include<conio.h>

3. int main()

4. {

5. int i, f;

6. char pop[100], c;

7. printf(" enter a plaintext \n");

8. gets(pop);

9. printf("enter key");
10. scanf("%d", &f);

11. for(i = 0; pop[i] ! = '\0'; i++)

12. {

13. c = pop[i];

14. if(c >= 'a' && c <= 'z')

15. {

16. c = c + f;

17. if(c > 'z')

18. {

19. c = c - 'z' + 'a' - 1;

20. }

21. pop[i] = c;

22. }

23. else if(c >= 'A' && c < 'Z')

24. {

25. c = c + f;

26. if(c > 'Z')

27. {

28. c = c = 'Z' + 'A' - 1;

29. }

30. pop[i] = c;

31. }

32. }

33. printf(" Encrypted message: %s", pop);

34. return 0;

35. }

C program for the decryption

1. #include<stdio.h>

2. #include<conio.h>
3. int main()

4. {

5. int i, f;

6. char pop[100], c;

7. printf("enter a encrypted test \t");

8. gets(pop);

9. printf("enter key \t");

10. scanf("%d", &f);

11. for(i = 0; pop[i] != '\0'; i++)

12. {

13. c = pop[i];

14. if(c >= 'a' && c <= 'z')

15. {

16. c = c - f;

17. if(c < 'a')

18. {

19. c = c + 'z' - 'a' + 1;

20. }

21. pop[i] = c;

22. }

23. else if(c >= 'A' && c < 'Z')

24. {

25. c = c + f;

26. if(c < 'A')

27. {

28. c = c + 'Z' - 'A' + 1;

29. }

30. pop[i] = c;

31. }
32. }

33. printf(" Dycrypted message: %s", pop);

34. return 0;

35. }

You might also like