Cryptography is the technique used for securing communication. It uses encryption and decryption algorithms to protect data. Encryption is the process of converting plain text (human-readable) text into cipher text. Decryption is the process of converting non-human readable text back into plain text. These algorithms are called Cipher.

This article will discuss one cryptographic method, the Shift cipher. Then we will see the formula by which encryption and decryption are performed. We will also discuss the implementation of shift cipher. Lastly, we will discuss the advantages and disadvantages of using shift cipher. But before that, let's see what cryptography is.

Cryptography

It is the study of encrypting and decrypting data using mathematics. It allows you to store sensitive information or send it through insecure networks. cryptography can be powerful or weak. The time and resources required determine its strength. Strong cryptography produces ciphertext that is extremely difficult to decode.

Cryptography is a word used in computing to describe secured information systems that transform messages in difficult hard-to-decipher ways using mathematical concepts and a sequence of rule-based computations known as algorithms. These proposed approaches are used for encryption keys generation, digital signature, data privacy verification, online surfing, and private communications, including banking transactions and email.

There are mainly three types of cryptography techniques.

Public key or symmetric key cryptography

Secret key or asymmetric key cryptography

Hash functions

Get the tech career you deserve, faster!

Connect with our expert counsellors to understand how to hack your way to success

User rating 4.7/5

1:1 doubt support

95% placement record

Akash Pal

Senior Software Engineer

326% Hike After Job Bootcamp

Himanshu Gusain

Programmer Analyst

32 LPA After Job Bootcamp

After Job Bootcamp

Shift Cipher

The 'Shift Cipher' is one of the oldest cryptography methods. It is named after Julius Caesar. In the first century, Suetonius, a Roman historian, wrote ‘The Lives of the Twelve Caesars’. This book contains about shift cipher. It was a technique used by Julius Caesar to send secret messages. If he had to say anything confidential, he wrote it in cipher. So in honor of Julius Ceasar, this cipher is known as the shift/caesar cipher. Later on, Suetonius explained the encryption and decryption process. He also described that encryption and description are reverse processes.

In shift cipher, each letter of the message is shifted down by ‘K’ letters. Where ‘K’ is any number specified by the user. For example, Earlier people uses cipher disks to decrypt or encrypt the message. People rotate the disk and write the message using corresponding alphabets. The disk looks like -

Formula Used

The formula used for encryption is -

E(m) = (m + k) mod 26

The formula of decryption is:

D(m) = (m - k) mod 26

Where,

E depicts encryption

D depicts decryption

m denotes the letter-value

k denotes the key-value

Let’s see an example for a better understanding.

Example

Let’s say we want to encrypt the word ‘CODING’ and the key is 5. The table of letters used will be as follows -

A

B

C

D

E

F

G

H

I

J

K

L

M

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

N

O

P

Q

R

S

T

U

V

W

X

Y

Z

If we apply the encryption formula on the word ‘CODING’ with k = 5, then the ciphertext will be as

Plaintext

Encryption

Ciphertext

C - 03

E = (03 + 5)%26

08 - H

O - 15

E = (15 + 5)%26

20 - T

D - 04

E = (04 + 5)%26

09 - I

I - 09

E = (09 + 5)%26

14 - N

N - 14

E = (14 + 5)%26

19 - S

G - 07

E = (07 + 5)%26

12 - L

So, the encrypted message is ‘HTINSL’

Using the decryption formula on this ciphertext with K = 5. The plaintext received will be as follows -

Ciphertext

Decryption

Plaintext

H - 08

D = (08 - 5)%26

03 - C

T - 20

D = (20 - 5)%26

15 - O

I - 09

D = (09 - 5)%26

04 - D

N - 14

D = (14 - 5)%26

09 - I

S - 19

D = (19 - 5)%26

14 - N

L - 12

D = (12 - 5)%26

07 - G

Yes, we got our original message i.e., ‘CODING’, back.

Implementation

Let’s see the implementation of shift cipher -

Code

#include <bits/stdc++.h>
using namespace std;
// Create encryption function
string encryption(string message, int size, int key)
{
for(int i=0;i<size;i++)
{
char c = message[i];
// Checking for lower-case letters
if(c >= 'a' && c <= 'z')
{
c = c + key;
if(c > 'z')
c = c - 'z' + 'a' - 1;
message[i] = c;
}
//Checking for upper-case letters
else if(c >= 'A' && c < 'Z')
{
c = c + key;
if(c > 'Z')
c = c = 'Z' + 'A' - 1;
message[i] = c;
}
}
return message;
}
// Create decryption function
string decryption(string message, int size, int key)
{
for(int i=0;i<size;i++)
{
char c = message[i];
// Checking for lower-case letters
if(c >= 'a' && c <= 'z')
{
c = c - key;
if(c < 'a')
c = c + 'z' - 'a' + 1;
message[i] = c;
}
// Checking for upper-case letters
else if(c >= 'A' && c <= 'Z')
{
c = c - key;
if(c < 'A')
c = c + 'Z' - 'A' + 1;
message[i] = c;
}
}
return message;
}
int main()
{
string message;
int key;
// Taking User Input
cout<<"Enter your message"<<endl;
getline(cin, message);
cout<<endl;
cout<<"Enter key"<<endl;
cin>>key;
int n=message.size();
cout<<endl;
string s = encryption(message, n, key);
cout<<"Encrypted message: "<<s<<endl;
message = decryption(s, n, key);
cout<<"Decrypted message: "<<message<<endl;
return 0;
}

Output

Advantages

Shift cipher has the following advantages -

It is simple and very easy to implement.

Only one short key is used in the entire process. The same key is used for encryption and decryption.

If the system does not use complex coding techniques, it is the method and easy too.

Only a few computing resources are required.

Disadvantages

Shift cipher has the following disadvantages -

Easy to hack. Attackers can use a brute force approach for description.

It is less secure.

The key used is not different and easy to detect.

The message is easy to decrypt by just looking at the pattern.

Frequently Asked Questions

What is cryptography?

Cryptography is a method of encrypting and decrypting messages and information to establish a secure information exchange between the sender and the receiver.

What is shift cipher?

Shift cipher is a cryptographic algorithm. It shifts each letter of a message forward by a specified number.

What are encryption and decryption?

Encryption is the conversion of plain text into ciphertext. In contrast, decryption is the process that converts ciphertext into plaintext.

How many different shifts are possible in a Caesar cipher?

The number of shifts possible will be one less than the number of letters present i.e., 26(0-25) in English.

What are plain text and ciphertext?

The plaintext can refer to anything that humans can understand. In contrast, the ciphertext is a series of randomized letters that don't make any sense to humans.

Conclusion

In this article, we have discussed the shift cipher. We also discussed the formula used for encryption and decryption by this Cipher. Using this formula, we implemented shift cipher. And at last, we discussed the advantages and disadvantages of using this shift cipher.

Do not stop learning! We recommend you read these articles -.