# Caesar Cipher Technique in Cryptography

0 comment

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 26The formula of decryption is:Dn (x) = (xi - n) mod 26If any case (Dn) value becomes negative (-ve), in this case, we will add 26 in the negative value.Where,En denotes the encryptionDn denotes the decryptionx denotes the letters valuen denotes the key value (shift value)`

Suppose,

```Text : ABCDEFGHIJKLMNOPQRSTUVWXYZ
Shift: 23
Cipher: XYZABCDEFGHIJKLMNOPQRSTUVW

Text : ATTACKATONCE
Shift: 4
Cipher: EXXEGOEXSRGI```

Algorithm for Caesar Cipher:
Input:

1. A String of lower case letters, called Text.
2. 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`

#### 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`

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

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.

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.

### C++ program to illustrate Caesar Cipher Technique

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

```.wp-block-code {
border: 0;
}

.wp-block-code > div {
overflow: auto;
}

.shcb-language {
border: 0;
clip: rect(1px, 1px, 1px, 1px);
-webkit-clip-path: inset(50%);
clip-path: inset(50%);
height: 1px;
margin: -1px;
overflow: hidden;
position: absolute;
width: 1px;
word-wrap: normal;
word-break: normal;
}

.hljs {
box-sizing: border-box;
}

.hljs.shcb-code-table {
display: table;
width: 100%;
}

.hljs.shcb-code-table > .shcb-loc {
color: inherit;
display: table-row;
width: 100%;
}

.hljs.shcb-code-table .shcb-loc > span {
display: table-cell;
}

.wp-block-code code.hljs:not(.shcb-wrap-lines) {
white-space: pre;
}

.wp-block-code code.hljs.shcb-wrap-lines {
white-space: pre-wrap;
}

.hljs.shcb-line-numbers {
border-spacing: 0;
counter-reset: line;
}

.hljs.shcb-line-numbers > .shcb-loc {
counter-increment: line;
}

.hljs.shcb-line-numbers .shcb-loc > span {
}

.hljs.shcb-line-numbers .shcb-loc::before {
border-right: 1px solid #ddd;
content: counter(line);
display: table-cell;
text-align: right;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
white-space: nowrap;
width: 1%;
}
```// 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;
}
```Code language: C++ (cpp)```

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!