# Caesar Cipher | Cyber Security

In this article, we will learn about **Caesar cipher in Cyber Security**. We will learn the encryption technique it uses and we'll see how to implement it.

Submitted by Sahil Singh, on September 28, 2019

## Caesar Cipher

**Caesar Cipher** is one of the simplest encryption techniques. In this, every letter is represented by a fixed number. For example, **A** is represented by 0, **B** is represented by 1, **C** is represented by 2, and so on (till **Z**). In **Caesar Cipher** we take a small integer value which we use for shifting. For example, if we took 3 as out shifting key and we are shifting towards the right side, then **A** will become **D** (As **A+3 = 0+3 = 3** which is equal to D). Similarly, **B** would become **E** and so on. If we are doing left shift then D will become **A**, **E** would become **B** and so on. This method was invented by "Julius Caesar" who used this method for the privacy of his messages, hence was named after his name as **Caesar Cipher**.

In the **Caesar Cipher**, we perform two types of shift operations: either the left shift or the right. In the left shift, we minus the key value from the numeric value assigned to each digit of the plain text, whereas in the right shift, we add the key value. Hence, the encryption is done as follows:

Plain text: M Given key: k Cipher text: Left shift: (m-k) % 26 {Here, m is each letter in the PT} Right shift: (m+k) % 26

**Example:**

**Given Plain Text : 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**

**1) Let us suppose that the key = 2 **

Left Shift: In the left shift, perform the following operation on each letter of the plain text: **(m-k)%26, i.e. (m-2)%26.**

Plain Text : 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 Cipher Text : Y Z A B C D E F G H I J K L M N O P Q R S T U V W X

Right Shift: In the right shift, perform the following operation on each letter of the plain text: **(m+k)%26, i.e. (m+2)%26**

Plain Text : 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 Cipher Text : C D E F G H I J K L M N O P Q R S T U V W X Y Z A B

**2) Let us suppose that the key = 3**

Left Shift: Perform the following operation on each letter of the plain text: **(m-k)%26, i.e. (m-3)%26**

Plain Text : 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 Cipher Text : X Y Z A B C D E F G H I J K L M N O P Q R S T U V W

Right Shift: Perform the following operation on each letter of the plain text: **(m+k)%26, i.e. (m+3)%26**

Plain Text : 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 Cipher Text : D E F G H I J K L M N O P Q R S T U V W X Y Z A B C

**Now, let us encrypt a sentence using the Caesar Cipher...**

Given plain text sentence to be encrypted: **"IF YOU HAVE A DREAM TO CHASE, NOTHING CAN STOP YOU"**.

Now let us take the key value 3 with the right shift operation:

Therefore, we perform the following operation on each letter of the plain text: **(m+k)%26, i.e. (m+3)%26**

Cipher Text Sentence: **"LI BRX KDYH D GUHDP WR FKDVH, QRWKLQJ FDQ VWRS BRX"**.

TOP Interview Coding Problems/Challenges

- Run-length encoding (find/print frequency of letters in a string)
- Sort an array of 0's, 1's and 2's in linear time complexity
- Checking Anagrams (check whether two string is anagrams or not)
- Relative sorting algorithm
- Finding subarray with given sum
- Find the level in a binary tree with given sum K
- Check whether a Binary Tree is BST (Binary Search Tree) or not
- 1[0]1 Pattern Count
- Capitalize first and last letter of each word in a line
- Print vertical sum of a binary tree
- Print Boundary Sum of a Binary Tree
- Reverse a single linked list
- Greedy Strategy to solve major algorithm problems
- Job sequencing problem
- Root to leaf Path Sum
- Exit Point in a Matrix
- Find length of loop in a linked list
- Toppers of Class
- Print All Nodes that don't have Sibling
- Transform to Sum Tree
- Shortest Source to Destination Path

Comments and Discussions

**Ad:**
Are you a blogger? Join our Blogging forum.