Javatpoint Logo
Javatpoint Logo

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.

Caesar Cipher Technique

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:

C program for the decryption






Youtube For Videos Join Our Youtube Channel: Join Now

Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA