Table of Contents
In this tutorial you’ll learn about Caesar Cipher Technique in Cryptography, which is based on mono-alphabetic cipher.
The Caesar cipher method is based on a mono-alphabetic cipher and is also called a shift cipher or additive cipher.It is one of the earliest and simplest method of encryption technique.
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.
- Plaintext: It is a simple message written by the user.
- Ciphertext: It is an encrypted message after applying some technique.
For example with a shift of 1, A would be replaced by B, B would become C, and so on. Thus to cipher a given text we need an integer value, known as shift which indicates the number of position each letter of the text has been moved down.
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.
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,
En denotes the encryption
Dn denotes the decryption
x denotes the letters value
n denotes the key value (shift value)
Suppose,
Text : ABCDEFGHIJKLMNOPQRSTUVWXYZ Shift: 23 Cipher: XYZABCDEFGHIJKLMNOPQRSTUVW Text : ATTACKATONCE Shift: 4 Cipher: EXXEGOEXSRGI
Algorithm for Caesar Cipher:
Input:
- A String of lower case letters, called Text.
- An Integer between 0-25 denoting the required shift.
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.
EXAMPLE
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: –
- It is very easy to implement.
- This method is the simplest method of cryptography.
- Only one short key is used in its entire process.
- If a system does not use complex coding techniques, it is the best method for it.
- It requires only a few computing resources.
Disadvantages of Caesar cipher
Its disadvantages are as follows: –
- It can be easily hacked. It means the message encrypted by this method can be easily decrypted.
- It provides very little security.
- By looking at the pattern of letters in it, the entire message can be decrypted.
C++ program to illustrate Caesar Cipher Technique
Program that receives a Text (string) and Shift value( integer) and returns the encrypted text.
// A C++ program to illustrate Caesar Cipher Technique
#include <iostream>
using namespace std;
// This function receives text and shift and
// returns the encrypted text
string encrypt(string text, int s)
{
string result = "";
// traverse text
for (int i=0;i<text.length();i++)
{
// apply transformation to each character
// Encrypt Uppercase letters
if (isupper(text[i]))
result += char(int(text[i]+s-65)%26 +65);
// Encrypt Lowercase letters
else
result += char(int(text[i]+s-97)%26 +97);
}
// Return the resulting string
return result;
}
// Driver program to test the above function
int main()
{
string text="ATTACKATONCE";
int s = 4;
cout << "Text : " << text;
cout << "\nShift: " << s;
cout << "\nCipher: " << encrypt(text, s);
return 0;
}
Hope you like this article on Caesar Cipher Technique. Thank you for reading, If you have reached so far, please like the article, It will encourage me to write more such articles. Do share your valuable suggestions, I appreciate your honest feedback!